funtools 0.0.2 → 0.1.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/pattern-matching.rb +37 -0
- data/lib/funtools.rb +1 -1
- 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: 2b4b981489c79e05b8c4a44d6f8afbff26dfc0e2
|
4
|
+
data.tar.gz: 465f3f22070619217d67a596f1cd2220f1d090be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5adbb472fd5108a306e5987f790dcd5425cb2803487bceccfff739f063126c3392d8c7e33ca53a99b364b93306fbe0a3802f719e0c271cd3e51958aad6d80070
|
7
|
+
data.tar.gz: 7550c3eaaa1c40fabe779e44b8c617faa444524701ec7ec82e62e8e4763fae055b889faeb9dd1b14718170e21c7d0b4e055888e78033385a072ffe9278a511d7
|
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
class Object
|
4
|
+
# Public: Define a method in the current scope which allow pattern matching
|
5
|
+
# function declaration to be used.
|
6
|
+
#
|
7
|
+
# sym - Symbol defining the name of the method to be created.
|
8
|
+
# block - Block containing the logic for the function to be created.
|
9
|
+
#
|
10
|
+
# Returns nothing.
|
11
|
+
def defpattern(sym)
|
12
|
+
patterns = []
|
13
|
+
self.class.send(:define_method, sym) do |*l, &b|
|
14
|
+
patterns << ->(*n) do
|
15
|
+
->(*m) do
|
16
|
+
flat_m = m.flatten
|
17
|
+
flat_n = n.flatten
|
18
|
+
if flat_m.length == flat_n.length
|
19
|
+
e = flat_m.zip(n.flatten)
|
20
|
+
unless e.reject { |e| e[0].nil? || e[0]==e[1] }.any?
|
21
|
+
b.call(*flat_n)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end.(l.flatten)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
yield
|
29
|
+
|
30
|
+
self.send(:define_method, sym) do |*args|
|
31
|
+
patterns.each do |pattern|
|
32
|
+
result = pattern[args]
|
33
|
+
break result unless result.nil?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
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.0
|
4
|
+
version: 0.1.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
|
+
date: 2014-09-04 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
|
@@ -17,6 +17,7 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- lib/funtools.rb
|
20
|
+
- lib/funtools/pattern-matching.rb
|
20
21
|
- lib/funtools/recursion.rb
|
21
22
|
homepage: https://gitlab.com/wuest/funtools
|
22
23
|
licenses:
|