totally_lazy 0.1.28 → 0.1.29

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
- MWZkMGRlODJjYzAwMDBmMmFiMTE3NDk0N2QzNTA4NTJkMTgxZTNkMw==
4
+ NTY1MTJjY2YzODk1NWFlZjI0Y2VjYzYyNzEyZWI0NTFlMjU5ZmI2NQ==
5
5
  data.tar.gz: !binary |-
6
- OWM4YjJjMmIxYmUxZjE1N2RiYTI1Mjg0ZThhZmU4MzhlNGM2MWYxOQ==
6
+ NGE3YmJlNTg4NGRiY2I3YmFkYmUxODU3N2U2ODBiNzc3ZTlhN2Q2YQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- Y2FiN2M4YzIzN2M5OGIzNWFjNmFiM2UyYmYxYTE0MmI4OTBiNDZiZjE3MTdj
10
- ZTY0YTkwYTYzM2FiY2FlNWFkMzlhNzhhMGRjZmQzYmE1OWE4NjE0M2UzN2I2
11
- ZDBiNTE4NzFlMTY5OWExMjE5YTNlZGVjNzMxZjNkYzZiOWExYjY=
9
+ Njg1ZTY4MjM5ZTcwNzc0YTI4NzMzZDNkYWQ5MjY2NmJiMDZiZTRiYjZjOGUy
10
+ ZjQzMGM2YWY4NDZmN2ZhNWJmMGI2YzU2OGNhMTc4Njg1MTFmZDFjYjFmYWU0
11
+ NjEyMWVjMmI3NTRmNDliMzgxMTI4NmQyOTg1YzcyYjY0NDNjNTc=
12
12
  data.tar.gz: !binary |-
13
- OTY2MDA1ZjI5MjAzOGIwOGI2NDJkZTQxNmZiZDk2MzJkODNlZTRjOWI1NWY5
14
- MjU0YTAzZTYyMDdmYTdlYzY2ZDgzMDdiMjcwNTU1NzFjN2FiNzQ5NDUwMjk4
15
- Yzk1MTJlNzk4MTgyYzg3OWFmODcyMTU5YTkwZTNjYmE0MTk2Yjk=
13
+ NTU2OTllZjkzZmRhZmI5NDZkNjg3ZmY5NWExYWVlMzk3MDJlNTdlODZlZDFh
14
+ M2Y5MGZmYTRjZGY4OGFkMDg2ODFlNGY5MWE0ZmExZDljMDQ1NGYyZjBhYTgy
15
+ MGQyOGFmNGRiNGJlYWZmMjZiMGY5MzkyOWNmOWI2ZWJhNzhlODY=
@@ -1,5 +1,17 @@
1
1
  require_relative 'lambda_block'
2
2
 
3
+ class Proc
4
+ def or_exception
5
+ -> (value) {
6
+ begin
7
+ right(self.(value))
8
+ rescue Exception => e
9
+ left(e)
10
+ end
11
+ }
12
+ end
13
+ end
14
+
3
15
  module Eithers
4
16
  private
5
17
  def left(value)
@@ -54,6 +66,10 @@ class Left < Either
54
66
  left(block_given? ? block.call(@value) : fn.(@value))
55
67
  end
56
68
 
69
+ def flat_map(fn=nil, &block) # a function which returns an either
70
+ assert_funcs(fn, block_given?)
71
+ self
72
+ end
57
73
  def <=>(other)
58
74
  @value <=> other.left_value
59
75
  end
@@ -94,6 +110,11 @@ class Right < Either
94
110
  self
95
111
  end
96
112
 
113
+ def flat_map(fn=nil, &block) # a function which returns an either
114
+ assert_funcs(fn, block_given?)
115
+ block_given? ? block.call(@value) : fn.(@value)
116
+ end
117
+
97
118
  def <=>(other)
98
119
  @value <=> other.right_value
99
120
  end
@@ -1,3 +1,5 @@
1
+ # encoding: UTF-8
2
+
1
3
  require_relative '../spec_helper'
2
4
 
3
5
  describe 'Either' do
@@ -21,9 +23,24 @@ describe 'Either' do
21
23
  expect(left(3).map_left { |a| a+2 }).to eq(left(5))
22
24
  end
23
25
 
26
+ it 'should support flat_map' do
27
+ expect(right(4).flat_map(divide(2).or_exception)).to eq(right(2))
28
+ expect(right(4).flat_map { right(2) }).to eq(right(2))
29
+
30
+ result = right(4).flat_map(divide(0).or_exception)
31
+ expect(result.is_left?).to eq(true)
32
+ expect(result.left_value.class).to eq(ZeroDivisionError)
33
+
34
+ expect(left(2).flat_map(divide(2).or_exception)).to eq(left(2))
35
+ expect(left(2).flat_map { right(5) }).to eq(left(2))
36
+ end
37
+
24
38
  it 'should raise exception if you try to use both lambda and block' do
25
39
  expect { right(1).map(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
26
40
  expect { right(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
27
41
  expect { left(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
42
+ expect { right(10).flat_map(divide(2)) { |a| a/2 } }.to raise_error(RuntimeError)
43
+ expect { left(10).flat_map(divide(2)) { |a| a/2 } }.to raise_error(RuntimeError)
28
44
  end
45
+
29
46
  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.28
4
+ version: 0.1.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raymond Barlow