funtools 0.1.0 → 0.2.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/composition.rb +53 -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: 0b5edb0da798439a7fdd98b6af37f909bd28f84b
|
4
|
+
data.tar.gz: 6bbba9a10d70f3cf3759dfee9fa4f7f6a8e83f3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47ddcc87eb9fa6947bfadafdefb8f0ff2652f6c0c9553d7c0e586614a04d768ee42a7e0ba6d2813ff44741908c9aaa3e20ac84f296e4fba31d6f6d13aecd92ec
|
7
|
+
data.tar.gz: 00a0a21287953eb66a9d78f3e2e8a827685ccef8cb514abede7ad817fd911fcf433cceceef91376e707fd1fa392c95c36d40bf2b380031ae18ab995e28a7ab06
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
class Method
|
4
|
+
# Public: Compose a Method.
|
5
|
+
#
|
6
|
+
# f - Method, Proc or Symbol describing a Proc to compose with the current
|
7
|
+
# method.
|
8
|
+
#
|
9
|
+
# Returns a composed Proc.
|
10
|
+
def *(f)
|
11
|
+
to_proc * f
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Proc
|
16
|
+
# Public: Compose a Proc.
|
17
|
+
#
|
18
|
+
# f - Method, Proc or Symbol describing a Proc to compose with the current
|
19
|
+
# Proc.
|
20
|
+
#
|
21
|
+
# Returns a composed Proc.
|
22
|
+
def *(f)
|
23
|
+
->(*n) { self[f.to_proc[*n]] }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Object
|
28
|
+
# Public: Compose a series of functions.
|
29
|
+
#
|
30
|
+
# f - Any number of Procs, Methods, or Symbols which should be composed. If
|
31
|
+
# functions f, g, and h are composed in that order, the result will be
|
32
|
+
# f . g . h -> f(g(h(...)))
|
33
|
+
#
|
34
|
+
# Returns a composed Proc.
|
35
|
+
def compose(*f)
|
36
|
+
f.map do |g|
|
37
|
+
g.is_a?(Array) ? ->(n) { g.first.to_proc[n,*g.drop(1)] } : g.to_proc
|
38
|
+
end.reduce(&:*)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Public: Mimic the -> macro in Clojure.
|
42
|
+
#
|
43
|
+
# data - Any data to be passed to the composed function.
|
44
|
+
# f - Any number of Procs, Methods, or Symbols which should be composed.
|
45
|
+
# If functions f, g, and h are composed in that order, the result will
|
46
|
+
# be f . g . h -> f(g(h(...)))
|
47
|
+
#
|
48
|
+
# Returns the result of the composed functions, called with data as the
|
49
|
+
# argument.
|
50
|
+
def pl(data, *f)
|
51
|
+
compose(*f.reverse)[*[data].flatten]
|
52
|
+
end
|
53
|
+
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.2.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-09-
|
11
|
+
date: 2014-09-10 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/composition.rb
|
20
21
|
- lib/funtools/pattern-matching.rb
|
21
22
|
- lib/funtools/recursion.rb
|
22
23
|
homepage: https://gitlab.com/wuest/funtools
|