totally_lazy 0.0.14 → 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3229a338c39916c91506f35facbbfb43f2c95147
4
- data.tar.gz: c4f0b6ffa654e6360b0dc57268b17329b1ccdfed
3
+ metadata.gz: 76f6d260b177c8aa5b64578b0091377494ce839b
4
+ data.tar.gz: 312fa25893fa88b9d4cdc32222da221c57151e1d
5
5
  SHA512:
6
- metadata.gz: bea2ee197eddc5241277684ed67678c757a31256e261b5d488d876df66a54e42be56e6ec04ada9c5e414e4261ee6f0b090aa12691c1e74533e334500cd9c4541
7
- data.tar.gz: 6dd14258d2aecb4081a33ae6fc6086144fd9d798807abed81a557ba642cf346a02ca311788bf5606b799d8152fdb0d18c22956f914be015b2bbc551805eb62df
6
+ metadata.gz: b70457704551cdc3892eb34f9a02fb089d5fa4c0f39e2d5f10705536db1cf03f3bd2a080b52e31ef02ab9d2413342764a61fba413da77fdb86cee6b7de2e4370
7
+ data.tar.gz: 518b3ef257b1825686154c90e6a541ef207012fba8443b2fd18b42805c3be849891aba64924a885126a388b47c343cc565b268dd2a7932053696882c568b217f
data/README.md CHANGED
@@ -55,8 +55,8 @@ sequence(pair(1, 2), pair(3, 4)).filter(where(key:odd)) # lazily returns 1,3
55
55
 
56
56
  #### Sequences
57
57
 
58
- * sequence wraps whatever is passed without modification - e.g. sequence([1,2,3]).head returns the array [1,2,3]
59
- * sequence1 modifies what is passed if it's a single item - e.g. sequence1([1,2,3]).head returns the first element of 1
58
+ * sequence modifies what is passed if it's a single item - e.g. sequence1([1,2,3]).head returns the first element of 1
59
+ * sequence1 wraps whatever is passed without modification - e.g. sequence([1,2,3]).head returns the array [1,2,3]
60
60
 
61
61
  #### Generators
62
62
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.14
1
+ 0.0.15
data/lib/option.rb CHANGED
@@ -67,6 +67,10 @@ module Option
67
67
  sequence(get)
68
68
  end
69
69
 
70
+ def to_seq1
71
+ sequence1(get)
72
+ end
73
+
70
74
  def contains(item)
71
75
  value == item
72
76
  end
@@ -79,47 +83,51 @@ module Option
79
83
  sequence(value) << target_sequence
80
84
  end
81
85
 
86
+ def each(&block)
87
+ @content.each(&block)
88
+ end
89
+
82
90
  def map(predicate=nil, &block)
83
- as_option(sequence(@content).map(predicate, &block))
91
+ sequence(@content).map(predicate, &block)
84
92
  end
85
93
 
86
94
  alias collect map
87
95
 
88
96
  def select(predicate=nil, &block)
89
- as_option(sequence(@content).select(predicate, &block))
97
+ sequence(@content).select(predicate, &block)
90
98
  end
91
99
 
92
100
  alias find_all select
93
101
  alias filter select
94
102
 
95
103
  def reject(predicate=nil, &block)
96
- as_option(sequence(@content).reject(predicate, &block))
104
+ sequence(@content).reject(predicate, &block)
97
105
  end
98
106
 
99
107
  alias unfilter reject
100
108
 
101
109
  def grep(pattern)
102
- as_option(sequence(@content).grep(pattern))
110
+ sequence(@content).grep(pattern)
103
111
  end
104
112
 
105
113
  def drop(n)
106
- as_option(sequence(@content).drop(n))
114
+ sequence(@content).drop(n)
107
115
  end
108
116
 
109
117
  def drop_while(&block)
110
- as_option(sequence(@content).drop_while(&block))
118
+ @content.drop_while(&block)
111
119
  end
112
120
 
113
121
  def take(n)
114
- as_option(sequence(@content).take(n))
122
+ sequence(@content).take(n)
115
123
  end
116
124
 
