Module: Rupture::Sequence

Included in:
NilClass, Seq
Defined in:
lib/rupture/sequence.rb

Instance Method Summary collapse

Instance Method Details

#concat(*colls) ⇒ Object



171
172
173
# File 'lib/rupture/sequence.rb', line 171

def concat(*colls)
  F.concat(self, *colls)
end

#conj(item) ⇒ Object



51
52
53
# File 'lib/rupture/sequence.rb', line 51

def conj(item)
  F.cons(item, self)
end

#countObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rupture/sequence.rb', line 39

def count
  F.loop(0, self) do |recur, i, s|
    if s.empty?
      i
    elsif s.respond_to?(:size)
      s.size + i
    else
      recur[i.inc, s.rest]
    end
  end
end

#divideObject



27
28
29
# File 'lib/rupture/sequence.rb', line 27

def divide
  [first, rest]
end

#doall(n = nil) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rupture/sequence.rb', line 303

def doall(n = nil)
  if n
    loop(n, seq) do |recur, n, s|
      recur[n.dec, s.next] if s and n.pos?
    end
  else
    loop(seq) do |recur, s|
      recur[s.next] if s
    end
  end
end

#drop(n) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rupture/sequence.rb', line 71

def drop(n)
  F.lazy_seq do
    F.loop(n, seq) do |recur, n, s|
      if s and n.pos?
        recur[n.dec, s.next]
      else
        s
      end
    end
  end
end

#drop_last(n) ⇒ Object



93
94
95
# File 'lib/rupture/sequence.rb', line 93

def drop_last(n)
  F.map(self, drop(n)) {|x,_| x}
end

#drop_while(p = nil, &pred) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/rupture/sequence.rb', line 115

def drop_while(p = nil, &pred)
  pred ||= p
  F.lazy_seq do
    F.loop(seq) do |recur, s|
      if s and pred[s.first]
        recur[s.next]
      else
        s
      end
    end
  end
end

#eachObject



31
32
33
34
35
36
37
# File 'lib/rupture/sequence.rb', line 31

def each
  s = self
  while s = s.seq
    yield s.first
    s = s.rest
  end
end

#empty?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rupture/sequence.rb', line 19

def empty?
  not seq
end

#every?(p = nil, &pred) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
226
# File 'lib/rupture/sequence.rb', line 223

def every?(p = nil, &pred)
  pred ||= p || F[:identity]
  all?(&pred)
end

#filter(p = nil, &pred) ⇒ Object



133
134
135
136
# File 'lib/rupture/sequence.rb', line 133

def filter(p = nil, &pred)
  pred ||= p
  F.filter(pred, self)
end

#firstObject



3
4
5
# File 'lib/rupture/sequence.rb', line 3

def first
  seq.first
end

#flattenObject



152
153
154
155
# File 'lib/rupture/sequence.rb', line 152

def flatten
  sequential = lambda {|x| x.class <= Seq or x.class == Array}
  tree_seq(sequential, :seq).remove(sequential)
end

#foldr(*args, &fn) ⇒ Object



206
207
208
209
# File 'lib/rupture/sequence.rb', line 206

def foldr(*args, &fn)
  fn ||= args.shift
  reverse.reduce(*args) {|a,b| fn[b,a]}
end

#frequenciesObject



297
298
299
300
301
# File 'lib/rupture/sequence.rb', line 297

def frequencies
  reduce(Hash.new(0)) do |counts, x|
    counts.update!(x, :inc)
  end
end

#into(coll) ⇒ Object



55
56
57
# File 'lib/rupture/sequence.rb', line 55

def into(coll)
  coll.seq.reduce(:conj, self)
end

#lastObject



97
98
99
# File 'lib/rupture/sequence.rb', line 97

def last
  take_last(1).first
end

#map(f = nil, &fn) ⇒ Object



157
158
159
160
161
162
# File 'lib/rupture/sequence.rb', line 157

def map(f = nil, &fn)
  fn ||= f
  F.lazy_loop(seq) do |recur, s|
    F.cons(fn[s.first], recur[s.next]) if s
  end
end

#map_indexed(f = nil, &fn) ⇒ Object



164
165
166
167
168
169
# File 'lib/rupture/sequence.rb', line 164

def map_indexed(f = nil, &fn)
  fn ||= f
  F.lazy_loop(0, seq) do |recur, i, s|
    F.cons(fn[i, s.first], recur[i.inc, s.next]) if s
  end
end

#mapcat(f = nil, &fn) ⇒ Object



175
176
177
178
# File 'lib/rupture/sequence.rb', line 175

def mapcat(f = nil, &fn)
  fn ||= f
  F.mapcat(fn, self)
end

#nextObject



11
12
13
# File 'lib/rupture/sequence.rb', line 11

def next
  rest.seq
end

#not_emptyObject



23
24
25
# File 'lib/rupture/sequence.rb', line 23

def not_empty
  self if seq
end

#nth(n) ⇒ Object



241
242
243
# File 'lib/rupture/sequence.rb', line 241

def nth(n)
  drop(n.dec).first
end

#partition(n, step = n, pad = nil) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rupture/sequence.rb', line 245

