funkify 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -11
- data/lib/funkify.rb +5 -5
- data/lib/funkify/version.rb +1 -1
- data/spec/funkify_spec.rb +2 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45022580a84d3526daa607b98036af6b25a5866c
|
4
|
+
data.tar.gz: b16a211aebf621fa75120b2936fb286c7b5a231e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e8d44488265a956d845f8ce1a701ab1f09f0922494feb4f8cbc5945aa76c5b258b14e551be2ee28cf380c65166958221cf7ec6b03961d88cbad0ba6f8d1d194
|
7
|
+
data.tar.gz: 98937ae54c307757bd8d9720183bccb4f62795d751f0d10d80e52ca527c564d610d45757343211e741cce3d3699ca88713df29b7f338126a15865ec15454980f
|
data/README.md
CHANGED
@@ -2,17 +2,22 @@
|
|
2
2
|
|
3
3
|
_Haskell-style partial application and composition for Ruby methods_
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
--[Wikipedia](http://en.wikipedia.org/wiki/Partial_application)
|
5
|
+
Function composition when used in conjunction with partial application can yield exceptionally concise code, often more concise than the idiomatic Ruby counterpart. Check out the links
|
6
|
+
below for further explanations of these features and examples of their use in Haskell:
|
8
7
|
|
9
|
-
[Curring in Haskell](http://www.haskell.org/haskellwiki/Currying)<br>
|
10
8
|
[Partial application in Haskell](http://www.haskell.org/haskellwiki/Partial_application)<br>
|
11
|
-
[Function composition in Haskell](http://www.haskell.org/haskellwiki/Function_composition)
|
9
|
+
[Function composition in Haskell](http://www.haskell.org/haskellwiki/Function_composition)<br>
|
10
|
+
[Curring in Haskell](http://www.haskell.org/haskellwiki/Currying)
|
12
11
|
|
12
|
+
Also, watch [this video](http://www.youtube.com/watch?v=m3svKOdZijA) to learn about some of the cool things you can do with
|
13
|
+
function composition and partial application (the video is in Javascript but the ideas still apply to Ruby with Funkify)
|
13
14
|
|
14
15
|
## Usage
|
15
16
|
|
17
|
+
### Autocurrying
|
18
|
+
|
19
|
+
In order for a Ruby method to be amenable to partial application and composition it must first be autocurried:
|
20
|
+
|
16
21
|
```ruby
|
17
22
|
class MyFunkyClass
|
18
23
|
include Funkify
|
@@ -48,12 +53,12 @@ funky = MyFunkyClass.new
|
|
48
53
|
|
49
54
|
funky.add(1, 2) #=> This works normally and returns 3
|
50
55
|
add_1 = funky.add(1) #=> The `1` is partially applied and a `Proc` is returned
|
51
|
-
add_1.(2) #=> We invoke that `Proc` with the remaining argument
|
56
|
+
add_1.(2) #=> 3 (We invoke that `Proc` with the remaining argument)
|
52
57
|
```
|
53
58
|
|
54
59
|
### Function composition
|
55
60
|
|
56
|
-
We compose methods using the `*` and `|` operators.
|
61
|
+
We compose methods using the `*` and `|` operators.
|
57
62
|
|
58
63
|
`*` composes right to left, this is the standard way to compose functions found in languages like Haskell:
|
59
64
|
```ruby
|
@@ -68,9 +73,9 @@ We compose methods using the `*` and `|` operators.
|
|
68
73
|
(mult(5) | add(1) | negate).(3) #=> -16
|
69
74
|
```
|
70
75
|
|
71
|
-
As a cute bonus, we can inject values from the left into a pipeline with the `pass` method ([see more](http://showterm.io/47f46234281cf2c25f44a#fast)):
|
76
|
+
As a cute bonus, we can inject values from the left into a pipeline with the `pass` method together with the `>=` (pipeline operator) (([see more](http://showterm.io/47f46234281cf2c25f44a#fast)):
|
72
77
|
```ruby
|
73
|
-
pass(3)
|
78
|
+
pass(3) >= mult(5) | add(1) | negate #=> -16
|
74
79
|
```
|
75
80
|
|
76
81
|
#### Other examples:
|
@@ -84,7 +89,7 @@ Add 10 to every item in an Enumerable:
|
|
84
89
|
Multiply by 10 and negate every item in an Enumerable:
|
85
90
|
|
86
91
|
```ruby
|
87
|
-
(1..5).map &(funky.negate * funky.mult(10))
|
92
|
+
(1..5).map &(funky.negate * funky.mult(10)) #=> [-10, -20, -30, -40, -50]
|
88
93
|
```
|
89
94
|
|
90
95
|
## Installation
|
@@ -100,7 +105,7 @@ And then execute:
|
|
100
105
|
Or install it yourself as:
|
101
106
|
|
102
107
|
$ gem install funkify
|
103
|
-
|
108
|
+
|
104
109
|
## Dedication
|
105
110
|
|
106
111
|
This library was inspired in part by stimulating conversations with [epitron](https://github.com/epitron) on Freenode.
|
data/lib/funkify.rb
CHANGED
data/lib/funkify/version.rb
CHANGED
data/spec/funkify_spec.rb
CHANGED
@@ -143,11 +143,11 @@ describe Funkify do
|
|
143
143
|
|
144
144
|
describe "pass method" do
|
145
145
|
it 'passes values into a reverse-composition stream' do
|
146
|
-
(@c.pass(5)
|
146
|
+
(@c.pass(5) >= @c.add(5) | @c.mult(5)).should == 50
|
147
147
|
end
|
148
148
|
|
149
149
|
it 'passes values into a normal-composition stream' do
|
150
|
-
(@c.pass(5)
|
150
|
+
(@c.pass(5) >= @c.add(5) * @c.mult(5)).should == 30
|
151
151
|
end
|
152
152
|
end
|
153
153
|
end
|