117
125
  def take_while(&block)
118
- as_option(sequence(@content).take_while(&block))
126
+ sequence(@content).take_while(&block)
119
127
  end
120
128
 
121
129
  def flat_map(&block)
122
- as_option(sequence(@content).flat_map(&block))
130
+ sequence(@content).flat_map(&block)
123
131
  end
124
132
 
125
133
  alias collect_concat flat_map
@@ -201,6 +209,14 @@ module Option
201
209
  empty
202
210
  end
203
211
 
212
+ def to_seq1
213
+ empty
214
+ end
215
+
216
+ def each(&block)
217
+ empty
218
+ end
219
+
204
220
  def contains(item)
205
221
  false
206
222
  end
@@ -217,46 +233,46 @@ module Option
217
233
  alias << join
218
234
 
219
235
  def map(predicate=nil, &block)
220
- none
236
+ empty
221
237
  end
222
238
 
223
239
  alias collect map
224
240
 
225
241
  def select(predicate=nil, &block)
226
- none
242
+ empty
227
243
  end
228
244
 
229
245
  alias find_all select
230
246
  alias filter select
231
247
 
232
248
  def reject(predicate=nil, &block)
233
- none
249
+ empty
234
250
  end
235
251
 
236
252
  alias unfilter reject
237
253
 
238
254
  def grep(pattern)
239
- none
255
+ empty
240
256
  end
241
257
 
242
258
  def drop(n)
243
- none
259
+ empty
244
260
  end
245
261
 
246
262
  def drop_while(&block)
247
- none
263
+ empty
248
264
  end
249
265
 
250
266
  def take(n)
251
- none
267
+ empty
252
268
  end
253
269
 
254
270
  def take_while(&block)
255
- none
271
+ empty
256
272
  end
257
273
 
258
274
  def flat_map(&block)
259
- none
275
+ empty
260
276
  end
261
277
 
262
278
  alias collect_concat flat_map
data/lib/pair.rb CHANGED
@@ -48,7 +48,7 @@ module Pair
48
48
  end
49
49
 
50
50
  def self.from_map(a_map)
51
- sequence1(a_map).map { |k, v| Pair.new(k, v) }
51
+ sequence(a_map).map { |k, v| Pair.new(k, v) }
52
52
  end
53
53
 
54
54
  def to_s
data/lib/sequence.rb CHANGED
@@ -44,7 +44,7 @@ module Sequences
44
44
  # end
45
45
  # end
46
46
 
47
- def sequence1(*items)
47
+ def sequence(*items)
48
48
  if items.size == 1
49
49
  if [Range, Hash, Array, Set].include?(items.first.class)
50
50
  Sequence.new(items.first)
@@ -57,9 +57,8 @@ module Sequences
57
57
  Sequence.new(items)
58
58
  end
59
59
  end
60
- module_function :sequence1
61
60
 
62
- def sequence(*items)
61
+ def sequence1(*items)
63
62
  if items.first.nil?
64
63
  empty
65
64
  else
@@ -67,7 +66,6 @@ module Sequences
67
66
  end
68
67
  end
69
68
 
70
-
71
69
  # Creates an empty sequence
72
70
  #
73
71
  # == Returns:
@@ -82,7 +80,7 @@ module Sequences
82
80
  end
83
81
 
84
82
  def deserialize(data)
85
- sequence1(data).deserialize
83
+ sequence(data).deserialize
86
84
  end
87
85
 
88
86
  class Sequence < Enumerator
@@ -463,6 +461,7 @@ module Sequences
463
461
  def all
464
462
  to_a.flatten
465
463
  end
464
+ alias flatten all
466
465
 
467
466
  def sorting_by(*attr, &block)
468
467
  if attr.empty?
data/spec/option_spec.rb CHANGED
@@ -29,6 +29,15 @@ describe 'Option' do
29
29
  pass
30
30
  end
31
31
 
32
+ it 'should support each' do
33
+ result = []
34
+ option(sequence(1,2,3)).each{|i| result << i+1}
35
+ expect(result).to eq([2,3,4])
36
+ result = []
37
+ option(none).each{|i| result << i+1}
38
+ expect(result).to eq([])
39
+ end
40
+
32
41
  it 'should get or else' do
