functional_support 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/functional_support/core_ext/array.rb +28 -0
- data/lib/functional_support/version.rb +1 -1
- 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: 140547ef87edd3b80ce133b0d8509021b7f681f7
|
4
|
+
data.tar.gz: ef99369b86653b44445ae93c03e3d4113f37798e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d4477d492bb79cda2c64c328d0d0c0c482a9122e9b045811021a86fbdf18f4911730c7cb7bc262f9d095cb26286b2b402221ce0c847c403a965f984ff6ea96b
|
7
|
+
data.tar.gz: 83a937615e25234afebbce60b82b9c7bcbdf4ef0f3696959aa857359522592a42571b36897e5512437e4a26aa75b83e15a8a02d4a1c5835cc45e818327fec0cf
|
@@ -8,6 +8,34 @@ class Array
|
|
8
8
|
self.take self.count - 1 unless self.count == 0
|
9
9
|
end
|
10
10
|
|
11
|
+
# same as reduce, except the reduction function can have arity > 2
|
12
|
+
# with the second, third, etc. arguments being the lookahead
|
13
|
+
def reduce_with_lookahead(*parameters, &reducer)
|
14
|
+
case parameters.length
|
15
|
+
when 0
|
16
|
+
in_repeated_groups_of(reducer.arity - 1).reduce do |acc, arr|
|
17
|
+
reducer.call acc, *arr
|
18
|
+
end
|
19
|
+
when 1
|
20
|
+
init = parameters.first
|
21
|
+
in_repeated_groups_of(reducer.arity - 1).reduce(init) do |acc, arr|
|
22
|
+
reducer.call acc, *arr
|
23
|
+
end
|
24
|
+
else
|
25
|
+
raise LocalJumpError, "no block given"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
alias_method :inject_with_lookahead, :reduce_with_lookahead
|
29
|
+
|
30
|
+
# Suppose n = 3, this takes something like [1,2,3,4,5]
|
31
|
+
# and splits it into something like the following:
|
32
|
+
# [[1,2,3], [2,3,4], [3,4,5]]
|
33
|
+
def in_repeated_groups_of(n=1)
|
34
|
+
return [self] if length == n
|
35
|
+
return shift(nil).in_repeated_groups_of(n) if length < n
|
36
|
+
[take(n)] + tail.in_repeated_groups_of(n)
|
37
|
+
end
|
38
|
+
|
11
39
|
def present_unshift(element=nil)
|
12
40
|
unshift element if element.present?
|
13
41
|
self
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: functional_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|