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/strings.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Strings
2
+ def join(separator='')
3
+ ->(a, b) { "#{a}#{separator}#{b}" }
4
+ end
5
+
6
+ def to_characters
7
+ ->(string) { Sequence.new(character_enumerator(string)) }
8
+ end
9
+
10
+ def to_string
11
+ ->(value) { value.to_s }
12
+ end
13
+ end
data/lib/totally_lazy.rb CHANGED
@@ -1,170 +1,19 @@
1
- require_relative 'type_check'
2
- require 'set'
3
- require 'prime'
4
- require_relative 'sequence'
5
- require_relative 'pair'
1
+ require_relative 'comparators'
2
+ require_relative 'enumerators'
3
+ require_relative 'functions'
4
+ require_relative 'numbers'
6
5
  require_relative 'option'
7
- require_relative 'functor'
8
- require_relative 'predicates/predicates'
9
- require_relative 'predicates/compare'
10
- require_relative 'predicates/conversions'
11
- require_relative 'predicates/numbers'
12
- require_relative 'predicates/where'
13
- require_relative 'predicates/where_processor'
14
- require_relative 'parallel/parallel'
15
- require_relative 'generators'
16
- require_relative 'any'
17
- require_relative 'utils'
6
+ require_relative 'pair'
7
+ require_relative 'predicates'
8
+ require_relative 'sequence'
9
+ require_relative 'strings'
18
10
 
19
- include Sequences
11
+ include Comparators
12
+ include Enumerators
13
+ include Functions
14
+ include Numbers
20
15
  include Option
21
16
  include Pair
22
17
  include Predicates