33
42
  expect(option(1).get_or_else(2)).to eq(1)
34
43
  expect(option(empty).get_or_else(2)).to eq(2)
@@ -59,6 +68,11 @@ describe 'Option' do
59
68
  expect(none.to_seq).to eq(empty)
60
69
  end
61
70
 
71
+ it 'should convert to sequence1' do
72
+ expect(option([1,2,3]).to_seq1).to eq(sequence1([1,2,3]))
73
+ expect(none.to_seq1).to eq(empty)
74
+ end
75
+
62
76
  it 'should raise empty exception when calling get on none' do
63
77
  expect { option(empty).get }.to raise_error(NoSuchElementException)
64
78
  end
@@ -113,57 +127,57 @@ describe 'Option' do
113
127
  end
114
128
 
115
129
  it 'should support select' do
116
- expect(option(2).filter(even)).to eq(some(2))
117
- expect(option(2).find_all(even)).to eq(some(2))
118
- expect(option(2).select(even)).to eq(some(2))
119
- expect(option(2).filter(&:even?)).to eq(some(2))
120
- expect(option(2).filter{|v| v == 2}).to eq(some(2))
121
- expect(option(nil).filter(even)).to eq(none)
130
+ expect(option(2).filter(even)).to eq(sequence(2))
131
+ expect(option(2).find_all(even)).to eq(sequence(2))
132
+ expect(option(2).select(even)).to eq(sequence(2))
133
+ expect(option(2).filter(&:even?)).to eq(sequence(2))
134
+ expect(option(2).filter{|v| v == 2}).to eq(sequence(2))
135
+ expect(option(nil).filter(even)).to eq(empty)
122
136
  end
123
137
 
124
138
  it 'should support map' do
125
- expect(option([{apple:1,pear:2},{melon:3}]).map{|h| h}).to eq(some([{apple:1,pear:2},{melon:3}]))
126
- expect(option({apple:1,pear:2})).to eq(some({apple:1,pear:2}))
127
- expect(option(1).map(as_string)).to eq(some('1'))
128
- expect(option(1).collect(as_string)).to eq(some('1'))
129
- expect(option(sequence(1, 2, 3)).map { |s| s.entries }).to eq(some([1, 2, 3]))
130
- expect(option(nil).map(as_string)).to eq(none)
139
+ expect(option([{apple:1,pear:2},{melon:3}]).map{|h| h}).to eq([{apple:1,pear:2},{melon:3}])
140
+ expect(option(1).map(as_string)).to eq(sequence('1'))
141
+ expect(option(1).collect(as_string)).to eq(sequence('1'))
142
+ expect(option(sequence(1, 2, 3)).map { |s| s.entries }.to_a).to eq([[1, 2, 3]])
143
+ expect(option(1).map{|v| v+1}).to eq(sequence(2))
144
+ expect(option(nil).map(as_string)).to eq(empty)
131
145
  end
132
146
 
133
147
  it 'should support reject' do
134
- expect(option(2).reject(odd)).to eq(some(2))
135
- expect(option(2).unfilter(odd)).to eq(some(2))
136
- expect(option(2).reject(&:odd?)).to eq(some(2))
137
- expect(option(2).reject{|v| v == 1}).to eq(some(2))
138
- expect(option(nil).reject(odd)).to eq(none)
148
+ expect(option(2).reject(odd)).to eq(sequence(2))
149
+ expect(option(2).unfilter(odd)).to eq(sequence(2))
150
+ expect(option(2).reject(&:odd?)).to eq(sequence(2))
151
+ expect(option(2).reject{|v| v == 1}).to eq(sequence(2))
152
+ expect(option(nil).reject(odd)).to eq(empty)
139
153
  end
140
154
 
141
155
  it 'should support grep' do
142
- expect(option('apple').grep(/p/)).to eq(some('apple'))
143
- expect(option(nil).grep(/p/)).to eq(none)
156
+ expect(option('apple').grep(/p/)).to eq(sequence('apple'))
157
+ expect(option(nil).grep(/p/)).to eq(empty)
144
158
  end
