funtools 0.5.1 → 0.6.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: 760735a64660eda23cf109045b3696041da6e8a4
4
- data.tar.gz: 610702e131264a06618c6e4a31b7742ca5e7e623
3
+ metadata.gz: b6162644033dfb0b9f74e9160309669f04c2515f
4
+ data.tar.gz: 05e8411ba686b59b795d704d7137d8d5eaa70cf2
5
5
  SHA512:
6
- metadata.gz: 225ca09218729ba85fab9a6fa331b1d8f3d3fd7a6a857ae28f06cb945e7c26daaf5da98f7797a6a054477a294f73463d78cd239d175a6ed919bf2a14e9af1f39
7
- data.tar.gz: d340f6d4c99996b7202f4a7ce1273d8850f68e6c842800646186a838781af57aa7b1a0240c0428df924e080841b523f1fe2a1fba121c43d2327cd195e60dedcd
6
+ metadata.gz: d8b8d0ae31a9584ac89608d90612fad08965d96e64a4bf94009d5d5658ff8e2b68581f3b14380c69e5e4e665792998e1be68448fb876fd2ae3485537c71d4660
7
+ data.tar.gz: fabe23f7ac2a025750a415a2a4485f1aeb58c585f39acc5090f2e81062407505d45159117226c4750f7fbaeb2a499712a2b1ba26ea18fa7348b7d3d3f89c7dc4
data/lib/funtools.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'funtools/cons'
2
+ require 'funtools/types'
2
3
  require 'funtools/recursion'
3
4
  require 'funtools/composition'
4
5
  require 'funtools/pattern-matching'
5
6
 
6
7
  module Funtools
7
- VERSION = '0.5.1'
8
+ VERSION = '0.6.0'
8
9
  end
data/lib/funtools/cons.rb CHANGED
@@ -58,8 +58,8 @@ class Cons
58
58
  left, right = right.car, right.cdr
59
59
  end
60
60
  else
61
- block.(right) unless right.nil?
62
- right = nil
61
+ block.(right) unless right.nil?
62
+ right = nil
63
63
  end
64
64
  end
65
65
  end
@@ -0,0 +1,58 @@
1
+ class Object
2
+ # Public: Define a method in the current scope which will execute a given
3
+ # block recursively until a fixpoint is reached.
4
+ #
5
+ # sym - Symbol defining the name of the method to be created.
6
+ # block - Block containing the logic for the function to be created.
7
+ #
8
+ # Returns nothing.
9
+ def settype(sym, *args, ret)
10
+ message = :define_method if respond_to?(:define_method, true)
11
+ message ||= :define_singleton_method
12
+ old_method = self.method(sym)
13
+ params = old_method.parameters
14
+
15
+ typedefs = old_method.parameters.select do |kind,_|
16
+ kind != :block
17
+ end.each_with_index.map do |req,index|
18
+ [req.first, args[index]]
19
+ end
20
+
21
+ check_type = ->(value, type) do
22
+ case type
23
+ when Class, Module
24
+ unless value.is_a?(type)
25
+ raise(TypeError, "Expected #{type}; got #{value.class}")
26
+ end
27
+ when Enumerable
28
+ unless type.map { |kind| value.is_a?(kind) }.any?
29
+ raise(TypeError, "Expected one of: #{type.join(', ')}; got #{value.class}")
30
+ end
31
+ else
32
+ raise(TypeError, "Unable to test type against #{type.class}")
33
+ end
34
+ value
35
+ end
36
+
37
+ align_types = ->(a) do
38
+ pivot = typedefs.index(typedefs.select { |k,n| k == :rest }.flatten)
39
+
40
+ if pivot
41
+ types_min = pivot + 1
42
+ args_num = a.length - types_min
43
+ rest_min = a.length - (args_num + 1)
44
+
45
+ typedefs[0...pivot].to_a.zip(a[0...pivot].to_a) +
46
+ a[pivot, args_num].to_a.zip([typedefs[pivot]].cycle).map(&:reverse) +
47
+ typedefs[types_min..-1].to_a.zip(a[rest_min..-1].to_a)
48
+ else
49
+ typedefs.zip(a)
50
+ end.reject { |t, v| v.nil? && t.first == :opt }.map { |t, v| [v, t.last] }
51
+ end
52
+
53
+ self.send(message, sym) do |*n, &b|
54
+ align_types.(n).each { |pair| check_type.(*pair) }
55
+ check_type.(old_method.(*n, &b), ret)
56
+ end
57
+ end
58
+ 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.5.1
4
+ version: 0.6.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-10-16 00:00:00.000000000 Z
11
+ date: 2014-10-28 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
@@ -21,6 +21,7 @@ files:
21
21
  - lib/funtools/cons.rb
22
22
  - lib/funtools/pattern-matching.rb
23
23
  - lib/funtools/recursion.rb
24
+ - lib/funtools/types.rb
24
25
  homepage: https://gitlab.com/wuest/funtools
25
26
  licenses:
26
27
  - MIT