funtools 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/funtools/recursion.rb +35 -1
- data/lib/funtools.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: 2e521d975280777a8df7fb3f67fdeff050745293
|
4
|
+
data.tar.gz: 297c3ca3cec5422c5e62452c561fbb1c4be70f03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe6023fb759a731e2a5f54365c1d7d133936467f0264129b6664d64901b399e65fb495138acc8088115946af97f0c6bdb0866946b4008114b13cf5a830fa714f
|
7
|
+
data.tar.gz: 604c170f0f4b3e4df6a4dfad143c246a0ed3dbd09e917ab093f45d16e02fba52621ef2d4de8967ccf9ce695d9fe541d6f83e862f01aff49354ce2f3d74b9ea3e
|
data/lib/funtools/recursion.rb
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
module Funtools
|
2
|
+
class RecurseArgs < Array; end
|
3
|
+
end
|
4
|
+
|
1
5
|
class Object
|
2
6
|
# Public: Define a method in the current scope which will execute a given
|
3
7
|
# block recursively until a fixpoint is reached.
|
@@ -6,7 +10,7 @@ class Object
|
|
6
10
|
# block - Block containing the logic for the function to be created.
|
7
11
|
#
|
8
12
|
# Returns nothing.
|
9
|
-
def
|
13
|
+
def defix(sym, &block)
|
10
14
|
message = :define_method if respond_to?(:define_method, true)
|
11
15
|
message ||= :define_singleton_method
|
12
16
|
self.send(message, sym) do |*n|
|
@@ -18,4 +22,34 @@ class Object
|
|
18
22
|
end
|
19
23
|
end
|
20
24
|
end
|
25
|
+
|
26
|
+
# Public: Define a method in the current scope which will execute a given
|
27
|
+
# block in such a manner that recursive calls are handled properly so long as
|
28
|
+
# the call constitutes tail recursion.
|
29
|
+
#
|
30
|
+
# sym - Symbol defining the name of the method to be created.
|
31
|
+
# block - Block containing the logic for the function to be created.
|
32
|
+
#
|
33
|
+
# Returns nothing.
|
34
|
+
def deftail(sym, &block)
|
35
|
+
message = :define_method if respond_to?(:define_method, true)
|
36
|
+
message ||= :define_singleton_method
|
37
|
+
|
38
|
+
self.send(message, sym) do |*n|
|
39
|
+
instance_exec { self.dup }.instance_exec do
|
40
|
+
class << self
|
41
|
+
self
|
42
|
+
end.class_eval do
|
43
|
+
define_method(sym) do |*a|
|
44
|
+
a.reduce(Funtools::RecurseArgs.new) { |c,e| c << e }
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
loop do
|
49
|
+
n = instance_exec(*n, &block)
|
50
|
+
return n unless n.is_a?(Funtools::RecurseArgs)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
21
55
|
end
|
data/lib/funtools.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: funtools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tina Wuest
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-11-
|
11
|
+
date: 2014-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Tools to assist in programming in a more functional style
|
14
14
|
email: tina@wuest.me
|