totally_lazy 0.1.58 → 0.1.59
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/totally_lazy/enumerators.rb +0 -11
- data/lib/totally_lazy/numbers.rb +1 -1
- data/lib/totally_lazy/sequence.rb +11 -0
- data/readme.md +20 -2
- data/spec/totally_lazy/sequence_spec.rb +4 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODg1OGNlYmMyZjM5YmUxYjJlNWIxNTM5MjdmN2Y1N2FhNThlNzA5Mw==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDhlNTQwNzFiMGQzZDQ4MTYyNzk1MDU4ZDM4OTE3ODUwYTQ3ODUzMA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
Y2ZhM2Y5NWVhZGI4MGE2ZjNkODc1YzNiZTQ2NmU3MmYwNTI0ZTA1YzVhZDg3
|
10
|
+
YjMwNmIzMTc4NWY2OGM0OGU5YmUxZTgzMGUzYTE2MDA1OGQ3NTRmYzZhMjYx
|
11
|
+
NTIxMzgyMWViY2E5MmU4YTI5ZDI1Yjg2MGVkYWE1OWJmMThhMzY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MjI2MjA3OWI0ZjBhNGQyYzI3MWIxMGQ3Y2VhZDk4MzMwNjAxNmMxMWM5NDhl
|
14
|
+
MjZkNDUyNjhkZjcwZDI0OGMzNzc4YTA4NjZjNGU5ODA3NWFjNDM1ZjNjNDc4
|
15
|
+
MmNjNGI4N2VmNTFhYWU1MTVjM2RiOTViNTU2MDU2YjZmNzg3NzM=
|
@@ -72,17 +72,6 @@ module Enumerators
|
|
72
72
|
end.lazy
|
73
73
|
end
|
74
74
|
|
75
|
-
def enumerate(fn, start)
|
76
|
-
Enumerator.new do |y|
|
77
|
-
current = start
|
78
|
-
loop do
|
79
|
-
result = current
|
80
|
-
current = fn.(current)
|
81
|
-
y << result
|
82
|
-
end
|
83
|
-
end.lazy
|
84
|
-
end
|
85
|
-
|
86
75
|
def cycle_enumerator(e)
|
87
76
|
Enumerator.new do |y|
|
88
77
|
loop do
|
data/lib/totally_lazy/numbers.rb
CHANGED
@@ -42,6 +42,17 @@ module Sequences
|
|
42
42
|
Sequence.new(repeat_fn_enumerator(item))
|
43
43
|
end
|
44
44
|
|
45
|
+
def enumerate(fn, start)
|
46
|
+
Sequence.new(Enumerator.new do |y|
|
47
|
+
current = start
|
48
|
+
loop do
|
49
|
+
result = current
|
50
|
+
current = fn.(current)
|
51
|
+
y << result
|
52
|
+
end
|
53
|
+
end.lazy)
|
54
|
+
end
|
55
|
+
|
45
56
|
private
|
46
57
|
|
47
58
|
def pair_enumerator(left, right)
|
data/readme.md
CHANGED
@@ -20,13 +20,13 @@ This gem requires ruby >= 2.0.0
|
|
20
20
|
In your bundler Gemfile
|
21
21
|
|
22
22
|
```ruby
|
23
|
-
|
23
|
+
gem totally_lazy, '~>0.1.54' # (or latest)
|
24
24
|
```
|
25
25
|
|
26
26
|
Or with rubygems
|
27
27
|
|
28
28
|
```
|
29
|
-
|
29
|
+
gem install totally_lazy
|
30
30
|
```
|
31
31
|
|
32
32
|
### Examples
|
@@ -56,3 +56,21 @@ sequence(pair(1, 2), pair(3, 4)).filter(where(first, equal_to?(3)))
|
|
56
56
|
# lazily returns pair(3,4)
|
57
57
|
sequence(1, 2, 3).to_s # eagerly returns "[1,2,3]"
|
58
58
|
```
|
59
|
+
|
60
|
+
### Generators
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
range(1, 4) # lazily returns 1,2,3,4
|
64
|
+
repeat("car") # lazily returns an infinite sequence of "car"s
|
65
|
+
enumerate(increment, 1) # lazily returns 1,2,3 ... to infinity
|
66
|
+
range(1, 4).cycle() # lazily returns 1,2,3,4,1,2,3,4,1,2,3,4 infinitely
|
67
|
+
powers_of(3) # lazily returns the powers of 3 (i.e 1,3,9,27 ...)
|
68
|
+
```
|
69
|
+
|
70
|
+
Naturally you can combine these operations together ...
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
enumerate(increment, 1).filter(even).take(10).reduce(sum) # returns 110
|
74
|
+
```
|
75
|
+
|
76
|
+
And because all the operations except reduce are lazy the sequence of numbers is only processed once.
|
@@ -305,6 +305,10 @@ describe 'Sequence' do
|
|
305
305
|
expect(sequence(1, 2, 3).join(sequence(4, 5, 6))).to eq(sequence(1, 2, 3, 4, 5, 6))
|
306
306
|
end
|
307
307
|
|
308
|
+
it 'should support cycle' do
|
309
|
+
expect(range(1, 3).cycle.take(10)).to eq(sequence(1, 2, 3, 1, 2, 3, 1, 2, 3, 1))
|
310
|
+
end
|
311
|
+
|
308
312
|
it 'should raise exception if you try to use both lambda and block' do
|
309
313
|
expect { empty.map(->(a) { a+1 }) { |b| b+2 } }.to raise_error(RuntimeError)
|
310
314
|
expect { empty.map_concurrently(->(a) { a+1 }) { |b| b+2 } }.to raise_error(RuntimeError)
|
@@ -326,8 +330,4 @@ describe 'Sequence' do
|
|
326
330
|
expect { empty.group_by(->(_) { true }) { |_| true } }.to raise_error(RuntimeError)
|
327
331
|
expect { empty.each(->(v) { puts(v) }) { |v| puts(v) } }.to raise_error(RuntimeError)
|
328
332
|
end
|
329
|
-
|
330
|
-
it 'should support cycle' do
|
331
|
-
expect(range(1, 3).cycle.take(10)).to eq(sequence(1, 2, 3, 1, 2, 3, 1, 2, 3, 1))
|
332
|
-
end
|
333
333
|
end
|