totally_lazy 0.1.11 → 0.1.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzYxMTA5ZGE2YTIyZTM2Mzk1YzJiNjgwYTdlNGQ1ZjkwNmMzYzkwZg==
4
+ NzNiZGM3ZGU2ODAwMTFlZjMxYzg2NzAxYzI5YzA2YzU5YmQyZGNlMA==
5
5
  data.tar.gz: !binary |-
6
- NDMxYzEyM2VlYzQ3NWVjZDMyMmM4NTEzY2Q2Y2IyZDkzYjQwMWMyNg==
6
+ NGQ5ZDc1ZTQ4YWFkZmExMzJlMDAwMGI2NTEwYjA1MWMxZDQxY2JmOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTUwYTRjNWNkZTUwYWI2YzRkYWUzZmZmMDRmOTdkOWY4YTA0ZTliNjNlNTY1
10
- NThjNDMwZmExYTk1ZjQwOGMxNmVlYjZmZjQ3Mjk2MDE3MjhlOTY2NzJhN2U4
11
- NzhmYWRjYjVkZDQyZmIxMGVlNTc3MzNjMWJkOWExZTZhYmVhOTc=
9
+ YzZjYzZiNTI1M2QwMjNmNGRmNjQzYjA1NjY3YTUxNDk3OTNlYjQ1NTAwNzU3
10
+ NzlmNGZiZWQxMDNkNDNmNGUxMTQ0MTYxMGE2NGRhMTcwMDg2MjExOTI5NGU4
11
+ ODZmZTVlN2YyMTAzZDQ4ZTZhY2E1ODU2MTdiNDJiOGZiNDI3YTM=
12
12
  data.tar.gz: !binary |-
13
- NmRmYzgzZDIyYzk1NDRlNzRlNmQ0ZDY1ZTEwNTgyMTA5MDE4NDkxN2I4OGU5
14
- MWEyMTRlNWYzM2EyNzMzODQ4NWUwZDkzMTc3ZTcwMmI0YWY0ZThkZWMyM2Iy
15
- NmM1M2M2OWU3MDYyNDRlYTk0Y2VjMzk4ZThkZDQyMmExMTZlOGE=
13
+ ZGI0ZjdmYWQ0MjViOWU4Y2Q2ODdmYjlkYTUxNWFkOGM5MDAwNDIyM2MxN2Vm
14
+ NjJkN2FhZWE3ZWMxN2U4ZmQ0MzI0NGY1NDlkNzE5MDFiZGQxOWFhZjk2MDlh
15
+ OWNiNDVmZjZhOTk1OWRiN2ZiOWVlODdlZmI2YWFkMDkxZWQzOTI=
data/Rakefile CHANGED
@@ -24,7 +24,7 @@ task :guard do
24
24
  system('bundle exec guard')
25
25
  end
26
26
 
27
- version = "#{File.read('VERSION').to_s}.#{ENV['BUILD_NUMBER'].to_s}"
27
+ version = "#{File.read('VERSION').to_s}.#{ENV['BUILD_NUMBER'].nil? ? 'devbuild' : ENV['BUILD_NUMBER'].to_s}"
28
28
 
29
29
  require 'jeweler'
30
30
  Jeweler::Tasks.new do |gem|
data/lib/functions.rb CHANGED
@@ -2,6 +2,13 @@ require 'concurrent/executors'
2
2
  require 'concurrent/promise'
3
3
 
4
4
  module Functions
5
+ def monoid(fn, id)
6
+ fn.define_singleton_method(:identity) do
7
+ id
8
+ end
9
+ fn
10
+ end
11
+
5
12
  def returns(value)
6
13
  -> { value }
7
14
  end
data/lib/numbers.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Numbers
2
2
  def sum
3
- ->(a, b) { a + b }
3
+ monoid(->(a, b) { a + b }, 0)
4
4
  end
5
5
 
6
6
  def even
@@ -34,5 +34,4 @@ module Numbers
34
34
  def mod(divisor)
35
35
  ->(number) { number % divisor }
36
36
  end
37
-
38
37
  end
data/lib/sequence.rb CHANGED
@@ -128,18 +128,25 @@ module Sequences
128
128
 
