fn-ruby 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/functional-ruby.rb +127 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee18b610a7d2fc2c8c01d51e9be6c20995a0f096
4
+ data.tar.gz: 21a83c8174539c386f84b0c3957d02a3e76d83ee
5
+ SHA512:
6
+ metadata.gz: 21b3c24eb1f5542faa59d429894760afaeb1cb7eec2e9a84ad37ac33089ff027af30e2abad8a59745fc002fb619ea2deba8c18a7dc7dbecd68b61d5aaa0cfda3
7
+ data.tar.gz: 0c5bf2f7346f3c1863ca54c273a7911ef2f9404b713ecbb8e24900b5f8f1a4c1afe04f724062170e9e1256888b045c28a16b32a236f6352c9075e07c501f4943
@@ -0,0 +1,127 @@
1
+ module FunctionalRuby
2
+ ##
3
+ # Example
4
+ # pow = -> x, y { x ** y }
5
+ # (pow >> 2)[3] #=> 8
6
+ def apply_head(*first)
7
+ -> *rest { self[*first.concat(rest)] }
8
+ end
9
+
10
+ alias >> apply_head
11
+ alias % apply_head
12
+
13
+ ##
14
+ # Example
15
+ # pow = -> x, y { x ** y }
16
+ # (pow % 2)[3] #=> 9
17
+ def apply_tail(*last)
18
+ -> *rest { self[*rest.concat(last)] }
19
+ end
20
+
21
+ alias << apply_tail
22
+
23
+ ##
24
+ # Example
25
+ # f = -> x { x * x }
26
+ # g = -> x { x + 1 }
27
+ # (f * g)[3] #=> 16
28
+ def compose(f)
29
+ if respond_to? :arity and arity == 1
30
+ -> *args { self[f[*args]] }
31
+ else
32
+ -> *args { self[*f[*args]] }
33
+ end
34
+ end
35
+
36
+ alias * compose
37
+
38
+ ##
39
+ # Example
40
+ # f = -> x { x * x }
41
+ # d = [1, 2, 3]
42
+ # f | d #=> [1, 4, 9]
43
+ def map(enum)
44
+ enum.to_enum.map &self
45
+ end
46
+
47
+ alias | map
48
+
49
+ ##
50
+ # Example
51
+ # f = -> a, e { a * e }
52
+ # d = [1, 2, 3]
53
+ # f <= d #=> 6
54
+ def reduce(enum)
55
+ enum.to_enum.reduce &self
56
+ end
57
+
58
+ alias <= reduce
59
+
60
+ ##
61
+ # Example
62
+ # fact = +-> n { n < 2 ? 1 : n * fact[n - 1] }
63
+ # t = Time.now; fact[1000]; Time.now - t #=> 0.018036605
64
+ # t = Time.now; fact[1000]; Time.now - t #=> 2.6761e-05
65
+ def memoize
66
+ cache = {}
67
+
68
+ -> *args do
69
+ cache.fetch(args) do
70
+ cache[args] = self[*args]
71
+ end
72
+ end
73
+ end
74
+
75
+ alias +@ memoize
76
+
77
+ ##
78
+ # Example
79
+ # f = -> x { x * x }
80
+ # g = -> x { x + 1 }
81
+ # (f + g)[3] #=> 13
82
+ def +(f)
83
+ -> *args { self[*args] + f[*args] }
84
+ end
85
+
86
+ ##
87
+ # Example
88
+ # f = -> x { x * x }
89
+ # g = -> x { x + 1 }
90
+ # (f - g)[3] #=> 5
91
+ def -(f)
92
+ -> *args { self[*args] - f[*args] }
93
+ end
94
+
95
+ ##
96
+ # Example
97
+ # f = -> x { x * x }
98
+ # g = -> x { x + 1 }
99
+ # (f / g)[3] #=> 2
100
+ def /(f)
101
+ -> *args { self[*args] / f[*args] }
102
+ end
103
+
104
+ ##
105
+ # Example
106
+ # f = -> x { x * x }
107
+ # g = -> x { x + 1 }
108
+ # f.mult(g)[5] #=> 36
109
+ def mult(f)
110
+ -> *args { self[*args] * f[*args] }
111
+ end
112
+ end
113
+
114
+ class Proc
115
+ include FunctionalRuby
116
+ end
117
+
118
+ class Method
119
+ include FunctionalRuby
120
+ end
121
+
122
+ class Symbol
123
+ def +@
124
+ method(self)
125
+ end
126
+ end
127
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fn-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Speransky Danil
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: There are some utils for working in ruby in functional style.
14
+ email: speranskydanil@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/functional-ruby.rb
20
+ homepage: http://speranskydanil.github.io/functional-ruby/
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: There are some utils for working in ruby in functional style.
44
+ test_files: []
45
+ has_rdoc: