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 +8 -8
- data/lib/totally_lazy/either.rb +8 -0
- data/lib/totally_lazy/functions.rb +1 -2
- data/lib/totally_lazy/numbers.rb +2 -2
- data/lib/totally_lazy/predicates.rb +23 -11
- data/lib/totally_lazy/sequence.rb +4 -0
- data/spec/totally_lazy/functions_spec.rb +1 -1
- data/spec/totally_lazy/predicates_spec.rb +5 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODE3OWEyZGRkZjQwZjQ5MjdjOTI5YmFkMjdiZDU1Y2FhMjc0MzAwYQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZTEyZjkwNWU1ZmNjODA1NDFjOWVkOGEwNmQ4MmI3NmYyNzY5MWJmMg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MmU3MDY1NDBkOTg4NGFmZGE5ZDFlNTZjMDhlNjdhMGIyNjI2YzE3MDMxNjMy
|
10
|
+
ZWRhNDAwNGQzMGE5ZmI2NjRlMTdkNjhhOTA1Y2M4MjYyYTZjOGNiOWYyNGI1
|
11
|
+
MzZjOWRiZDQxOWI0YzY4OGQ4NGVmOTlhNWEyY2FmNGU4OWYxNzk=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YzM5MmRhZjQ4NTY3ZDAzMjQ0YzgyMjA2ZGQyMmJlNzBhNmI4N2FmZTdkMjA5
|
14
|
+
ZDg0ZjNkYzI2NDY1NGZiMGM1MTM5NjEwMzMwYjAwOGNjZjA2MWQ1M2RkODFk
|
15
|
+
NjVhNWZjMzIxY2JlMGNkOWIyZDJjMTkyYWJkODhmOTM1NjEyOTg=
|
data/lib/totally_lazy/either.rb
CHANGED
data/lib/totally_lazy/numbers.rb
CHANGED
@@ -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
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
12
|
-
-> (
|
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
|
@@ -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.
|
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.
|
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-
|
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
|