129
129
  def reduce(fn=nil, &block)
130
130
  assert_funcs(fn, block_given?)
131
- @enumerator.inject { |accumulator, value|
132
- block_given? ? block.call(accumulator, value) : fn.(accumulator, value)
133
- }
131
+ _fn = block_given? ? ->(a, b) { block.call(a, b) } : fn
132
+ accumulator = seed(@enumerator, fn)
133
+ while has_next(@enumerator)
134
+ accumulator = _fn.(accumulator, @enumerator.next)
135
+ end
136
+ accumulator
134
137
  end
135
138
 
136
139
  alias reduce_left reduce
137
140
 
138
141
  def reduce_right(fn=nil, &block)
139
142
  assert_funcs(fn, block_given?)
140
- Enumerators::reverse(@enumerator).inject { |accumulator, value|
141
- block_given? ? block.call(value, accumulator) : fn.(value, accumulator)
142
- }
143
+ _fn = block_given? ? ->(a, b) { block.call(a, b) } : fn
144
+ reversed = Enumerators::reverse(@enumerator)
145
+ accumulator = seed(reversed, fn)
146
+ while has_next(reversed)
147
+ accumulator = _fn.(reversed.next, accumulator)
148
+ end
149
+ accumulator
143
150
  end
144
151
 
145
152
  def find(fn_pred=nil, &block_pred)
@@ -251,6 +258,11 @@ module Sequences
251
258
  def assert_funcs(fn, block_given)
252
259
  raise 'Cannot pass both lambda and block expressions' if !fn.nil? && block_given
253
260
  end
261
+
262
+ def seed(enumerator, fn)
263
+ enumerator.rewind
264
+ !fn.nil? && fn.respond_to?(:identity) ? fn.identity : enumerator.next
265
+ end
254
266
  end
255
267
 
256
268
  def group(key, enumerator)
data/lib/strings.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Strings
2
2
  def join(separator='')
3
- ->(a, b) { "#{a}#{separator}#{b}" }
3
+ monoid(->(a, b) { "#{a}#{separator}#{b}" }, '')
4
4
  end
5
5
 
6
6
  def to_characters
@@ -104,6 +104,10 @@ describe 'Sequence' do
104
104
  expect(sequence('1', '2', '3').reduce_left(join)).to eq('123')
105
105
  end
106
106
 
107
+ it 'should support reduce of empty sequence' do
108
+ expect(empty.reduce(sum)).to eq(0)
109
+ end
110
+
107
111
  it 'should support fold_right' do
108
112
  expect(empty.fold_right(4, sum)).to eq(4)
109
113
  expect(sequence(1).fold_right(4, sum)).to eq(5)
@@ -117,12 +121,12 @@ describe 'Sequence' do
117
121
  end
118
122
 
119
123
  it 'should support reduce_right' do
120
- # expect(sequence().reduce_right(sum)).to eq(0) <-- need a monoid to do this
124
+ expect(empty.reduce_right(sum)).to eq(0)
121
125
  expect(sequence(1).reduce_right(sum)).to eq(1)
122
126
  expect(sequence(1, 2).reduce_right(sum)).to eq(3)
123
127
  expect(sequence(1, 2, 3).reduce_right(sum)).to eq(6)
124
128
  expect(sequence(1, 2, 3).reduce_right { |a, b| a+b }).to eq(6)
125
- # expect(sequence().reduce_right(join)).to eq('') <-- need a monoid to do this
129
+ expect(empty.reduce_right(join)).to eq('')
126
130
  expect(sequence('1').reduce_right(join)).to eq('1')
127
131
  expect(sequence('1', '2').reduce_right(join)).to eq('12')
128
132
  expect(sequence('1', '2', '3').reduce_right(join)).to eq('123')
@@ -296,6 +300,4 @@ describe 'Sequence' do
296
300
  strings_block = sequence(1, 2).map_concurrently { |value| to_string.(value) }
297
301
  expect(strings_block).to eq(sequence('1', '2'))
298
302
  end
299
-
300
-
301
303
  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.11
4
+ version: 0.1.12
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-02-25 00:00:00.000000000 Z
12
+ date: 2016-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec