totally_lazy 0.1.26 → 0.1.28

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
- MjAwMDFhNmJiNmFlNTZmMDA3ODg5ZTUxNGNjMDZhNzA1MmFkYTRmOA==
4
+ MWZkMGRlODJjYzAwMDBmMmFiMTE3NDk0N2QzNTA4NTJkMTgxZTNkMw==
5
5
  data.tar.gz: !binary |-
6
- ZDgwNjk1NTU3NDk5ZDk1OThlMTVlYWRhYmY2ZGUzNDQxOTIzNDBmNw==
6
+ OWM4YjJjMmIxYmUxZjE1N2RiYTI1Mjg0ZThhZmU4MzhlNGM2MWYxOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZTExNjEzN2FhOWJjN2MzMDI2N2RmOTZlZmZlYzUxY2U4Mzc1NGZiMmZjYzJh
10
- ZjViMjdlYzQzMmRkNDdkZjhlNDAxYjYwZDczZDRlOTA0MTY4NWYzYjlmMWNj
11
- ZjJiODM4MDY1OWZkNmJhNjA5NTc5NDZkOGVmNTFmYTkzZGQwNmQ=
9
+ Y2FiN2M4YzIzN2M5OGIzNWFjNmFiM2UyYmYxYTE0MmI4OTBiNDZiZjE3MTdj
10
+ ZTY0YTkwYTYzM2FiY2FlNWFkMzlhNzhhMGRjZmQzYmE1OWE4NjE0M2UzN2I2
11
+ ZDBiNTE4NzFlMTY5OWExMjE5YTNlZGVjNzMxZjNkYzZiOWExYjY=
12
12
  data.tar.gz: !binary |-
13
- YWIxNzc0N2EwYTRkNDA1Y2RmM2Q0MGFkMTRlZjk4Njk5ODQ5YWQxMWM2OWNj
14
- ZGM4ZTRiYWYwN2JlZGQ4MDNjZTQ1OGYyMDlhZWIyYTVjZmQxNzgyN2ViZDgw
15
- ZjExODFmYzkwYjk3YTZlMGZjNWFjYmI0ODljMjhiMjJkY2JmMmI=
13
+ OTY2MDA1ZjI5MjAzOGIwOGI2NDJkZTQxNmZiZDk2MzJkODNlZTRjOWI1NWY5
14
+ MjU0YTAzZTYyMDdmYTdlYzY2ZDgzMDdiMjcwNTU1NzFjN2FiNzQ5NDUwMjk4
15
+ Yzk1MTJlNzk4MTgyYzg3OWFmODcyMTU5YTkwZTNjYmE0MTk2Yjk=
@@ -1,3 +1,5 @@
1
+ require_relative 'lambda_block'
2
+
1
3
  module Eithers
2
4
  private
3
5
  def left(value)
@@ -11,6 +13,7 @@ end
11
13
 
12
14
  class Either
13
15
  include Comparable
16
+ include LambdaBlock
14
17
 
15
18
  def self.left(value)
16
19
  Left.new(value)
@@ -19,7 +22,6 @@ class Either
19
22
  def self.right(value)
20
23
  Right.new(value)
21
24
  end
22
-
23
25
  end
24
26
 
25
27
  class Left < Either
@@ -35,16 +37,25 @@ class Left < Either
35
37
  false
36
38
  end
37
39
 
38
- def left
40
+ def left_value
39
41
  @value
40
42
  end
41
43
 
42
- def right
44
+ def right_value
43
45
  raise NoSuchElementException.new
44
46
  end
45
47
 
48
+ def map_lr(fn_left, fn_right)
49
+ fn_left.(@value)
50
+ end
51
+
52
+ def map_left(fn=nil, &block)
53
+ assert_funcs(fn, block_given?)
54
+ left(block_given? ? block.call(@value) : fn.(@value))
55
+ end
56
+
46
57
  def <=>(other)
47
- @value <=> other.left
58
+ @value <=> other.left_value
48
59
  end
49
60
  end
50
61
 
@@ -61,15 +72,29 @@ class Right < Either
61
72
  true
62
73
  end
63
74
 
64
- def left
75
+ def left_value
65
76
  raise NoSuchElementException.new
66
77
  end
67
78
 
68
- def right
79
+ def right_value
69
80
  @value
70
81
  end
71
82
 
83
+ def map(fn=nil, &block)
84
+ assert_funcs(fn, block_given?)
85
+ right(block_given? ? block.call(@value) : fn.(@value))
86
+ end
87
+
88
+ def map_lr(fn_left, fn_right)
89
+ fn_right.(@value)
90
+ end
91
+
92
+ def map_left(fn=nil, &block)
93
+ assert_funcs(fn, block_given?)
94
+ self
95
+ end
96
+
72
97
  def <=>(other)
73
- @value <=> other.right
98
+ @value <=> other.right_value
74
99
  end
75
100
  end
@@ -87,10 +87,10 @@ module Functions
87
87
  end
88
88
 
89
89
  def get_left
90
- ->(either) { either.left }
90
+ ->(either) { either.left_value }
91
91
  end
92
92
 
93
93
  def get_right
94
- ->(either) { either.right }
94
+ ->(either) { either.right_value }
95
95
  end
96
96
  end
@@ -4,8 +4,8 @@ module Numbers
4
4
  monoid(->(a, b) { a + b }, 0)
5
5
  end
6
6
 
7
- def add
8
- sum
7
+ def add(increment)
8
+ -> (number) { number + increment }
9
9
  end
10
10
 
11
11
  def even
@@ -1,9 +1,29 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
3
  describe 'Either' do
4
- it 'should support filter and map' do
4
+ it 'can be used in filter and map' do
5
5
  eithers = sequence(left('error'), right(3))
6
6
  expect(eithers.filter(is_left?).map(get_left)).to eq(sequence('error'))
7
7
  expect(eithers.filter(is_right?).map(get_right)).to eq(sequence(3))
8
8
  end
9
+
10
+ it 'should support map' do
11
+ expect(right(3).map(add(2))).to eq(right(5))
12
+ expect(right(3).map { |a| a+2 }).to eq(right(5))
13
+ expect(left(3).map_lr(add(2), nil)).to eq(5)
14
+ expect(right(3).map_lr(nil, add(2))).to eq(5)
15
+ end
16
+
17
+ it 'should support map_left' do
18
+ expect(right(3).map_left(add(2))).to eq(right(3))
19
+ expect(right(3).map_left { |a| a+2 }).to eq(right(3))
20
+ expect(left(3).map_left(add(2))).to eq(left(5))
21
+ expect(left(3).map_left { |a| a+2 }).to eq(left(5))
22
+ end
23
+
24
+ it 'should raise exception if you try to use both lambda and block' do
25
+ expect { right(1).map(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
26
+ expect { right(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
27
+ expect { left(1).map_left(add(2)) { |a| a+2 } }.to raise_error(RuntimeError)
28
+ end
9
29
  end
@@ -29,11 +29,11 @@ describe 'Option' do
29
29
  end
30
30
 
31
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)
32
+ expect(option(1).fold(1, sum)).to eq(2)
33
+ expect(option(1).fold_left(1, sum)).to eq(2)
34
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)
35
+ expect(some(1).fold(1, sum)).to eq(2)
36
+ expect(none.fold(1, sum)).to eq(1)
37
37
  end
38
38
 
39
39
  it 'should support map' do
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.26
4
+ version: 0.1.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raymond Barlow
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-29 00:00:00.000000000 Z
12
+ date: 2016-03-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec