funtools 0.6.1 → 0.7.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 85c1455381faf2166f294f26ce8b18012c1865e3
4
- data.tar.gz: 1a36bf917be530c0081faff2d9c5dfa3890bf985
3
+ metadata.gz: 2e521d975280777a8df7fb3f67fdeff050745293
4
+ data.tar.gz: 297c3ca3cec5422c5e62452c561fbb1c4be70f03
5
5
  SHA512:
6
- metadata.gz: dcb8af919e949195458d5481baa423b03434d05f12d05eb6ef6585066a680db4c7ce7f7bfc28e0bad684f9287ab920cf0959c7be9a9d7277ccff62361845719c
7
- data.tar.gz: 0b7c58ed9c8e6f69e995dfaf9bf0d4120cdc4d64ab0f27506b9bf75af5b85ef7907f156cd7bebb0a97dd6b829ac7479aca995f26f5be090fe75dec4c5cfb794f
6
+ metadata.gz: fe6023fb759a731e2a5f54365c1d7d133936467f0264129b6664d64901b399e65fb495138acc8088115946af97f0c6bdb0866946b4008114b13cf5a830fa714f
7
+ data.tar.gz: 604c170f0f4b3e4df6a4dfad143c246a0ed3dbd09e917ab093f45d16e02fba52621ef2d4de8967ccf9ce695d9fe541d6f83e862f01aff49354ce2f3d74b9ea3e
@@ -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 deftail(sym, &block)
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
@@ -5,5 +5,5 @@ require 'funtools/composition'
5
5
  require 'funtools/pattern-matching'
6
6
 
7
7
  module Funtools
8
- VERSION = '0.6.1'
8
+ VERSION = '0.7.0'
9
9
  end
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.6.1
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-05 00:00:00.000000000 Z
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