Module: Pacer::Core::ArrayRoute

Defined in:
lib/pacer/core/array_route.rb

Instance Method Summary collapse

Instance Method Details

#compactedObject



65
66
67
68
69
# File 'lib/pacer/core/array_route.rb', line 65

def compacted
  map element_type: element_type, route_name: 'compact' do |a|
    a.compact
  end
end

#edges(*exts) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/pacer/core/array_route.rb', line 126

def edges(*exts)
  r = select_case Pacer::Edge
  if exts.any?
    r.map_in { |e| e.add_extensions exts }
  else
    r
  end
end

#elements(*exts) ⇒ Object



135
136
137
138
139
140
141
142
# File 'lib/pacer/core/array_route.rb', line 135

def elements(*exts)
  r = select_case Pacer::Element
  if exts.any?
    r.map_in { |e| e.add_extensions exts }
  else
    r
  end
end

#flatten(*opts) ⇒ Object



61
62
63
# File 'lib/pacer/core/array_route.rb', line 61

def flatten(*opts)
  scatter(*opts)
end

#heads(et = nil) ⇒ Object



71
72
73
74
75
# File 'lib/pacer/core/array_route.rb', line 71

def heads(et = nil)
  map element_type: et, route_name: 'heads' do |a|
    a.first
  end
end

#help(section = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pacer/core/array_route.rb', line 4

def help(section = nil)
  case section
  when :arrays
    puts "The following array route methods are available:\n\n#lengths            Return the length of each array\n\n#transpose          Route version of Ruby's Array#transpase\n\n#compacted          Removes nils from each array\n\n#heads              Route to only the first element from each array\n\n#tails              Route to only the last element from each array\n\n#pairs(head, tail)  Route to an array of only the head and tail elements\n    head: Number    Array index of the : first  : element in the pair\n    tail: Number                       : second :\n\n#len(n)             Filter paths by length\n    n: Number | Range\n\n#map_in             Map on each array\n#reduce_in          Reduce on each array\n#select_in          Select on each array\n#reject_in          Reject on each array\n\n#select_case(*cases) Simplified select on each array without needing a block\n#reject_case(*cases)\n    cases: the same type of objects you would give to case statements\n     (ie. exact match, regex, type, etc)\n\n#vertices(*exts)    Filter each array to only its vertices\n#edges(*exts)           \"        \"        \"       edges\n#elements(*exts)        \"        \"        \"       elements\n    exts: extensions to add to the filtered elements\n\n"
  else
    super
  end
  description
end

#len(n) ⇒ Object



89
90
91
92
93
# File 'lib/pacer/core/array_route.rb', line 89

def len(n)
  select do |path|
    n === path.length
  end
end

#lengthsObject



49
50
51
# File 'lib/pacer/core/array_route.rb', line 49

def lengths
  map(element_type: :integer) { |s| s.length }
end

#map_in(&block) ⇒ Object



95
96
97
98
99
# File 'lib/pacer/core/array_route.rb', line 95

def map_in(&block)
  map element_type: element_type do |e|
    e.map(&block)
  end
end

#pairs(head = 0, tail = -1)) ⇒ Object



83
84
85
86
87
# File 'lib/pacer/core/array_route.rb', line 83

def pairs(head = 0, tail = -1)
  map element_type: element_type, route_name: "pairs[#{ head },#{ tail }]" do |a|
    [a[head], a[tail]]
  end
end

#reduce_in(initial, &block) ⇒ Object



101
102
103
# File 'lib/pacer/core/array_route.rb', line 101

def reduce_in(initial, &block)
  map { |e| e.reduce(initial, &block) }
end

#reject_case(*cases) ⇒ Object



111
112
113
114
115
# File 'lib/pacer/core/array_route.rb', line 111

def reject_case(*cases)
  map element_type: element_type do |e|
    e.reject { |x| cases.any? { |c| c === x } }
  end
end

#reject_in(&block) ⇒ Object



150
151
152
153
154
# File 'lib/pacer/core/array_route.rb', line 150

def reject_in(&block)
  map element_type: element_type do |e|
    e.reject(&block)
  end
end

#select_case(*cases) ⇒ Object



105
106
107
108
109
# File 'lib/pacer/core/array_route.rb', line 105

def select_case(*cases)
  map element_type: element_type do |e|
    e.select { |x| cases.any? { |c| c === x } }
  end
end

#select_in(&block) ⇒ Object



144
145
146
147
148
# File 'lib/pacer/core/array_route.rb', line 144

def select_in(&block)
  map element_type: element_type do |e|
    e.select(&block)
  end
end

#tails(et = nil) ⇒ Object



77
78
79
80
81
# File 'lib/pacer/core/array_route.rb', line 77

def tails(et = nil)
  map element_type: et, route_name: 'tails' do |a|
    a.last
  end
end

#transposeObject

This could be done more efficiently by reimplementing transpose… Right now it needs 2n memory.



55
56
57
58
59
# File 'lib/pacer/core/array_route.rb', line 55

def transpose
  gather { [] }.
    map(element_type: :array) { |a| a.transpose }.
    scatter(element_type: :array)
end

#vertices(*exts) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/pacer/core/array_route.rb', line 117

def vertices(*exts)
  r = select_case Pacer::Vertex
  if exts.any?
    r.map_in { |e| e.add_extensions exts }
  else
    r
  end
end