totally_lazy 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjQ2MGUzNjA5MGRjZWI0YzRlMjBmN2MyMjkyYzIyNmQ2OGQxNTIzMg==
4
+ M2FmZDUwYjhlMzkyMjQ5ZjQ0ZTc4NGVlZWI3MzhjMWE5MjMxNjRhYw==
5
5
  data.tar.gz: !binary |-
6
- YWVkMTVlYWFiZThkNWQ2NWZlMmUwMWExNmE4YTJmNTU3NWY0MjhmOA==
6
+ NTJkODZiODkxZDQ3YWY5MTExMmRlODM4NGIyYzZkZTIyMmFmOGYwZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OGY0MjIxNmY1YmVjNDRkNzNkMDkzMTU2NzJhY2RjYmU4MWFjYjlmNmI0MGMx
10
- NTE2MmM4OTRkNzM0NWY3NjRhYjUxYjAwODM0OTYxYzVkNmZhMzMwYmY3ZThk
11
- ZTM5ZjQ4ZmNkNWNkMGIxOTg1ZGE2N2E1ZmI0MjAzZTE2MTYxNTE=
9
+ OTk0ZDNlMjFkYWQzYzhmZWVjMjhmOGZkMTNlYTdkM2M0Y2M0NGQzNWEyMDQ1
10
+ ZDc2OTI2M2ExMDM3MzZiMzk2ZjU0NGMwOTAzNjgxMmIyNzYzMjdlMDRhMTVi
11
+ ODc1ZjUxNGI4NWNmN2M2Mzk4NzVmZjEyNzQxN2ZmNGFiNjQxOGU=
12
12
  data.tar.gz: !binary |-
13
- MGMyZjE0MWI1MDRkOWJkYTY1YjVlOWY3NWQ3ZWZjMmI0NzQyZDljNDIzOGFl
14
- MDI5ZTgwNjlmODlkNGFkZWM0NjI2YmIxZjYxYWU5YjVjOGVkOGQzOWNjZDIw
15
- OWQzNDM1M2Y0YmIxZmRiOGVhYTllMjkwMDVmNzRjOGUzN2IyMGM=
13
+ ZGMyMzg4NTg0YWIyMDZjMjE2MGY3NjliNjg0OWRiNTFmMjMzNGFkZjJkNzll
14
+ YWNlZDhiZTQ4ODg4MDg5ZmE2NGZkODczNWE0NjFkM2JlMTBiMjUyMDdiNTQ2
15
+ YWJhMDY0YmVkNDcwM2I0MDhmMzJjMTUyODZkODhmYTM4ODg5NTU=
data/lib/numbers.rb CHANGED
@@ -3,6 +3,10 @@ module Numbers
3
3
  monoid(->(a, b) { a + b }, 0)
4
4
  end
5
5
 
6
+ def add
7
+ sum
8
+ end
9
+
6
10
  def even
7
11
  remainder_is(2, 0)
8
12
  end
data/lib/option.rb CHANGED
@@ -47,6 +47,17 @@ module Option
47
47
  some(fn.(value))
48
48
  end
49
49
 
50
+ def fold(seed, fn=nil, &block)
51
+ assert_funcs(fn, block_given?)
52
+ block_given? ? block.call(seed, @value) : fn.(seed, @value)
53
+ end
54
+
55
+ alias fold_left fold
56
+
57
+ def is_empty?
58
+ false
59
+ end
60
+
50
61
  def enumerator
51
62
  Enumerator.new { |y|
52
63
  y << @value
@@ -54,10 +65,6 @@ module Option
54
65
  }
55
66
  end
56
67
 
57
- def is_empty?
58
- false
59
- end
60
-
61
68
  def <=>(other)
62
69
  @value <=> other.value
63
70
  end
@@ -77,6 +84,7 @@ module Option
77
84
  end
78
85
 
79
86
  def exists?(fn_pred=nil, &block_pred)
87
+ assert_funcs(fn_pred, block_given?)
80
88
  false
81
89
  end
82
90
 
@@ -84,6 +92,13 @@ module Option
84
92
  none
85
93
  end
86
94
 
95
+ def fold(seed, fn=nil &block)
96
+ assert_funcs(fn, block_given?)
97
+ seed
98
+ end
99
+
100
+ alias fold_left fold
101
+
87
102
  def enumerator
88
103
  Enumerator.new { |y|
89
104
  raise StopIteration.new
data/spec/option_spec.rb CHANGED
@@ -28,8 +28,23 @@ describe 'Option' do
28
28
  expect(none.is?(greater_than(0))).to eq(false)
29
29
  end
30
30
 
31
+ it 'should support fold (aka fold_left)' do
32
+ expect(option(1).fold(1, add)).to eq(2)
33
+ expect(option(1).fold_left(1, add)).to eq(2)
34
+ expect(option(1).fold(1) { |a, b| a + b }).to eq(2)
35
+ expect(some(1).fold(1, add)).to eq(2)
36
+ expect(none.fold(1, add)).to eq(1)
37
+ end
38
+
31
39
  it 'should raise exception if you try to use both lambda and block' do
32
- expect { empty.exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
33
- expect { empty.is?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
40
+ expect { some(1).exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
41
+ expect { none.exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
42
+ expect { some(1).is?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
43
+ expect { none.is?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
44
+ expect { some(1).fold(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
45
+ expect { none.fold(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
46
+ expect { some(1).fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
47
+ expect { none.fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
34
48
  end
49
+
35
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: totally_lazy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raymond Barlow