totally_lazy 0.1.53 → 0.1.54

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
- ZTdhZDRmMzNkZmNiODRjYjcxMTFjM2Y1NDgwN2MxZWFiMjU1YzQ2MA==
4
+ ODE3OWEyZGRkZjQwZjQ5MjdjOTI5YmFkMjdiZDU1Y2FhMjc0MzAwYQ==
5
5
  data.tar.gz: !binary |-
6
- NTllOGQ4MjEwY2UyZDNiMDI3YTAyNTgwY2UwNGU5NWRhYmFhOTY4ZA==
6
+ ZTEyZjkwNWU1ZmNjODA1NDFjOWVkOGEwNmQ4MmI3NmYyNzY5MWJmMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- Yjc5M2JmOTBmNWMyNWRiMmU3YzE5ZWE3OGMyYzM0MzM4OTQ5ZGExYjdjMjZm
10
- NWM4YWQwNDJmZjQ1MjBlNzBjY2M2YjlmMTRhYTE1YjZiYmZjMjdhNzZjMTNj
11
- ZTZiMTM2OWMwYmI3MDQ1ODhkNGYxMzcyN2RhZjI3N2ExNTgzNTM=
9
+ MmU3MDY1NDBkOTg4NGFmZGE5ZDFlNTZjMDhlNjdhMGIyNjI2YzE3MDMxNjMy
10
+ ZWRhNDAwNGQzMGE5ZmI2NjRlMTdkNjhhOTA1Y2M4MjYyYTZjOGNiOWYyNGI1
11
+ MzZjOWRiZDQxOWI0YzY4OGQ4NGVmOTlhNWEyY2FmNGU4OWYxNzk=
12
12
  data.tar.gz: !binary |-
13
- NmZjYWIxMjM4MWMzNzE0MTlmMDg1Y2ZmOGNiYzgxZGFmYzNmNzljNDc0Mzk0
14
- ODc4MmNhMDNmMWU3MmU5NGE5Y2VhODFjYjY1ZDdkMWFkYjA4MjI5NDg4MTU3
15
- ZWMwMzEzMTRiM2QwMDllODBkMjA3ZDVmOTcwYmJiYjQwZjk3NzM=
13
+ YzM5MmRhZjQ4NTY3ZDAzMjQ0YzgyMjA2ZGQyMmJlNzBhNmI4N2FmZTdkMjA5
14
+ ZDg0ZjNkYzI2NDY1NGZiMGM1MTM5NjEwMzMwYjAwOGNjZjA2MWQ1M2RkODFk
15
+ NjVhNWZjMzIxY2JlMGNkOWIyZDJjMTkyYWJkODhmOTM1NjEyOTg=
@@ -22,6 +22,14 @@ module Eithers
22
22
  Either.right(value)
23
23
  end
24
24
 
25
+ def is_left
26
+ predicate(-> (either) { either.is_left? })
27
+ end
28
+
29
+ def is_right
30
+ predicate(-> (either) { either.is_right? })
31
+ end
32
+
25
33
  def as_left
26
34
  ->(value) { left(value) }
27
35
  end
@@ -10,10 +10,9 @@ class Proc
10
10
  Proc.compose(self, g)
11
11
  end
12
12
 
13
- def and(g)
13
+ def and_then(g)
14
14
  Proc.compose(g, self)
15
15
  end
16
- alias and_then and
17
16
  end
18
17
 
19
18
  module Functions
@@ -21,7 +21,7 @@ module Numbers
21
21
  end
22
22
 
23
23
  def remainder_is(divisor, remainder)
24
- ->(dividend) { remainder(dividend, divisor) == remainder }
24
+ predicate(->(dividend) { remainder(dividend, divisor) == remainder })
25
25
  end
26
26
 
27
27
  def remainder(dividend, divisor)
@@ -45,6 +45,6 @@ module Numbers
45
45
  end
46
46
 
47
47
  def greater_than(right)
48
- ->(left) { left > right }
48
+ predicate(->(left) { left > right })
49
49
  end
50
50
  end
@@ -1,27 +1,39 @@
1
1
  module Predicates
2
2
  private
3
- def is_not(pred)
4
- -> (bool) { !pred.(bool) }
5
- end
6
-
7
- def is_left
8
- -> (either) { either.is_left? }
3
+ def predicate(fn)
4
+ def fn.and(other)
5
+ -> (value) { self.(value) && other.(value) }
6
+ end
7
+ def fn.or(other)
8
+ -> (value) { self.(value) || other.(value) }
9
+ end
10
+ fn
9
11
  end
10
12
 
11
- def is_right
12
- -> (either) { either.is_right? }
13
+ def is_not(pred)
14
+ predicate(-> (bool) { !pred.(bool) })
13
15
  end
14
16
 
15
17
  def matches(regex)
16
- ->(value) { !regex.match(value).nil? }
18
+ predicate(->(value) { !regex.match(value).nil? })
17
19
  end
18
20
 
19
21
  def equal_to?(that)
20
- ->(this) { this == that }
22
+ predicate(->(this) { this == that })
21
23
  end
22
24
  alias is equal_to?
23
25
 
24
26
  def where(fn, predicate)
25
- ->(value) { predicate.(fn.(value)) }
27
+ predicate(->(value) { predicate.(fn.(value)) })
28
+ end
29
+ end
30
+
31
+ class PredicateFunction < Proc
32
+ def and(other)
33
+ ->(value) { self.(value) && other.(value) }
34
+ end
35
+
36
+ def or(other)
37
+ ->(value) { self.(value) || other.(value) }
26
38
  end
27
39
  end
@@ -319,6 +319,10 @@ class Sequence
319
319
  @enumerator.entries <=> other.enumerator.entries
320
320
  end
321
321
 
322
+ def inspect
323
+ to_s
324
+ end
325
+
322
326
  def to_s
323
327
  sample = take(100).to_a.to_seq
324
328
  "[#{sample.is_empty? ? '' : sample.reduce(join_with_sep(','))}]"
@@ -5,6 +5,6 @@ describe 'Functions' do
5
5
  add_2 = ->(value) { value+2 }
6
6
  divide_by_2 = ->(value) { value/2 }
7
7
  expect(sequence(10).map(divide_by_2 * add_2)).to eq(sequence(6))
8
- expect(sequence(10).map(divide_by_2.and(add_2))).to eq(sequence(7))
8
+ expect(sequence(10).map(divide_by_2.and_then(add_2))).to eq(sequence(7))
9
9
  end
10
10
  end
@@ -32,4 +32,9 @@ describe 'Predicates' do
32
32
  it 'should be able to negate other predicates using is_not' do
33
33
  expect(sequence(raymond).filter(where(age, is_not(greater_than(40))))).to eq(empty)
34
34
  end
35
+
36
+ it 'should allow predicates to be composed using logical operations (AND/OR)' do
37
+ expect(sequence(1,2,3,4,5).filter(greater_than(2).and(odd))).to eq(sequence(3,5))
38
+ expect(sequence(1,2,3,4,5).filter(greater_than(2).or(odd))).to eq(sequence(1,3,4,5))
39
+ end
35
40
  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.53
4
+ version: 0.1.54
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-03-02 00:00:00.000000000 Z
12
+ date: 2016-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby-edge