totally_lazy 0.0.20 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/.idea/.name +1 -0
  3. data/.idea/.rakeTasks +7 -0
  4. data/.idea/compiler.xml +22 -0
  5. data/.idea/encodings.xml +6 -0
  6. data/.idea/misc.xml +19 -0
  7. data/.idea/modules.xml +8 -0
  8. data/.idea/vcs.xml +6 -0
  9. data/.travis.yml +2 -5
  10. data/Gemfile +6 -8
  11. data/Guardfile +2 -17
  12. data/LICENSE +202 -0
  13. data/Rakefile +18 -33
  14. data/VERSION +1 -1
  15. data/contributors.txt +1 -0
  16. data/lib/comparators.rb +9 -0
  17. data/lib/enumerators.rb +74 -0
  18. data/lib/functions.rb +66 -0
  19. data/lib/numbers.rb +38 -0
  20. data/lib/option.rb +38 -268
  21. data/lib/pair.rb +13 -51
  22. data/lib/predicates.rb +5 -0
  23. data/lib/sequence.rb +171 -526
  24. data/lib/strings.rb +13 -0
  25. data/lib/totally_lazy.rb +14 -165
  26. data/readme.md +2 -0
  27. data/spec/option_spec.rb +6 -182
  28. data/spec/sequence_spec.rb +202 -132
  29. data/spec/spec_helper.rb +0 -13
  30. data/totally_lazy.iml +74 -0
  31. metadata +58 -71
  32. data/.document +0 -5
  33. data/.rspec +0 -1
  34. data/LICENSE.txt +0 -20
  35. data/README.md +0 -173
  36. data/lib/any.rb +0 -13
  37. data/lib/functor.rb +0 -92
  38. data/lib/generators.rb +0 -161
  39. data/lib/parallel/parallel.rb +0 -442
  40. data/lib/parallel/processor_count.rb +0 -85
  41. data/lib/predicates/compare.rb +0 -25
  42. data/lib/predicates/conversions.rb +0 -22
  43. data/lib/predicates/numbers.rb +0 -21
  44. data/lib/predicates/predicates.rb +0 -141
  45. data/lib/predicates/where.rb +0 -34
  46. data/lib/predicates/where_processor.rb +0 -13
  47. data/lib/type_check.rb +0 -19
  48. data/lib/utils.rb +0 -9
  49. data/spec/functor_spec.rb +0 -35
  50. data/spec/generators_spec.rb +0 -37
  51. data/spec/pair_spec.rb +0 -44
  52. data/spec/predicate_spec.rb +0 -77
  53. data/spec/serialization_spec.rb +0 -56
  54. data/spec/type_check_spec.rb +0 -20
  55. data/spec/util_spec.rb +0 -10
  56. data/totally_lazy.gemspec +0 -101
data/lib/option.rb CHANGED
@@ -1,304 +1,74 @@
1
1
  module Option
2
2
 
3
- def option(thing)
4
- validate = thing.respond_to?(:empty?) ? thing.empty? : !thing
5
- validate ? none : some(thing)
3
+ def option(value)
4
+ value.nil? ? none : some(value)
6
5
  end
7
6
 
8
- def some(thing)
9
- Some.new(thing)
7
+ def some(value)
8
+ Some.new(value)
10
9
  end
11
10
 
12
- def none(thing=nil)
13
- None.new(thing)
11
+ def none
12
+ NONE
14
13
  end
15
14
 
