totally_lazy 0.1.16 → 0.1.17

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
- NjgzMzY3YjI3MTk4OTc5YTM0YWM5YTcwMDA3ZGJhZTdmMWY3MzM1MA==
4
+ ZGUyNzc3NTNjMDYzY2UyOWU4NjFjNzhmZDZlZTJlZDM0NDJkMmU0NA==
5
5
  data.tar.gz: !binary |-
6
- OGJjNzdkZDNmZGY2NGNhMGI5ZGM2YjNmMDExMThiOGMzYTcxMDhlNg==
6
+ YzI3YjVkZGU4YmNiMmM0Y2QxZjVlNTU2OTBkMGQyNjVhOGZmOTc1NA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTk5MWJhZDA3MGRkNThhZmJmZWU2ZDEwNWE2NzgzYzE0NjUyYzI4ZTYyOTA4
10
- OTAyNTVjMWYyOTI4NWRhYjM1YjkxY2U1YjFhMWVjMDIxZTRmZmY0MDNkNGY0
11
- YTBhNThkYTM2MzY3MDczNWQzMzk2OTYzYTc5MWE2YTBkMDI0ZTA=
9
+ ZTUxOGRjMDVlNDM2NThhOWJhNDBiZjJlNzI1YzkxZjA4NGI4NWQ3ZTM1YmM1
10
+ YjgxYmUwNmU5ZTQ2ZTQ2NmEwYWY3ODJjYzVkNDExYmUyNzllODM1MjVkY2I4
11
+ NThkN2EyMWEyMDIwODRhNDhlYjBhODI2MjY2MTU2MWY1Zjk3M2U=
12
12
  data.tar.gz: !binary |-
13
- NDdkZDE1NGJiYjY3MTg0MzU4ZjY2N2UxYWZkYTg0MTM0YmEyZGRjNWE1ZWJj
14
- NDU0NDk5ZGZlNThmOTcxOTk1MDlmOGYwZDMzODhkZTIxYWI4OGY4NWY2NDRi
15
- Y2U4ZmJjMjdkMzE2MjM1MTc5MTY0MmRkMGViMzJhZDA0NDE4NTc=
13
+ NTMxYzE0YTQzNzhhM2M4ZGQ5OTk0MGEyZWNmOThlNjI0YThhZDRkMDQyMGZk
14
+ NDU2NWY1YmFhYWIzMTQ0YTk0YjEzYTQ4OTZlYzBjYzBjNTczNmNhNWU1NmM2
15
+ NTYyZWMyYzhlNDI0NTFkNzU2M2FkNDkwODU1ZDlkMTkyMGZmNGE=
data/lib/functions.rb CHANGED
@@ -1,6 +1,18 @@
1
1
  require 'concurrent/executors'
2
2
  require 'concurrent/promise'
3
3
 
4
+ class Proc
5
+ def to_optional
6
+ ->(value) {
7
+ begin
8
+ option(self.(value))
9
+ rescue
10
+ none
11
+ end
12
+ }
13
+ end
14
+ end
15
+
4
16
  module Functions
5
17
  def monoid(fn, id)
6
18
  fn.define_singleton_method(:identity) do
data/lib/numbers.rb CHANGED
@@ -15,6 +15,10 @@ module Numbers
15
15
  remainder_is(2, 1)
16
16
  end
17
17
 
18
+ def divide(divisor)
19
+ ->(dividend) { dividend / divisor }
20
+ end
21
+
18
22
  def remainder_is(divisor, remainder)
19
23
  ->(dividend) { remainder(dividend, divisor) == remainder }
20
24
  end
data/lib/option.rb CHANGED
@@ -48,6 +48,11 @@ module Option
48
48
  some(block_given? ? block.call(@value) : fn.(@value))
49
49
  end
50
50
 
51
+ def flat_map(fn=nil, &block) # function should return an option
52
+ assert_funcs(fn, block_given?)
53
+ block_given? ? block.call(@value) : fn.(@value)
54
+ end
55
+
51
56
  def fold(seed, fn=nil, &block)
52
57
  assert_funcs(fn, block_given?)
53
58
  block_given? ? block.call(seed, @value) : fn.(seed, @value)
@@ -94,6 +99,11 @@ module Option
94
99
  none
95
100
  end
96
101
 
102
+ def flat_map(fn=nil, &block) # function should return an option
103
+ assert_funcs(fn, block_given?)
104
+ none
105
+ end
106
+
97
107
  def fold(seed, fn=nil &block)
98
108
  assert_funcs(fn, block_given?)
99
109
  seed
data/spec/option_spec.rb CHANGED
@@ -44,6 +44,14 @@ describe 'Option' do
44
44
  expect(some(2).map(ignore_and_return(nil))).to eq(some(nil)) # differs from Java TL which reutrns none
45
45
  end
46
46
 
47
+ it 'should support flat_map' do
48
+ expect(some(4).flat_map(divide(2).to_optional)).to eq(some(2))
49
+ expect(some(4).flat_map{ |v| divide(2).to_optional.(v) }).to eq(some(2))
50
+ expect(some(4).flat_map(divide(0).to_optional)).to eq(none)
51
+ expect(none.flat_map(constant(none))).to eq(none)
52
+ expect(none.flat_map(some(4))).to eq(none)
53
+ end
54
+
47
55
  it 'should raise exception if you try to use both lambda and block' do
48
56
  expect { some(1).exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
49
57
  expect { none.exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
@@ -55,6 +63,7 @@ describe 'Option' do
55
63
  expect { none.fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
56
64
  expect { some(1).map(->(v) { v.to_s }) { |v| v.to_s } }.to raise_error(RuntimeError)
57
65
  expect { none.map(->(v) { v.to_s }) { |v| v.to_s } }.to raise_error(RuntimeError)
66
+ expect { some(4).flat_map(divide(2).to_optional){ |v| divide(2).to_optional.(v) } }.to raise_error(RuntimeError)
58
67
  end
59
68
 
60
69
  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.16
4
+ version: 0.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raymond Barlow