Module: Rupture::Function

Extended by:
Function
Included in:
Function
Defined in:
lib/rupture/function.rb

Constant Summary collapse

F =
Function

Instance Method Summary collapse

Instance Method Details

#concat(*colls) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/rupture/function.rb', line 14

def concat(*colls)
  lazy_seq do
    head, *tail = colls.collect(&:seq)
    if head
      cons(head.first, concat(head.rest, *tail))
    elsif tail.any?
      concat(*tail)
    end
  end
end

#cons(head, tail) ⇒ Object



110
111
112
# File 'lib/rupture/function.rb', line 110

def cons(head, tail)
  Cons.new(head, tail)
end

#constantly(x) ⇒ Object



127
128
129
# File 'lib/rupture/function.rb', line 127

def constantly(x)
  lambda {x}
end

#decorate(*args) ⇒ Object



141
142
143
# File 'lib/rupture/function.rb', line 141

def decorate(*args)
  juxt(identity, *args)
end

#filter(pred, coll) ⇒ Object



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

def filter(pred, coll)
  lazy_seq do
    if s = coll.seq
      e = s.first
      tail = filter(pred, s.rest)
      pred[e] ? cons(e, tail) : tail
    end
  end
end

#fix(val, pred, f = nil, &fn) ⇒ Object



149
150
151
152
153
# File 'lib/rupture/function.rb', line 149

def fix(val, pred, f = nil, &fn)
  fn ||= f
  pred = pred.to_proc[val] if pred.respond_to?(:to_proc)
  pred ? fn[val] : val
end

#for(s, *seqs, &fn) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rupture/function.rb', line 159

def for(s, *seqs, &fn)
  if seqs.empty?
    s.seq.map(&fn)
  else
    lazy_seq do
      tails = self.for(*seqs) {|*args| args}
      s.seq.mapcat do |head|
        tails.map do |tail|
          fn[head, *tail]
        end
      end
    end
  end
end

#hash_map(*kvs, &block) ⇒ Object



118
119
120
121
122
123
124
125
# File 'lib/rupture/function.rb', line 118

def hash_map(*kvs, &block)
  h = block_given? ? HashMap.new(&block) : HashMap.empty
  if kvs.size == 1
    h.into(kvs.first)
  else
    h.assoc(*kvs)
  end
end

#identity(x) ⇒ Object



131
132
133
# File 'lib/rupture/function.rb', line 131

def identity(x)
  x
end

#iterate(*args, &fn) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/rupture/function.rb', line 76

def iterate(*args, &fn)
  fn ||= args.shift
  Utils.verify_args(args, 1)
  x = args.first
  lazy_seq do
    cons(x, iterate(fn[x], &fn))
  end
end

#juxt(*fs) ⇒ Object



135
136
137
138
139
# File 'lib/rupture/function.rb', line 135

def juxt(*fs)
  lambda do |*args|
    fs.collect {|f| f[*args]}
  end
end

#lazy_loop(*vals, &block) ⇒ Object



69
70
71
72
73
74
# File 'lib/rupture/function.rb', line 69

def lazy_loop(*vals, &block)
  recur = lambda do |*v|
    lazy_seq {block[recur, *v]}
  end
  recur[*vals]
end

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



105
106
107
108
# File 'lib/rupture/function.rb', line 105

def lazy_seq(f = nil, &fn)
  fn ||= f
  LazySeq.new(fn)
end

#let(*vals) {|vals| ... } ⇒ Object

Yields:

  • (vals)


145
146
147
# File 'lib/rupture/function.rb', line 145

def let(*vals)
  yield(*vals)
end

#list(*xs) ⇒ Object



114
115
116
# File 'lib/rupture/function.rb', line 114

def list(*xs)
  List.new(*xs)
end

#loop(*vals) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rupture/function.rb', line 55

def loop(*vals)
  more  = true
  recur = lambda do |*new_vals|
    vals = new_vals
    more = true
  end

  while more
    more = nil
    result = yield(recur, *vals)
  end
  result
end

#map(*colls, &fn) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/rupture/function.rb', line 3

def map(*colls, &fn)
  fn ||= colls.shift
  lazy_loop(colls.seq.map(:seq)) do |recur, seqs|
    if seqs.every?
      firsts = seqs.map(:first)
      nexts  = seqs.map(:next)
      cons(fn[*firsts], recur[nexts])
    end
  end
end

#mapcat(*colls, &fn) ⇒ Object



25
26
27
28
# File 'lib/rupture/function.rb', line 25

def mapcat(*colls, &fn)
  fn ||= colls.shift
  concat(*map(*colls, &fn))
end

#remove(pred, coll) ⇒ Object



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

def remove(pred, coll)
  filter(pred.complement, coll)
end

#repeat(*args) ⇒ Object



99
100
101
102
103
# File 'lib/rupture/function.rb', line 99

def repeat(*args)
  Utils.verify_args(args, 1, 2)
  x, n = args.reverse
  repeatedly(n) {x}
end

#repeatedly(*args, &fn) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rupture/function.rb', line 85

def repeatedly(*args, &fn)
  fn ||= args.pop
  Utils.verify_args(args, 0, 1)
  n = args.first

  lazy_seq do
    if n.nil?
      cons(fn[], repeatedly(n, fn))
    elsif n > 0
      cons(fn[], repeatedly(n.dec, fn))
    end
  end
end

#when_let(val) {|val| ... } ⇒ Object

Yields:

  • (val)


155
156
157
# File 'lib/rupture/function.rb', line 155

def when_let(val)
  yield(val) if val
end

#zip(*colls) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rupture/function.rb', line 30

def zip(*colls)
  lazy_seq do
    seqs = colls.collect(&:seq)
    if seqs.any?
      firsts = seqs.collect(&:first)
      rests  = seqs.collect(&:rest)
      cons(firsts, zip(*rests))
    end
  end
end