16
- class Some
17
-
18
- include Comparable
19
-
20
- def initialize(content)
21
- @content = content
22
- raise(Exception, 'some cannot be nil') if @content.nil?
23
- end
24
-
25
- def <=>(object)
26
- self.state <=> object.state
27
- end
28
-
29
- def is_none?
30
- self.is_a?(None)
31
- end
32
-
33
- def is_some?
34
- self.is_a?(Some)
35
- end
36
-
37
- def get
38
- @content
39
- end
40
-
41
- def head_option
42
- as_option(sequence(@content))
43
- end
44
-
45
- def value
46
- get
47
- end
48
-
49
- def empty?
50
- blank?
51
- end
52
-
53
- def defined?
54
- !blank?
55
- end
56
-
57
- def get_or_else(item)
58
- blank? ? item : @content
59
- end
60
-
61
- def get_or_nil
62
- blank? ? nil : @content
63
- end
64
-
65
- def get_or_throw(exception, message='')
66
- blank? ? raise(exception, message) : @content
67
- end
68
-
69
- def to_seq
70
- sequence(get)
71
- end
72
-
73
- def contains(item)
74
- value == item
75
- end
76
-
77
- def exists?(predicate)
78
- value
79
- end
80
-
81
- def join(target_sequence)
82
- sequence(value) << target_sequence
83
- end
84
-
85
- def each(&block)
86
- block.call(@content)
87
- end
88
-
89
- def map(predicate=nil, &block)
90
- as_option(sequence(@content).map(predicate, &block))
91
- end
92
-
93
- alias collect map
94
-
95
- def select(predicate=nil, &block)
96
- as_option(sequence(@content).select(predicate, &block))
97
- end
98
-
99
- alias find_all select
100
- alias filter select
101
-
102
- def reject(predicate=nil, &block)
103
- as_option(sequence(@content).reject(predicate, &block))
104
- end
105
-
106
- alias unfilter reject
107
-
108
- def grep(pattern)
109
- as_option(sequence(@content).grep(pattern))
110
- end
111
-
112
- def drop(n)
113
- as_option(sequence(@content).drop(n))
114
- end
115
-
116
- def drop_while(&block)
117
- as_option(@content.drop_while(&block))
118
- end
119
-
120
- def take(n)
121
- as_option(sequence(@content).take(n))
122
- end
123
-
124
- def take_while(&block)
125
- as_option(@content.take_while(&block))
126
- end
127
-
128
- def flatten
129
- @content
130
- end
131
-
132
- def flat_map(&block)
133
- some(block.call(@content)).flatten
134
- end
135
-
136
- alias collect_concat flat_map
137
-
138
- # TODO - fix me
139
- # def zip(*args, &block)
140
- # sequence(@content).zip(*args,&block).head_option
141
- # end
142
-
143
- alias + join
144
- alias << join
145
-
146
- protected
147
- def state
148
- @content
149
- end
150
-
151
- private
152
-
153
- def blank?
154
- @content.respond_to?(:empty?) ? @content.empty? : !@content
15
+ class Option
16
+ def is_defined?
17
+ !is_empty?
155
18
  end
156
-
157
- def as_option(seq)
158
- seq.count == 1 ? seq.head_option : option(seq.entries)
159
- end
160
-
161
19
  end
162
20
 
163
- class None
164
-
21
+ class Some < Option
165
22
  include Comparable
23
+ attr_reader :value
166
24
 
167
- def initialize(content)
168
- @content = content
25
+ def initialize(value)
26
+ @value = value
169
27
  end
170
28
 
171
- # def entries
172
- # []
173
- # end
174
- #
175
- def <=>(object)
176
- self.state <=> object.state
29
+ def map(fn)
30
+ some(fn.(value))
177
31
  end
178
32
 
179
- def is_none?
180
- self.is_a?(None)
33
+ def enumerator
34
+ Enumerator.new { |y|
35
+ y << @value
36
+ raise StopIteration.new
37
+ }
181
38
  end
182
39
 
183
- def is_some?
184
- self.is_a?(Some)
185
- end
186
-
187
- def get
188
- raise NoSuchElementException.new, 'The option was empty'
189
- end
190
-
191
- def value
192
- raise NoSuchElementException.new, 'The option was empty'
193
- end
194
-
195
- def empty?
196
- true
197
- end
198
-
199
- def defined?
40
+ def is_empty?
200
41
  false
201
42
  end
202
43
 
203
- def get_or_else(item)
204
- item
205
- end
206
-
207
- def get_or_nil
208
- nil
209
- end
210
-
211
- def get_or_throw(exception, message='')
212
- raise(exception, message)
44
+ def <=>(other)
45
+ @value <=> other.value
213
46
  end
214
47
 
