class Pione::Lang::OrdinalSequence

OridinalSequence is a sequence that has an ordinal number index.

Public Class Methods

fold(init, seq, &b) click to toggle source

Fold pieces with the init sequence.

# File lib/pione/lang/ordinal-sequence.rb, line 43
def fold(init, seq, &b)
  seq.pieces.inject(init) do |_seq, piece|
    b.call(_seq, piece)
  end
end
fold2(init, seq1, seq2, &b) click to toggle source

Fold pieces of two sequences with the init sequence.

# File lib/pione/lang/ordinal-sequence.rb, line 50
def fold2(init, seq1, seq2, &b)
  seq1.pieces.inject(init) do |_seq1, piece1|
    seq2.pieces.inject(_seq1) do |_seq2, piece2|
      b.call(_seq2, piece1, piece2)
    end
  end
end
make_piece(val) click to toggle source
# File lib/pione/lang/ordinal-sequence.rb, line 58
def make_piece(val)
  if val.is_a?(Piece)
    val
  else
    klass = piece_classes.first
    klass.new({klass.members.first => val})
  end
end
map(seq, &b) click to toggle source

Map pieces of the sequence by applying the block and build a new sequence with it.

# File lib/pione/lang/ordinal-sequence.rb, line 17
def map(seq, &b)
  of(*seq.pieces.map{|piece| b.call(piece)})
end
map2(seq1, seq2, &b) click to toggle source

Map pieces of two sequence and build a new sequence with it.

# File lib/pione/lang/ordinal-sequence.rb, line 22
def map2(seq1, seq2, &b)
  vals = seq1.pieces.map do |piece1|
    seq2.pieces.map do |piece2|
      b.call(piece1, piece2)
    end
  end.flatten
  of(*vals)
end
map3(seq1, seq2, seq3, &b) click to toggle source
# File lib/pione/lang/ordinal-sequence.rb, line 31
def map3(seq1, seq2, seq3, &b)
  vals = seq1.pieces.map do |piece1|
    seq2.pieces.map do |piece2|
      seq3.pieces.map do |piece3|
        b.call(piece1, piece2, piece3)
      end
    end
  end.flatten
  of(*vals)
end
of(*args) click to toggle source

Build a sequence with arguments. The arguments can be values or pieces.

# File lib/pione/lang/ordinal-sequence.rb, line 10
def of(*args)
  # map objects to pieces and create a sequece with it
  new(pieces: args.map {|arg| make_piece(arg)})
end

Public Instance Methods

empty() click to toggle source

Return the sequecen with no pieces.

# File lib/pione/lang/ordinal-sequence.rb, line 71
def empty
  set(pieces: [])
end
fold(init, &b) click to toggle source

Fold my pieces with initial sequence.

# File lib/pione/lang/ordinal-sequence.rb, line 111
def fold(init, &b)
  pieces.inject(init) {|_seq, piece| b.call(_seq, piece)}
end
fold2(init, other, &b) click to toggle source

Fold my pieces and other pieces with initial sequence.

# File lib/pione/lang/ordinal-sequence.rb, line 116
def fold2(init, other, &b)
  pieces.inject(init) do |_seq1, piece1|
    other.pieces.inject(_seq1) do |_seq2, piece2|
      b.call(_seq2, piece1, piece2)
    end
  end
end
inspect() click to toggle source
# File lib/pione/lang/ordinal-sequence.rb, line 124
def inspect
  "#%s%s" % [self.class.name.split("::").last, to_h.inspect]
end
map(&b) click to toggle source

Map my pieces by applying the block.

# File lib/pione/lang/ordinal-sequence.rb, line 76
def map(&b)
  set(pieces: pieces.map{|piece| make_piece(b.call(piece))})
end
map2(other, &b) click to toggle source

Map my pieces and other pieces.

# File lib/pione/lang/ordinal-sequence.rb, line 90
def map2(other, &b)
  _pieces = pieces.map do |piece1|
    other.pieces.map do |piece2|
      make_piece(b.call(piece1, piece2))
    end
  end.flatten
  set(pieces: _pieces)
end
map3(other1, other2, &b) click to toggle source
# File lib/pione/lang/ordinal-sequence.rb, line 99
def map3(other1, other2, &b)
  _pieces = pieces.map do |piece1|
    other1.pieces.map do |piece2|
      other2.pieces.map do |piece3|
        make_piece(b.call(piece1, piece2, piece3))
      end
    end
  end.flatten
  set(pieces: _pieces)
end
map_by(seq, &b) click to toggle source

Map by the sequence and build a new sequence of result piece based on self.

# File lib/pione/lang/ordinal-sequence.rb, line 81
def map_by(seq, &b)
  # build new pieces by applying the block
  _pieces = seq.pieces.map {|piece| make_piece(b.call(piece))}

  # create a new sequence
  set(pieces: _pieces)
end