23
- include Predicates::Numbers
24
- include Predicates::Conversions
25
- include Predicates::Compare
26
- include Predicates::Where
27
- include Generators
28
- include Any
29
- include Maps
30
-
31
-
32
- # p sequence(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).filter(
33
- # where( is(greater_than 2).or(is equal_to 5) )
34
- # ).entries
35
- #
36
- # sequence.filter(where.is(greater_than 2))
37
-
38
-
39
-
40
- # p sequence(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).filter(where(is greater_than 2).or(is less_than 8 ).and(is odd)).entries
41
-
42
- # p sequence(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12).filter(where(is greater_than 2).and(is less_than 8 ).and(is even)).entries
43
-
44
- # p sequence(pair(1, 2), pair(3, 4)).filter(where(key:odd)).entries
45
-
46
- # p sequence(1,2,3,4,5,6).map_concurrently(as_string,in_threads:10).entries
47
- # p sequence(1,2,3,4,5,6).map_concurrently{|i| i+5 }.entries
48
-
49
- # sequence(1,2,3,4,5).each_concurrently{|i| sleep rand(10); p i }
50
-
51
-
52
-
53
- # p Seq.repeat('car').take(10).entries
54
- # p Seq.range(1,1000000000000000000000000000).take(20).entries
55
-
56
- # p Seq.iterate(:*,2).take(10).entries
57
- # p Seq.fibonacci.take(20).entries
58
- # p Iter.fibonacci.take(20).entries
59
- # p Iter.primes.take(10).entries
60
-
61
- # p Seq.powers_of(3).take(10).entries
62
- # p Iter.powers_of(3).take(10).entries
63
-
64
- # p Seq.iterate(:+,1,0).take(10).entries
65
-
66
- # p Seq.range(1,4).cycle.take(20).entries
67
- # p Iter.range(1,4).cycle.take(20).entries
68
-
69
- # p Seq.iterate(:+, 1).filter(even).take(2).reduce(:+)
70
-
71
- #
72
- # class Person
73
- # attr_accessor :first_name,:last_name,:age
74
- # def initialize(first_name,last_name,age)
75
- # @first_name = first_name
76
- # @last_name = last_name
77
- # @age = age
78
- # end
79
- #
80
- # def name
81
- # @first_name
82
- # end
83
- #
84
- # end
85
- #
86
- # people = Seq.repeat(-> {Person.new(Any.string,Any.string,Any.string)}).take(10)
87
- #
88
- # p people.filter(where(first_name:matches(/s/))).map(&:first_name).entries
89
-
90
- # p sequence(pair('apples','pairs'),pair('banana','orange'),pair('apples','melon')).filter(where key:matches(/app/)).entries
91
-
92
- # p Seq.repeat(-> { rand(9)}).take(10).to_a.join.to_i
93
-
94
- # p Seq.range(1,100000000000000).shuffle.take(10).to_a
95
-
96
- # a = sequence(pair(7,8),sequence(1,2),sequence(3,sequence(5,6)),sequence(pair(:apple,99), option(1),none),[10,11],{:apple => 8,:pear => 9}).serialize
97
- #
98
- # p a
99
- # p deserialize(a)[5]
100
-
101
- # p sequence(pair(1,2))[0]
102
-
103
- # p empty.gan(ser).entries
104
-
105
- # def countdown(c,n)
106
- # return if n.zero? # base case
107
- # c << n
108
- # countdown(c,n-1) # getting closer to base case
109
- # end
110
- #
111
- #
112
- # a = []
113
- # countdown(a,5)
114
- # p a
115
-
116
-
117
- # s1 = Seq.range(1,3000000)
118
- # s2 = Seq.range(1,3000000)
119
- #
120
- # s = Time.now
121
- # p s1.join2(s2).head
122
- # f = Time.now
123
- # p f - s
124
- #
125
- #
126
- # s = Time.now
127
- # p s1.join(s2).head
128
- # f = Time.now
129
- # p f - s
130
-
131
-
132
- # p Seq.range(1,20).filter(odd).update(10).to_a
133
-
134
- # require 'ostruct'
135
- # #
136
- # #
137
- # # p Seq.repeat(->{OpenStruct.new(name:Any.string(5),age:Any.int(2))}).take(100000).update(age:11,name:'woops').take(1000).head
138
- # #
139
- # # p sequence({apple:1,pear:2}).each_slice(2).map{|s| [s[0],s[1]]}.to_a
140
- #
141
- # a = Seq.range(1,10).map{|n| OpenStruct.new(name:Any.string(5),age:Any.int(2)) }.to_a
142
- #
143
- # orig = sequence(a)
144
- #
145
- # updates = orig.filter(where(age:greater_than(70))).update(age:2)
146
- #
147
- # p orig.count
148
- # p updates.count
149
-
150
- # p where(id:equals(1)).as_map.entries
151
-
152
- #
153
-
154
- # p sequence(1,2,3,4).filter(where(is equal_to(2))).entries
155
-
156
- # p where(is equal_to 2).predicates.head.value
157
-
158
-
159
- # p sequence(1,2,3,4,5,6).map(as_string).to_a
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
18
+ include Sequences
19
+ include Strings
data/readme.md ADDED
@@ -0,0 +1,2 @@
1
+ totally_lazy [![Build Status](https://travis-ci.org/raymanoz/totally_lazy.svg?branch=master)](https://travis-ci.org/raymanoz/totally_lazy)
2
+ ============
data/spec/option_spec.rb CHANGED
@@ -1,186 +1,10 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require_relative 'spec_helper'
2
2
 
3
3
  describe 'Option' do
4
-
5
- it 'should support contains' do
6
- expect(option(1).contains(1)).to eq(true)
7
- end
8
-
9
- it 'should support is alias' do
10
- pending('support is alias')
11
- pass
12
- end
13
-
14
- it 'should support exists' do
15
- pending('exists')
16
- pass
17
- end
18
-
19
- it 'should support join' do
20
- expect(option(1).join(sequence(2, 3))).to eq(sequence(1, 2, 3))
21
- end
22
-
23
- it 'should get value of some' do
24
- expect(option(1).get).to eq(1)
25
- end
26
-
27
- it 'should not get value of none' do
28
- pending('not get none')
29
- pass
30
- end
31
-
32
- it 'should support each' do
33
- result = 0
34
- option(1).each{|i| result=i+1}
35
- expect(result).to eq(2)
36
- result=0
37
- none.each{|i| result=100}
38
- expect(result).to eq(0)
39
- end
40
-
41
- it 'should get or else' do
42
- expect(option(1).get_or_else(2)).to eq(1)
43
- expect(option(empty).get_or_else(2)).to eq(2)
44
- end
45
-
46
- it 'should get or nil' do
47
- expect(option(1).get_or_nil).to eq(1)
48
- expect(option(empty).get_or_nil).to eq(nil)
49
- end
50
-
51
- it 'should get or raise exception' do
52
- expect(option(1).get_or_throw(Exception)).to eq(1)
53
- expect { option(empty).get_or_throw(Exception) }.to raise_error(Exception)
54
- end
55
-
56
- it 'should return boolean for empty?' do
57
- expect(option(1).empty?).to eq(false)
58
- expect(option(empty).empty?).to eq(true)
59
- end
60
-
61
- it 'should return boolean for defined?' do
62
- expect(option(1).defined?).to eq(true)
63
- expect(option('').defined?).to eq(false)
64
- end
65
-
66
- it 'should convert to sequence' do
67
- expect(option(1).to_seq).to eq(sequence(1))
68
- expect(none.to_seq).to eq(empty)
69
- end
70
-
71
- it 'should raise empty exception when calling get on none' do
72
- expect { option(empty).get }.to raise_error(NoSuchElementException)
73
- end
74
-
75
- it 'should raise empty exception when calling value on none' do
76
- expect { option(empty).value }.to raise_error(NoSuchElementException)
77
- end
78
-
79
- it 'should return true for empty on none' do
80
- expect(option(empty).empty?).to eq(true)
81
- end
82
-
83
- it 'should return a head option' do
84
- expect(option({:name => 'apples'}).head_option).to eq(some({:name => 'apples'}))
85
- expect(option(empty).head_option).to eq(none)
86
- end
87
-
88
- it 'should return false for defined on none' do
89
- expect(option(empty).defined?).to eq(false)
4
+ it 'should support is_empty? & is_defined?' do
5
+ expect(some(1).is_empty?).to eq(false)
6
+ expect(some(1).is_defined?).to eq(true)
7
+ expect(none.is_empty?).to eq(true)
8
+ expect(none.is_defined?).to eq(false)
90
9
  end
91
-
92
- it 'should return the else in get_or_else for none' do
93
- expect(option(empty).get_or_else(1)).to eq(1)
94
- end
95
-
96
- it 'should return nil in get_or_nil for none' do
97
- expect(option(empty).get_or_nil).to eq(nil)
98
- end
99
-
100
- it 'should raise exception in get_or_throw for none' do
101
- expect { option(empty).get_or_throw(Exception, 'oops') }.to raise_error(Exception)
102
- end
103
-
104
- it 'should return false for contains for none' do
105
- expect(option(empty).contains('ooo')).to eq(false)
106
- end
107
-
108
- it 'should return false for exists for none' do
109
- pending('needs work')
110
- pass
111
- end
112
-
113
- it 'should return false for contains for none' do
114
- expect(option(empty).contains('ooo')).to eq(false)
115
- end
116
-
117
- it 'should return original sequence for join on none' do
118
- expect(option(empty).join(sequence(1,2))).to eq(sequence(1,2))
119
- expect(option(empty).join([1,2])).to eq(sequence([1,2]))
120
- end
121
-
122
- it 'should know the type' do
123
- expect(option(empty).is_none?).to be(true)
124
- expect(option(empty).is_some?).to be(false)
125
- expect(option(1).is_none?).to be(false)
126
- expect(option(1).is_some?).to be(true)
127
- end
128
-
129
- it 'should support select' do
130
- expect(option(2).filter(even)).to eq(some(2))
131
- expect(option(2).find_all(even)).to eq(some(2))
132
- expect(option(2).select(even)).to eq(some(2))
133
- expect(option(2).filter(&:even?)).to eq(some(2))
134
- expect(option(2).filter{|v| v == 2}).to eq(some(2))
135
- expect(option(nil).filter(even)).to eq(none)
136
- end
137
-
138
- it 'should support map' do
139
- expect(option([{apple:1,pear:2},{melon:3}]).map{|h| h}).to eq(some([{apple:1,pear:2},{melon:3}]))
140
- expect(option({apple:1,pear:2})).to eq(some({apple:1,pear:2}))
141
- expect(option(1).map(as_string)).to eq(some('1'))
142
- expect(option(1).collect(as_string)).to eq(some('1'))
143
- expect(option(sequence(1, 2, 3)).map { |s| s.entries }).to eq(some([1, 2, 3]))
144
- expect(option(nil).map(as_string)).to eq(none)
145
- end
146
-
147
- it 'should support reject' do
148
- expect(option(2).reject(odd)).to eq(some(2))
149
- expect(option(2).unfilter(odd)).to eq(some(2))
150
- expect(option(2).reject(&:odd?)).to eq(some(2))
151
- expect(option(2).reject{|v| v == 1}).to eq(some(2))
152
- expect(option(nil).reject(odd)).to eq(none)
153
- end
154
-
155
- it 'should support grep' do
156
- expect(option('apple').grep(/p/)).to eq(some('apple'))
157
- expect(option(nil).grep(/p/)).to eq(none)
158
- end
159
-
160
- it 'should support drop' do
161
- expect(option(1).drop(1)).to eq(none)
162
- expect(option(nil).drop(1)).to eq(none)
163
- end
164
-
165
- it 'should support drop_while' do
166
- expect(option(sequence(1,7)).drop_while { |n| n < 5 }).to eq(some(1))
167
- expect(option(nil).drop_while { |n| n < 5 }).to eq(none)
168
- end
169
-
170
- it 'should support take' do
171
- expect(option(1).take(2)).to eq(some(1))
172
- expect(option(nil).take(2)).to eq(none)
173
- end
174
-
175
- it 'should support take_while' do
176
- expect(option(sequence(1,7)).take_while { |n| n < 5 }).to eq(some(1))
177
- expect(option(nil).take_while { |n| n < 5 }).to eq(none)
178
- end
179
-
180
- it 'should support flat_map' do
181
- expect(option(nil).flat_map{|v| v.first}).to eq(none)
182
- expect(option(1).flat_map{|v| some(2)}).to eq(some(2))
183
- expect(option(1).flat_map{|v| none}).to eq(none)
184
- end
185
-
186
10
  end