totally_lazy 0.1.15 → 0.1.16

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
- M2FmZDUwYjhlMzkyMjQ5ZjQ0ZTc4NGVlZWI3MzhjMWE5MjMxNjRhYw==
4
+ NjgzMzY3YjI3MTk4OTc5YTM0YWM5YTcwMDA3ZGJhZTdmMWY3MzM1MA==
5
5
  data.tar.gz: !binary |-
6
- NTJkODZiODkxZDQ3YWY5MTExMmRlODM4NGIyYzZkZTIyMmFmOGYwZg==
6
+ OGJjNzdkZDNmZGY2NGNhMGI5ZGM2YjNmMDExMThiOGMzYTcxMDhlNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTk0ZDNlMjFkYWQzYzhmZWVjMjhmOGZkMTNlYTdkM2M0Y2M0NGQzNWEyMDQ1
10
- ZDc2OTI2M2ExMDM3MzZiMzk2ZjU0NGMwOTAzNjgxMmIyNzYzMjdlMDRhMTVi
11
- ODc1ZjUxNGI4NWNmN2M2Mzk4NzVmZjEyNzQxN2ZmNGFiNjQxOGU=
9
+ YTk5MWJhZDA3MGRkNThhZmJmZWU2ZDEwNWE2NzgzYzE0NjUyYzI4ZTYyOTA4
10
+ OTAyNTVjMWYyOTI4NWRhYjM1YjkxY2U1YjFhMWVjMDIxZTRmZmY0MDNkNGY0
11
+ YTBhNThkYTM2MzY3MDczNWQzMzk2OTYzYTc5MWE2YTBkMDI0ZTA=
12
12
  data.tar.gz: !binary |-
13
- ZGMyMzg4NTg0YWIyMDZjMjE2MGY3NjliNjg0OWRiNTFmMjMzNGFkZjJkNzll
14
- YWNlZDhiZTQ4ODg4MDg5ZmE2NGZkODczNWE0NjFkM2JlMTBiMjUyMDdiNTQ2
15
- YWJhMDY0YmVkNDcwM2I0MDhmMzJjMTUyODZkODhmYTM4ODg5NTU=
13
+ NDdkZDE1NGJiYjY3MTg0MzU4ZjY2N2UxYWZkYTg0MTM0YmEyZGRjNWE1ZWJj
14
+ NDU0NDk5ZGZlNThmOTcxOTk1MDlmOGYwZDMzODhkZTIxYWI4OGY4NWY2NDRi
15
+ Y2U4ZmJjMjdkMzE2MjM1MTc5MTY0MmRkMGViMzJhZDA0NDE4NTc=
data/lib/functions.rb CHANGED
@@ -13,6 +13,18 @@ module Functions
13
13
  -> { value }
14
14
  end
15
15
 
16
+ def ignore_and_return(value)
17
+ returns1(value)
18
+ end
19
+
20
+ def returns1(value)
21
+ constant(value)
22
+ end
23
+
24
+ def constant(value)
25
+ ->(_) { value }
26
+ end
27
+
16
28
  def identity
17
29
  -> (a) { a }
18
30
  end
data/lib/option.rb CHANGED
@@ -40,11 +40,12 @@ module Option
40
40
 
41
41
  def exists?(fn_pred=nil, &block_pred)
42
42
  assert_funcs(fn_pred, block_given?)
43
- block_given? ? block_pred.call(@value) : fn_pred.(value)
43
+ block_given? ? block_pred.call(@value) : fn_pred.(@value)
44
44
  end
45
45
 
46
- def map(fn)
47
- some(fn.(value))
46
+ def map(fn=nil, &block)
47
+ assert_funcs(fn, block_given?)
48
+ some(block_given? ? block.call(@value) : fn.(@value))
48
49
  end
49
50
 
50
51
  def fold(seed, fn=nil, &block)
@@ -88,7 +89,8 @@ module Option
88
89
  false
89
90
  end
90
91
 
91
- def map(fn)
92
+ def map(fn=nil, &block)
93
+ assert_funcs(fn, block_given?)
92
94
  none
93
95
  end
94
96
 
data/spec/option_spec.rb CHANGED
@@ -36,6 +36,14 @@ describe 'Option' do
36
36
  expect(none.fold(1, add)).to eq(1)
37
37
  end
38
38
 
39
+ it 'should support map' do
40
+ expect(option(1).map(to_string)).to eq(option('1'))
41
+ expect(option(1).map { |value| value.to_s }).to eq(option('1'))
42
+ expect(some(2).map(to_string)).to eq(some('2'))
43
+ expect(none.map(to_string)).to eq(none)
44
+ expect(some(2).map(ignore_and_return(nil))).to eq(some(nil)) # differs from Java TL which reutrns none
45
+ end
46
+
39
47
  it 'should raise exception if you try to use both lambda and block' do
40
48
  expect { some(1).exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
41
49
  expect { none.exists?(->(a) { a == 1 }) { |b| b == 2 } }.to raise_error(RuntimeError)
@@ -45,6 +53,8 @@ describe 'Option' do
45
53
  expect { none.fold(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
46
54
  expect { some(1).fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
47
55
  expect { none.fold_left(0, ->(a, b) { a+b }) { |a, b| a+b } }.to raise_error(RuntimeError)
56
+ expect { some(1).map(->(v) { v.to_s }) { |v| v.to_s } }.to raise_error(RuntimeError)
57
+ expect { none.map(->(v) { v.to_s }) { |v| v.to_s } }.to raise_error(RuntimeError)
48
58
  end
49
59
 
50
60
  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.15
4
+ version: 0.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raymond Barlow