curry 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/curry.rb +138 -0
  2. metadata +67 -0
@@ -0,0 +1,138 @@
1
+ class Proc
2
+ def * g
3
+ if g.arity == 0
4
+ lambda { self.call(g.call) }
5
+ else
6
+ vars = (1..(g.arity)).map{|num| "var_#{num}"}.join(",")
7
+ eval "lambda {|#{vars}| self.call(g.call(#{vars})) }"
8
+ end
9
+ end
10
+
11
+ def [](*args)
12
+ diff = self.arity - args.size
13
+ # do this? or throw an error on diff < 0 ?
14
+ if diff <= 0
15
+ self.call(*args)
16
+ else
17
+ vars = (1..diff).map{|num| "var_#{num}"}.join(",")
18
+ eval "lambda {|#{vars}| self.call(*(args + [#{vars}])) }"
19
+ end
20
+ end
21
+
22
+ def <<(*args)
23
+ self.[](*args)
24
+ end
25
+ end
26
+
27
+ module Curry
28
+ extend self
29
+
30
+ def foldl
31
+ lambda do |func, acc, xs|
32
+ xs.each do |x|
33
+ acc = func.call(acc, x)
34
+ end
35
+ acc
36
+ end
37
+ end
38
+
39
+
40
+ def foldl1
41
+ lambda do |func, xs|
42
+ acc = nil
43
+ xs.each do |x|
44
+ next acc = x unless acc
45
+ acc = func.call(acc, x)
46
+ end
47
+ acc
48
+ end
49
+ end
50
+
51
+ def foldr
52
+ lambda do |func, acc, xs|
53
+ xs.reverse.each do |x|
54
+ acc = func.call(acc, x)
55
+ end
56
+ acc
57
+ end
58
+ end
59
+
60
+ def foldr1
61
+ lambda do |func, xs|
62
+ acc = nil
63
+ xs.reverse_each do |x|
64
+ next acc = x unless acc
65
+ acc = func.call(acc, x)
66
+ end
67
+ acc
68
+ end
69
+ end
70
+
71
+ def map; lambda { |func, xs| xs.map(&func) }; end
72
+ def each; lambda { |func, xs| xs.each(&func) }; end
73
+ def fill; lambda { |func, xs| xs.fill(&func) }; end
74
+ def sort; lambda { |func, xs| xs.sort(&func) }; end
75
+ def select; lambda { |func, xs| xs.select(&func) }; end
76
+ def index; lambda { |func, xs| xs.index(&func) }; end
77
+ def reject; lambda { |func, xs| xs.reject(&func) }; end
78
+ def permutation; lambda { |func, xs| xs.permutation(&func) }; end
79
+ def each_index; lambda { |func, xs| xs.each_index(&func) }; end
80
+ def count; lambda { |func, xs| xs.count(&func) }; end
81
+ def cycle; lambda { |func, xs| xs.cycle(&func) }; end
82
+ def cyclen; lambda { |func, n, xs| xs.cycle(n, &func) }; end
83
+ def zip; lambda{ |func, xs, ys| xs.zip(ys, &func) }; end
84
+
85
+ def delete_if; lambda { |func, xs| xs.delete(&func) }; end
86
+ def drop_while; lambda { |func, xs| xs.drop_while(&func) }; end
87
+ def take_while; lambda { |func, xs| xs.take_while(&func) }; end
88
+
89
+ def fputs; lambda { |x| puts x }; end
90
+ def fp; lambda { |x| p x }; end
91
+ def fprint; lambda { |x| print x }; end
92
+
93
+ # synonyms
94
+ def collect; map; end
95
+
96
+ def inject; foldl; end
97
+ def reduce; inject; end
98
+
99
+ def inject1; foldl1; end
100
+ def reduce1; inject1; end
101
+
102
+ def filter; select; end
103
+ def zipWith; zip; end
104
+
105
+ def all; lambda { |func, xs| xs.select(&func).size == xs.size }; end
106
+ def any; lambda { |func, xs| xs.select(&func).size > 0 }; end
107
+
108
+
109
+ # operators
110
+ def add; lambda { |a, b| a + b }; end
111
+ def sub; lambda { |a, b| a - b }; end
112
+ def mul; lambda { |a, b| a * b }; end
113
+ def div; lambda { |a, b| a / b }; end
114
+ def pow; lambda { |a, b| a ** b }; end
115
+ def eql; lambda { |a, b| a == b }; end
116
+ def gt; lambda { |a, b| b > a }; end
117
+ def gte; lambda { |a, b| b >= a }; end
118
+ def lt; lambda { |a, b| b < a }; end
119
+ def lte; lambda { |a, b| b <= a }; end
120
+ def even; lambda { |a| a % 2 == 0 }; end
121
+ def odd; lambda { |a| !even[a] }; end
122
+ def fnot; lambda { |a| !a }; end
123
+
124
+
125
+ # converting a proc to a method
126
+ def to_m(name, funcname)
127
+ eval("def #{name}(*args); #{funcname}[*args]; end")
128
+ end
129
+
130
+ =begin
131
+
132
+ Example Usage:
133
+ @__map = map[add[10]]
134
+ to_m("adder", "@__map")
135
+ puts adder([1, 2, 3, 4, 5])
136
+
137
+ =end
138
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: curry
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Aditya Bhargava
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-02-25 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Better functional programming in Ruby [Experimental]
23
+ email: aditya@wefoundland.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/curry.rb
32
+ has_rdoc: true
33
+ homepage:
34
+ licenses: []
35
+
36
+ post_install_message:
37
+ rdoc_options: []
38
+
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ hash: 3
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.7
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Better functional programming in Ruby [Experimental]
66
+ test_files: []
67
+