funtools 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b4b981489c79e05b8c4a44d6f8afbff26dfc0e2
4
- data.tar.gz: 465f3f22070619217d67a596f1cd2220f1d090be
3
+ metadata.gz: 0b5edb0da798439a7fdd98b6af37f909bd28f84b
4
+ data.tar.gz: 6bbba9a10d70f3cf3759dfee9fa4f7f6a8e83f3a
5
5
  SHA512:
6
- metadata.gz: 5adbb472fd5108a306e5987f790dcd5425cb2803487bceccfff739f063126c3392d8c7e33ca53a99b364b93306fbe0a3802f719e0c271cd3e51958aad6d80070
7
- data.tar.gz: 7550c3eaaa1c40fabe779e44b8c617faa444524701ec7ec82e62e8e4763fae055b889faeb9dd1b14718170e21c7d0b4e055888e78033385a072ffe9278a511d7
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
@@ -1,3 +1,3 @@
1
1
  module Funtools
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  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.1.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-04 00:00:00.000000000 Z
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