215
- def to_seq
216
- empty
217
- end
218
-
219
- def to_seq1
220
- empty
221
- end
222
-
223
- def each(&block)
224
- empty
225
- end
226
-
227
- def contains(item)
228
- false
229
- end
230
-
231
- def exists?(predicate)
232
- false
233
- end
234
-
235
- def join(target_sequence)
236
- target_sequence.is_a?(Sequences::Sequence) ? target_sequence : sequence(target_sequence)
237
- end
238
-
239
- alias + join
240
- alias << join
241
-
242
- def map(predicate=nil, &block)
243
- none
244
- end
245
-
246
- alias collect map
247
-
248
- def select(predicate=nil, &block)
249
- none
250
- end
251
-
252
- alias find_all select
253
- alias filter select
254
-
255
- def reject(predicate=nil, &block)
256
- none
257
- end
258
-
259
- alias unfilter reject
260
-
261
- def grep(pattern)
262
- none
263
- end
264
-
265
- def drop(n)
266
- none
267
- end
268
-
269
- def drop_while(&block)
270
- none
271
- end
272
-
273
- def take(n)
274
- none
275
- end
276
-
277
- def take_while(&block)
278
- none
48
+ def to_s
49
+ "Some(#{value})"
279
50
  end
51
+ end
280
52
 
281
- def flatten
282
- none
53
+ class None < Option
54
+ def is_empty?
55
+ true
283
56
  end
284
57
 
285
- def flat_map(&block)
58
+ def map(fn)
286
59
  none
287
60
  end
288
61
 
289
- def head_option
290
- none
62
+ def enumerator
63
+ Enumerator.new { |y|
64
+ raise StopIteration.new
65
+ }
291
66
  end
292
67
 
293
- alias collect_concat flat_map
294
-
295
- protected
296
-
297
- def state
298
- @content
68
+ def to_s
69
+ 'None'
299
70
  end
300
-
301
71
  end
302
72
 
303
-
304
- end
73
+ NONE=None.new
74
+ end
data/lib/pair.rb CHANGED
@@ -4,11 +4,8 @@ module Pair
4
4
  Pair.new(first, second)
5
5
  end
6
6
 
7
- def from_map(a_map)
8
- Pair.from_map(a_map)
9
- end
10
-
11
7
  class Pair
8
+ include Comparable
12
9
 
13
10
  def initialize(first, second)
14
11
  @first = -> { first }
@@ -16,63 +13,28 @@ module Pair
16
13
  end
17
14
 
18
15
  def first
19
- @first.call
16
+ @first.()
20
17
  end
21
18
 
22
- alias key first
23
-
24
19
  def second
25
- @second.call
26
- end
27
-
28
- def first=(v)
29
- @first = -> { v }
30
- end
31
- alias key= first=
32
-
33
- def second=(v)
34
- @second = -> { v }
35
- end
36
- alias value= second=
37
-
38
- def each(&block)
39
- [first,second].each do |i|
40
- block.call(i)
41
- end
20
+ @second.()
42
21
  end
43
22
 
44
- alias value second
45
-
46
- def to_map
47
- {first => second}
23
+ def enumerator
24
+ Enumerator.new { |y|
25
+ y << first
26
+ y << second
27
+ raise StopIteration.new
28
+ }
48
29
  end
49
30
 
50
- def self.from_map(a_map)
51
- sequence(a_map.map{|k,v| Pair.new(k,v)}).flatten
52
- # sequence(a_map).map{|k,v| Pair.new(k,v) }
53
- # sequence(a_map).map { |k,v| Pair.new(k,v) }
31
+ def <=>(other)
32
+ (first <=> other.first) <=> (second <=> other.second)
54
33
  end
55
34
 
56
35
  def to_s
57
- Type.responds_all(sequence(first, second), :to_s)
58
- {first.to_s => second.to_s}
59
- end
60
-
61
- def to_i
62
- Type.responds_all(sequence(first, second), :to_i)
63
- {first.to_i => second.to_i}
64
- end
65
-
66
- def to_f
67
- Type.responds_all(sequence(first, second), :to_f)
68
- {first.to_f => second.to_f}
36
+ "(#{first}, #{second})"
69
37
  end
70
-
71
- def values
72
- sequence(first, second)
73
- end
74
-
75
38
  end
76
39
 
77
-
78
- end
40
+ end
data/lib/predicates.rb ADDED
@@ -0,0 +1,5 @@
1
+ module Predicates
2
+ def not(pred)
3
+ -> (bool) { !pred.(bool) }
4
+ end
5
+ end