arrows 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -0
- data/lib/arrows/proc.rb +11 -0
- data/lib/arrows/version.rb +1 -1
- data/spec/arrows/proc_spec.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f8ad2dd9fb9682bfd2ee06da26cf374aaa87d372
|
4
|
+
data.tar.gz: b522108909adc26dfb1f062ff7f7b744034ea373
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0985e9dac6fa8e0ff9e72ca23d115fc9f4785683a0c83d522781c2565d84e322f5a5380aa517993ac791260f6de74afa5eaf0882950c14447f395bbaa2947023
|
7
|
+
data.tar.gz: 9de5e4aad6bd3c14a9559ccc6a10a817b43a42eeeee9f1aa2c06a7428542482d964b6baed99a5f71e3110a29fe9913d5b7d8e24bbc14ca21f2049e3bd7ca6a69
|
data/README.md
CHANGED
@@ -37,6 +37,21 @@ x -> [y,z]
|
|
37
37
|
### Arrow Fork
|
38
38
|
f ^ g produces a proc that takes in a Either, and if either is good, f is evaluated, if either is evil, g is evaluated
|
39
39
|
|
40
|
+
## Memoization
|
41
|
+
```ruby
|
42
|
+
@some_process = Arrows.lift -> (a) { a }
|
43
|
+
@memoized_process = @some_process.memoize
|
44
|
+
```
|
45
|
+
|
46
|
+
## Catching errors
|
47
|
+
```ruby
|
48
|
+
@times_two = Arrows.lift -> (x) { x * 2 }
|
49
|
+
@plus_one = Arrows.lift -> (x) { x == 4 ? raise(SomeError, "error: #{x}") : (x + 1) }
|
50
|
+
@times_two_plus_one = @times_two >> @plus_one
|
51
|
+
@caught_process = @times_two_plus_one.rescue_from(SomeError) { |error, arg| "we fucked up on: #{arg}" }
|
52
|
+
@caught_process.call 1 # 3
|
53
|
+
@caught_process.call 2 # we fucked up on: 2
|
54
|
+
```
|
40
55
|
|
41
56
|
## Use Case
|
42
57
|
Suppose you're running rails (lol what else is there in ruby?) for some sort of ecommerce app and you have an OfferController that handles inputs from an user who is trying to make an offer on some listing you have. Your controller might look like this:
|
data/lib/arrows/proc.rb
CHANGED
@@ -29,4 +29,15 @@ class Arrows::Proc < Proc
|
|
29
29
|
cache = {}
|
30
30
|
Arrows.lift -> (args) { cache.has_key?(args) ? cache[args] : (cache[args] = self[args]) }
|
31
31
|
end
|
32
|
+
|
33
|
+
# rescues errors from procs
|
34
|
+
def rescue_from(error_klass=StandardError, &block)
|
35
|
+
Arrows.lift -> (args) {
|
36
|
+
begin
|
37
|
+
self[args]
|
38
|
+
rescue error_klass => e
|
39
|
+
yield e, args
|
40
|
+
end
|
41
|
+
}
|
42
|
+
end
|
32
43
|
end
|
data/lib/arrows/version.rb
CHANGED
data/spec/arrows/proc_spec.rb
CHANGED
@@ -13,6 +13,22 @@ RSpec.describe Arrows::Proc do
|
|
13
13
|
specify { should eq twenty_two.call }
|
14
14
|
end
|
15
15
|
|
16
|
+
context 'rescue_from' do
|
17
|
+
let(:times2 ) { Arrows.lift -> (x) { x * 2 } }
|
18
|
+
let(:plus1) { Arrows.lift -> (x) { x == 4 ? raise(StandardError, "error: #{x}") : (x + 1) } }
|
19
|
+
let(:times2_plus1) { times2 >> plus1 }
|
20
|
+
let(:caught_proc) { times2_plus1.rescue_from { |e, x| "Oh look, we caught:#{x}" } }
|
21
|
+
let(:two) { Arrows.lift 2 }
|
22
|
+
let(:five) { two >> caught_proc }
|
23
|
+
let(:three) { Arrows.lift(1) >> caught_proc }
|
24
|
+
subject { five.call }
|
25
|
+
specify { should eq "Oh look, we caught:2" }
|
26
|
+
context 'regular usage' do
|
27
|
+
subject { three.call }
|
28
|
+
specify { should eq 3 }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
16
32
|
context '>> composition' do
|
17
33
|
let(:times2) { -> (x) { x * 2 } }
|
18
34
|
let(:plus3) { -> (x) { x + 3 } }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: arrows
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Chen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|