def partition(n, step = n, pad = nil)
  F.lazy_seq do
    if s = seq
      p = take(n)
      if n == p.count
        F.cons(p, drop(step).partition(n, step, pad))
      elsif pad
        F.cons(p.concat(pad).take(n), nil)
      else
        nil
      end
    end
  end
end

#partition_all(n, step = n) ⇒ Object



260
261
262
# File 'lib/rupture/sequence.rb', line 260

def partition_all(n, step = n)
  partition(n, step, [])
end

#partition_between(f = nil, &fn) ⇒ Object



276
277
278
279
280
281
282
283
284
285
# File 'lib/rupture/sequence.rb', line 276

def partition_between(f = nil, &fn)
  fn ||= f
  F.lazy_seq do
    if s = seq
      run = F.cons(s.first,
                   s.partition(2,1).take_while {|i| not fn[*i]}.map(:second))
      F.cons(run, s.drop(run.count).partition_between(fn))
    end
  end
end

#partition_by(f = nil, &fn) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/rupture/sequence.rb', line 264

def partition_by(f = nil, &fn)
  fn ||= f
  F.lazy_seq do
    if s = seq
      head = s.first
      val  = fn[head]
      run = F.cons(head, s.rest.take_while {|i| val == fn[i]})
      F.cons(run, s.drop(run.count).partition_by(fn))
    end
  end
end

#reduce(*args, &fn) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rupture/sequence.rb', line 180

def reduce(*args, &fn)
  fn ||= args.shift
  Utils.verify_args(args, 0, 1)

  if s = seq
    inject(*args, &fn)
  elsif args.size == 1
    args.first
  else
    fn[] if fn.arity == -1
  end
end

#reductions(*args, &fn) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/rupture/sequence.rb', line 193

def reductions(*args, &fn)
  fn ||= args.shift
  Utils.verify_args(args, 0, 1)
  acc, coll = (args.empty? ? divide : [args.first, self])

  F.lazy_loop(acc, coll) do |recur, acc, coll|
    if coll.seq
      acc = fn[acc, coll.first]
      F.cons(acc, recur[acc, coll.rest])
    end
  end.conj(acc)
end

#remove(p = nil, &pred) ⇒ Object



138
139
140
141
# File 'lib/rupture/sequence.rb', line 138

def remove(p = nil, &pred)
  pred ||= p
  F.remove(pred, self)
end

#restObject



7
8
9
# File 'lib/rupture/sequence.rb', line 7

def rest
  seq.rest
end

#reverseObject



59
60
61
# File 'lib/rupture/sequence.rb', line 59

def reverse
  Seq::Empty.into(self)
end

#secondObject



15
16
17
# File 'lib/rupture/sequence.rb', line 15

def second
  rest.first
end

#separate(p = nil, &pred) ⇒ Object



143
144
145
146
# File 'lib/rupture/sequence.rb', line 143

def separate(p = nil, &pred)
  pred ||= p
  [filter(pred), remove(pred)]
end

#sequential?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/rupture/sequence.rb', line 148

def sequential?
  true
end

#some(f = nil, &fn) ⇒ Object



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rupture/sequence.rb', line 228

def some(f = nil, &fn)
  fn ||= f || F[:identity]
  F.loop(seq) do |recur, s|
    if s
      if val = fn[s.first]
        val
      else
        recur[s.next]
      end
    end
  end
end

#sort(c = nil, &cmp) ⇒ Object



287
288
289
290
# File 'lib/rupture/sequence.rb', line 287

def sort(c = nil, &cmp)
  cmp||= c
  super(&cmp).seq
end

#sort_by(f = nil, &fn) ⇒ Object



292
293
294
295
# File 'lib/rupture/sequence.rb', line 292

def sort_by(f = nil, &fn)
  fn ||= f
  sort {|a,b| fn[a] <=> fn[b]}
end

#split_at(n) ⇒ Object



101
102
103
# File 'lib/rupture/sequence.rb', line 101

def split_at(n)
  [take(n), drop(n)]
end

#split_with(p = nil, &pred) ⇒ Object



128
129
130
131
# File 'lib/rupture/sequence.rb', line 128

def split_with(p = nil, &pred)
  pred ||= p
  [take_while(pred), drop_while(pred)]
end

#take(n) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/rupture/sequence.rb', line 63

def take(n)
  F.lazy_seq do
    if n.pos? and s = seq
      F.cons(s.first, s.rest.take(n.dec))
    end
  end
end

#take_last(n) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/rupture/sequence.rb', line 83

def take_last(n)
  F.loop(seq, drop(n).seq) do |recur, s, lead|
    if lead
      recur[s.next, lead.next]
    else
      s || Seq::Empty
    end
  end
end

#take_while(p = nil, &pred) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/rupture/sequence.rb', line 105

def take_while(p = nil, &pred)
  pred ||= p
  F.lazy_seq do
    if s = seq
      i = s.first
      F.cons(i, s.rest.take_while(pred)) if pred[i]
    end
  end
end

#tree_seq(branch, children, &f) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
# File 'lib/rupture/sequence.rb', line 211

def tree_seq(branch, children, &f)
  branch   ||= f
  children ||= f
  walk = lambda do |node|
    F.lazy_seq do
      rest = children[node].mapcat(walk) if branch[node]
      F.cons(node, rest)
    end
  end
  walk[self]
end