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 +4 -4
- data/lib/funtools.rb +2 -1
- data/lib/funtools/cons.rb +2 -2
- data/lib/funtools/types.rb +58 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6162644033dfb0b9f74e9160309669f04c2515f
|
4
|
+
data.tar.gz: 05e8411ba686b59b795d704d7137d8d5eaa70cf2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8b8d0ae31a9584ac89608d90612fad08965d96e64a4bf94009d5d5658ff8e2b68581f3b14380c69e5e4e665792998e1be68448fb876fd2ae3485537c71d4660
|
7
|
+
data.tar.gz: fabe23f7ac2a025750a415a2a4485f1aeb58c585f39acc5090f2e81062407505d45159117226c4750f7fbaeb2a499712a2b1ba26ea18fa7348b7d3d3f89c7dc4
|
data/lib/funtools.rb
CHANGED
data/lib/funtools/cons.rb
CHANGED
@@ -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.
|
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-
|
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
|