145
159
 
146
160
  it 'should support drop' do
147
- expect(option(1).drop(1)).to eq(none)
148
- expect(option(nil).drop(1)).to eq(none)
161
+ expect(option(1).drop(1)).to eq(empty)
162
+ expect(option(nil).drop(1)).to eq(empty)
149
163
  end
150
164
 
151
165
  it 'should support drop_while' do
152
- expect(option(1).drop_while { |n| n < 5 }).to eq(none)
153
- expect(option(nil).drop_while { |n| n < 5 }).to eq(none)
166
+ expect(option(sequence(1,7)).drop_while { |n| n < 5 }).to eq(sequence(7))
167
+ expect(option(nil).drop_while { |n| n < 5 }).to eq(empty)
154
168
  end
155
169
 
156
170
  it 'should support take' do
157
- expect(option(1).take(2)).to eq(some(1))
158
- expect(option(nil).take(2)).to eq(none)
171
+ expect(option(1).take(2)).to eq(sequence(1))
172
+ expect(option(nil).take(2)).to eq(empty)
159
173
  end
160
174
 
161
175
  it 'should support take_while' do
162
- expect(option(nil).take_while { |n| n < 5 }).to eq(none)
176
+ expect(option(nil).take_while { |n| n < 5 }).to eq(empty)
163
177
  end
164
178
 
165
179
  it 'should support flat_map' do
166
- expect(option(nil).flat_map{|v| v.first}).to eq(none)
180
+ expect(option(nil).flat_map{|v| v.first}).to eq(empty)
167
181
  end
168
182
 
169
183
  end
@@ -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(sequence1(1..50).shuffle.entries).not_to eq(sequence1(1..50).entries)
45
+ expect(sequence(1..50).shuffle.entries).not_to eq(sequence(1..50).entries)
46
46
  expect { empty.shuffle.first }.to raise_error(NoSuchElementException)
47
47
  end
48
48
 
@@ -153,6 +153,7 @@ describe 'Sequence' do
153
153
 
154
154
  it 'should return all as flattened array' do
155
155
  expect(sequence(sequence(1, 2, 3),sequence(4,5,6)).all).to eq([1,2,3,4,5,6])
156
+ expect(sequence(sequence(1, 2, 3),sequence(4,5,6)).flatten).to eq([1,2,3,4,5,6])
156
157
  end
157
158
 
158
159
  it 'should iterate empty' do
@@ -214,7 +215,7 @@ describe 'Sequence' do
214
215
  end
215
216
 
216
217
  it 'should convert sequence to a map' do
217
- expect(sequence1(oops('apple',1,'pear',2)).to_map).to eq({apple:1,pear:2})
218
+ expect(sequence(oops('apple',1,'pear',2)).to_map).to eq({apple:1,pear:2})
218
219
  end
219
220
 
220
221
  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.14 ruby lib
5
+ # stub: totally_lazy 0.0.15 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "totally_lazy"
9
- s.version = "0.0.14"
9
+ s.version = "0.0.15"
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-31"
14
+ s.date = "2015-11-01"
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 = [
@@ -59,7 +59,7 @@ Gem::Specification.new do |s|
59
59
  ]
60
60
  s.homepage = "http://github.com/kingsleyh/totally_lazy"
61
61
  s.licenses = ["MIT"]
62
- s.rubygems_version = "2.4.6"
62
+ s.rubygems_version = "2.2.2"
63
63
  s.summary = "Port of java functional library totally lazy to ruby"
64
64
 
65
65
  if s.respond_to? :specification_version then
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.14
4
+ version: 0.0.15
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-31 00:00:00.000000000 Z
11
+ date: 2015-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -201,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
201
  version: '0'
202
202
  requirements: []
203
203
  rubyforge_project:
204
- rubygems_version: 2.4.6
204
+ rubygems_version: 2.2.2
205
205
  signing_key:
206
206
  specification_version: 4
207
207
  summary: Port of java functional library totally lazy to ruby