totally_lazy 0.1.13 → 0.1.14

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
- OGM1MDEzNTU2ZDQ0OGRlOTA3MDk5N2JjNGEzZjY2NDJkOTY4Y2M2MQ==
4
+ NjQ2MGUzNjA5MGRjZWI0YzRlMjBmN2MyMjkyYzIyNmQ2OGQxNTIzMg==
5
5
  data.tar.gz: !binary |-
6
- OTAwNWE3ZDk1NDQwNTcyYTZmZDU2MDM2NTY3NmFhNWEyZWRhM2RmMQ==
6
+ YWVkMTVlYWFiZThkNWQ2NWZlMmUwMWExNmE4YTJmNTU3NWY0MjhmOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGUxODhhYTkzOGFjMTBkZTY4YTdmYTdiN2Y4MmFkNTQzZGEwMGI1YWJiNDQ3
10
- MDQ5Yzk4OGJhYTlhODZkZTVkMTY3NTQ5N2RjMGE1NmM2OTcwMzE4YmRmMmM2
11
- NjhjYWQ3MzEyMmRkMWQ5ZDBhMjQ5MDljOGQ0MjFjYjA5Y2FiYzY=
9
+ OGY0MjIxNmY1YmVjNDRkNzNkMDkzMTU2NzJhY2RjYmU4MWFjYjlmNmI0MGMx
10
+ NTE2MmM4OTRkNzM0NWY3NjRhYjUxYjAwODM0OTYxYzVkNmZhMzMwYmY3ZThk
11
+ ZTM5ZjQ4ZmNkNWNkMGIxOTg1ZGE2N2E1ZmI0MjAzZTE2MTYxNTE=
12
12
  data.tar.gz: !binary |-
13
- YzY2YjUzMmRkMmQyNjdkY2M1ODFiYWFmMWU0Mjg5MmUxMWMxYjk1NmY3MTk5
14
- ZDY3YjJlYmQyZTU1OWU2ZTJlYzdmY2U4ZWY1NTZhYzNhNzI3NjM3MDRiYjk2
15
- ZTBhY2I1ODhhNDExMzY2MzA5NDZmYWY1NWUyMGNiNWYzN2JjYTI=
13
+ MGMyZjE0MWI1MDRkOWJkYTY1YjVlOWY3NWQ3ZWZjMmI0NzQyZDljNDIzOGFl
14
+ MDI5ZTgwNjlmODlkNGFkZWM0NjI2YmIxZjYxYWU5YjVjOGVkOGQzOWNjZDIw
15
+ OWQzNDM1M2Y0YmIxZmRiOGVhYTllMjkwMDVmNzRjOGUzN2IyMGM=
@@ -0,0 +1,6 @@
1
+ module LambdaBlock
2
+ private
3
+ def assert_funcs(fn, block_given)
4
+ raise 'Cannot pass both lambda and block expressions' if !fn.nil? && block_given
5
+ end
6
+ end
data/lib/numbers.rb CHANGED
@@ -34,4 +34,8 @@ module Numbers
34
34
  def mod(divisor)
35
35
  ->(number) { number % divisor }
36
36
  end
37
+
38
+ def greater_than(right)
39
+ ->(left) { left > right }
40
+ end
37
41
  end
data/lib/option.rb CHANGED
@@ -1,4 +1,7 @@
1
+ require_relative 'lambda_block'
2
+
1
3
  module Option
4
+ include LambdaBlock
2
5
 
3
6
  def option(value)
4
7
  value.nil? ? none : some(value)
@@ -18,6 +21,11 @@ module Option
18
21
  end
19
22
  end
20
23
 
24
+ def is?(fn_pred=nil, &block_pred)
25
+ assert_funcs(fn_pred, block_given?)
26
+ exists?(block_given? ? ->(value) { block_pred.call(value) } : fn_pred)
27
+ end
28
+
21
29
  class Some < Option
22
30
  include Comparable
23
31
  attr_reader :value
@@ -30,6 +38,11 @@ module Option
30
38
  @value == value
31
39
  end
32
40
 
41
+ def exists?(fn_pred=nil, &block_pred)
42
+ assert_funcs(fn_pred, block_given?)
43
+ block_given? ? block_pred.call(@value) : fn_pred.(value)
44
+ end
45
+
33
46
  def map(fn)
34
47
  some(fn.(value))
35
48
  end
@@ -59,7 +72,11 @@ module Option
59
72
  true
60
73
  end
61
74
 
62
- def contains?(_)
75
+ def contains?(value)
76
+ false
77
+ end
78
+
79
+ def exists?(fn_pred=nil, &block_pred)
63
80
  false
64
81
  end
65
82
 
data/lib/sequence.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require_relative 'lambda_block'
2
+
1
3
  class NoSuchElementException < RuntimeError
2
4
  end
3
5
 
@@ -45,6 +47,7 @@ module Sequences
45
47
  # noinspection RubyTooManyMethodsInspection
46
48
  class Sequence
47
49
  include Comparable
50
+ include LambdaBlock
48
51
  attr_reader :enumerator
49
52
 
50
53
  def initialize(enumerator)
@@ -255,10 +258,6 @@ module Sequences
255
258
  end
256
259
 
257
260
  private
258
- def assert_funcs(fn, block_given)
259
- raise 'Cannot pass both lambda and block expressions' if !fn.nil? && block_given
260
- end
261
-
262
261
  def seed(enumerator, fn)
263
262
  enumerator.rewind
264
263
  !fn.nil? && fn.respond_to?(:identity) ? fn.identity : enumerator.next
data/lib/totally_lazy.rb CHANGED
@@ -16,4 +16,4 @@ include Option
16
16
  include Pair
17
17
  include Predicates
18
18
  include Sequences
19
- include Strings
19
+ include Strings
data/spec/option_spec.rb CHANGED
@@ -14,4 +14,22 @@ describe 'Option' do
14
14
  expect(none.contains?(2)).to eq(false)
15
15
  end
16
16
 
17
+ it 'should support exists' do
18
+ expect(option(1).exists?(greater_than(0))).to eq(true)
19
+ expect(option(1).exists? { |a| a > 0 }).to eq(true)
20
+ expect(option(1).exists?(greater_than(1))).to eq(false)
21
+ expect(none.exists?(greater_than(0))).to eq(false)
22
+ end
23
+
24
+ it 'should support is alias' do
25
+ expect(option(1).is?(greater_than(0))).to eq(true)
26
+ expect(option(1).is? { |a| a > 0 }).to eq(true)
27
+ expect(option(1).is?(greater_than(1))).to eq(false)
28
+ expect(none.is?(greater_than(0))).to eq(false)
29
+ end
30
+
31
+ 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)
34
+ end
17
35
  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.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raymond Barlow
@@ -157,6 +157,7 @@ files:
157
157
  - lib/comparators.rb
158
158
  - lib/enumerators.rb
159
159
  - lib/functions.rb
160
+ - lib/lambda_block.rb
160
161
  - lib/numbers.rb
161
162
  - lib/option.rb
162
163
  - lib/pair.rb