totally_lazy 0.0.11 → 0.0.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -7
- data/VERSION +1 -1
- data/lib/pair.rb +1 -1
- data/lib/predicates/conversions.rb +4 -4
- data/lib/predicates/predicates.rb +3 -3
- data/lib/sequence.rb +25 -2
- data/spec/sequence_spec.rb +2 -2
- data/totally_lazy.gemspec +3 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f349b796deda1ef4fcc94670c205949b50ef43e
|
4
|
+
data.tar.gz: 4dc70d58fdb8a03b646a2b719b3a0576abd55107
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9083cc7feaa8e94ac3a9bef436654d7a32e8881e201b6bfa22f6f9b8879dfdd111dc88a3643669812ed5ce8ba08d4a11a0f4a6dae576357042465bbc73b8dd89
|
7
|
+
data.tar.gz: dbb5b555a96b0f61f66a91e77c3f90b9d209a223ebf9904bad36f0e702b146e265f226a238cdf27b2abd169601203ba2faffe8742ab3090835522e4eeae20b0e
|
data/README.md
CHANGED
@@ -119,24 +119,30 @@ You can use a regex predicate
|
|
119
119
|
sequence("apple","pear").filter(matches(/app/))
|
120
120
|
sequence(pair("apple",1),pair("pear",2)).filter(where(key:matches(/app/)))
|
121
121
|
|
122
|
-
#### Custom
|
122
|
+
#### Custom Transformer
|
123
123
|
|
124
|
-
|
124
|
+
There is a built in helper for a simple transform
|
125
125
|
|
126
|
-
*
|
127
|
-
* value predicate
|
128
|
-
* self predicate
|
126
|
+
* simple_transform
|
129
127
|
|
130
|
-
#####
|
128
|
+
##### simple_transform
|
131
129
|
|
132
130
|
For example:
|
133
131
|
|
134
132
|
def as_uppercase
|
135
|
-
|
133
|
+
simple_transform(:as_uppercase, -> (v) { v.upcase })
|
136
134
|
end
|
137
135
|
|
138
136
|
sequence("apple","pear").map(as_uppercase) # returns ["APPLE","PEAR"]
|
139
137
|
|
138
|
+
|
139
|
+
#### Custom Predicates
|
140
|
+
|
141
|
+
Writing a custom predicate is very easy and there are 3 built in helpers:
|
142
|
+
|
143
|
+
* value predicate
|
144
|
+
* self predicate
|
145
|
+
|
140
146
|
##### value_predicate
|
141
147
|
|
142
148
|
For example:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.12
|
data/lib/pair.rb
CHANGED
@@ -3,19 +3,19 @@ module Predicates
|
|
3
3
|
module Conversions
|
4
4
|
|
5
5
|
def as_string
|
6
|
-
|
6
|
+
simple_transform(:as_string, -> (v) { v.to_s } )
|
7
7
|
end
|
8
8
|
|
9
9
|
def as_int
|
10
|
-
|
10
|
+
simple_transform(:as_int, -> (v) { Type.responds(v, :to_i); v.to_i } )
|
11
11
|
end
|
12
12
|
|
13
13
|
def as_float
|
14
|
-
|
14
|
+
simple_transform(:as_float, -> (v) { Type.responds(v, :to_f); v.to_f } )
|
15
15
|
end
|
16
16
|
|
17
17
|
def as_array
|
18
|
-
|
18
|
+
simple_transform(:as_array, -> (v) { [v] } )
|
19
19
|
end
|
20
20
|
|
21
21
|
end
|
@@ -70,8 +70,8 @@ module Predicates
|
|
70
70
|
SelfPredicate.new(name,pred)
|
71
71
|
end
|
72
72
|
|
73
|
-
def
|
74
|
-
|
73
|
+
def simple_transform(name,exec)
|
74
|
+
SimpleTransform.new(name,exec)
|
75
75
|
end
|
76
76
|
|
77
77
|
|
@@ -110,7 +110,7 @@ module Predicates
|
|
110
110
|
|
111
111
|
end
|
112
112
|
|
113
|
-
class
|
113
|
+
class SimpleTransform < Predicates::Predicate
|
114
114
|
attr_reader :name, :exec
|
115
115
|
|
116
116
|
def initialize(name, exec)
|
data/lib/sequence.rb
CHANGED
@@ -30,7 +30,21 @@ module Sequences
|
|
30
30
|
# sequence(1,2,3).head_option # eagerly returns an option
|
31
31
|
# some(sequence(1,2,3)).get_or_else(empty) # eagerly returns value or else empty sequence
|
32
32
|
# sequence(1, 2, 3, 4, 5).filter(where(is greater_than 2).and(is odd)) # lazily returns 3,5
|
33
|
-
def sequence(*items)
|
33
|
+
# def sequence(*items)
|
34
|
+
# if items.size == 1
|
35
|
+
# if [Range, Hash, Array, Set].include?(items.first.class)
|
36
|
+
# Sequence.new(items.first)
|
37
|
+
# elsif items.first.nil?
|
38
|
+
# empty
|
39
|
+
# else
|
40
|
+
# Sequence.new(items)
|
41
|
+
# end
|
42
|
+
# else
|
43
|
+
# Sequence.new(items)
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
|
47
|
+
def from_single(*items)
|
34
48
|
if items.size == 1
|
35
49
|
if [Range, Hash, Array, Set].include?(items.first.class)
|
36
50
|
Sequence.new(items.first)
|
@@ -43,6 +57,15 @@ module Sequences
|
|
43
57
|
Sequence.new(items)
|
44
58
|
end
|
45
59
|
end
|
60
|
+
module_function :from_single
|
61
|
+
|
62
|
+
def sequence(*items)
|
63
|
+
if items.first.nil?
|
64
|
+
empty
|
65
|
+
else
|
66
|
+
Sequence.new(items)
|
67
|
+
end
|
68
|
+
end
|
46
69
|
|
47
70
|
|
48
71
|
# Creates an empty sequence
|
@@ -59,7 +82,7 @@ module Sequences
|
|
59
82
|
end
|
60
83
|
|
61
84
|
def deserialize(data)
|
62
|
-
|
85
|
+
from_single(data).deserialize
|
63
86
|
end
|
64
87
|
|
65
88
|
class Sequence < Enumerator
|
data/spec/sequence_spec.rb
CHANGED
@@ -42,7 +42,7 @@ describe 'Sequence' do
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'should lazily shuffle the elements - throws NoSuchElementException if empty' do
|
45
|
-
expect(
|
45
|
+
expect(from_single(1..50).shuffle.entries).not_to eq(from_single(1..50).entries)
|
46
46
|
expect { empty.shuffle.first }.to raise_error(NoSuchElementException)
|
47
47
|
end
|
48
48
|
|
@@ -213,7 +213,7 @@ describe 'Sequence' do
|
|
213
213
|
end
|
214
214
|
|
215
215
|
it 'should convert sequence to a map' do
|
216
|
-
expect(
|
216
|
+
expect(from_single(oops('apple',1,'pear',2)).to_map).to eq({apple:1,pear:2})
|
217
217
|
end
|
218
218
|
|
219
219
|
def oops(*values)
|
data/totally_lazy.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: totally_lazy 0.0.
|
5
|
+
# stub: totally_lazy 0.0.12 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "totally_lazy"
|
9
|
-
s.version = "0.0.
|
9
|
+
s.version = "0.0.12"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Kingsley Hendrickse"]
|
14
|
-
s.date = "2015-10-
|
14
|
+
s.date = "2015-10-31"
|
15
15
|
s.description = "Port of java functional library totally lazy to ruby"
|
16
16
|
s.email = "kingsleyhendrickse@me.com"
|
17
17
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: totally_lazy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kingsley Hendrickse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|