Class: Pione::Lang::OrdinalSequence

Inherits:
Sequence show all
Defined in:
lib/pione/lang/ordinal-sequence.rb

Overview

OridinalSequence is a sequence that has an ordinal number index.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sequence

#assertive?, #attribute, #concat, #each, #eval, index_type, inherited, piece_class, piece_classes, #push, #set_annotation_type, set_index_type, #update_pieces, void, #void?

Methods included from Callable

#call_pione_method

Methods included from Util::Positionable

#line_and_column, #pos, #set_source_position

Methods inherited from Expr

#eval, #eval!, inherited, pione_type, set_pione_type, #textize, #to_s

Class Method Details

.fold(init, seq, &b) ⇒ Object

Fold pieces with the init sequence.



43
44
45
46
47
# 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) ⇒ Object

Fold pieces of two sequences with the init sequence.



50
51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
65
# 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) ⇒ Object

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



17
18
19
# 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) ⇒ Object

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



22
23
24
25
26
27
28
29
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object

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



10
11
12
13
# 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

Instance Method Details

#emptyObject

Return the sequecen with no pieces.



71
72
73
# File 'lib/pione/lang/ordinal-sequence.rb', line 71

def empty
  set(pieces: [])
end

#fold(init, &b) ⇒ Object

Fold my pieces with initial sequence.



111
112
113
# 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) ⇒ Object

Fold my pieces and other pieces with initial sequence.



116
117
118
119
120
121
122
# 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

#inspectObject



124
125
126
# File 'lib/pione/lang/ordinal-sequence.rb', line 124

def inspect
  "#%s%s" % [self.class.name.split("::").last, to_h.inspect]
end

#map(&b) ⇒ Object

Map my pieces by applying the block.



76
77
78
# 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) ⇒ Object

Map my pieces and other pieces.



90
91
92
93
94
95
96
97
# 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) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object

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



81
82
83
84
85
86
87
# 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