deterministic 0.15.0 → 0.15.1

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: 8a1e76d13413c85adaf4807bc4967275b85a0436
4
- data.tar.gz: c3eae53b159a16996c9f384c15b5274fe90b14f1
3
+ metadata.gz: d83f1708bbf0ddcf6cf2278121003fdcce3a5fcb
4
+ data.tar.gz: 3c58bb6bf5fa3bf36216c66a8e34e254bf1984f7
5
5
  SHA512:
6
- metadata.gz: 3955dda1c3e00c7a3814d5bb766453a3615c29a65cdec56e21a91bcdf4feff8d55f5f7bb1999d78116306d3bca59d7d30de620bc80f70e6df25b1c6eb7f349f8
7
- data.tar.gz: 84da7a63dab19f2fa754ad4694c7b3b00eccf3e5684d00caa5e5d9d854cb4f46075ea597f089685967e6f710cdd1dbe66e36833824d333bd981d452206c6ce75
6
+ metadata.gz: 82670f74e7a792e4d2fb45616d8d475f972cee51227290390d4425b712f038e08c3795104dc1f15dae75840d0785a6b26c8fb26c32e98a412fd61ebf6b0ffda7
7
+ data.tar.gz: 85142539d1a67926483429d55139f586415a5c0de5ffa67626877929cecaa7f223041e70f78a51e1bc8a3907df65d261e2ab382cadc50b13f4ac19f1a99efc6b
data/README.md CHANGED
@@ -106,6 +106,15 @@ Failure(1).or_else { |n| Success(n)} # => Success(1)
106
106
 
107
107
  Executes the block passed, but completely ignores its result. If an error is raised within the block it will **NOT** be catched.
108
108
 
109
+ Try failable operations to return `Success` or `Failure`
110
+
111
+ ```ruby
112
+ include Deterministic::Prelude::Result
113
+
114
+ try! { 1 } # => Success(1)
115
+ try! { raise "hell" } # => Failure(#<RuntimeError: hell>)
116
+ ```
117
+
109
118
  ### Result Chaining
110
119
 
111
120
  You can easily chain the execution of several operations. Here we got some nice function composition.
@@ -390,6 +399,30 @@ Threenum::Unary(5).match {
390
399
  } # => 5
391
400
  ```
392
401
 
402
+ Implementing methods for enums
403
+
404
+ ```ruby
405
+ Deterministic::impl(Threenum) {
406
+ def sum
407
+ match {
408
+ Nullary() { 0 }
409
+ Unary(u) { u }
410
+ Binary(a, b) { a + b }
411
+ }
412
+ end
413
+
414
+ def +(other)
415
+ match {
416
+ Nullary() { other.sum }
417
+ Unary(a) { |this| this.sum + other.sum }
418
+ Binary(a, b) { |this| this.sum + other.sum }
419
+ }
420
+ end
421
+ }
422
+
423
+ Threenum.Nullary + Threenum.Unary(1) # => Unary(1)
424
+ ```
425
+
393
426
  All matches must be exhaustive, i.e. cover all variants
394
427
 
395
428
  ## Maybe
@@ -1,10 +1,21 @@
1
-
2
1
  module Deterministic
3
2
  Result = Deterministic::enum {
4
3
  Success(:s)
5
4
  Failure(:f)
6
5
  }
7
6
 
7
+ class Result
8
+ class << self
9
+ def try!
10
+ begin
11
+ Success.new(yield)
12
+ rescue => err
13
+ Failure.new(err)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
8
19
  Deterministic::impl(Result) {
9
20
  def map(proc=nil, &block)
10
21
  match {
@@ -71,6 +82,7 @@ end
71
82
  module Deterministic
72
83
  module Prelude
73
84
  module Result
85
+ def try!(&block); Deterministic::Result.try!(&block); end
74
86
  def Success(s); Deterministic::Result::Success.new(s); end
75
87
  def Failure(f); Deterministic::Result::Failure.new(f); end
76
88
  end
@@ -1,3 +1,3 @@
1
1
  module Deterministic
2
- VERSION = "0.15.0"
2
+ VERSION = "0.15.1"
3
3
  end
@@ -22,7 +22,7 @@ class BookingController
22
22
  end
23
23
 
24
24
  def log(step)
25
- ->(data) { p [step, data] }
25
+ ->(data) { [step, data] }
26
26
  end
27
27
 
28
28
  def ability(ctx)
@@ -53,12 +53,10 @@ class Booking
53
53
  end
54
54
 
55
55
  def validate(id)
56
- p [:validate, id]
57
56
  Success(id)
58
57
  end
59
58
 
60
59
  def req(a, id)
61
- p [:req, a, id]
62
60
  Success(id: id + a)
63
61
  end
64
62
 
@@ -81,7 +79,6 @@ describe "Pref" do
81
79
  b = Booking.new(1)
82
80
  actual = b.validate(1) >> b.req(2) >> b.find >> b.render(:html)
83
81
 
84
- p [:actual, actual]
85
82
  expected = Deterministic::Result::Success.new("rendered in html: {:id=>3}")
86
83
  expect(actual).to eq expected
87
84
  end
@@ -49,4 +49,10 @@ describe Deterministic::Result do
49
49
  specify { expect(subject.and(Failure(2))).to eq Failure(2)}
50
50
  specify { expect(subject.and_then { Success(2) }).to eq Success(2)}
51
51
  specify { expect(subject.and_then { Failure(2) }).to eq Failure(2)}
52
+
53
+
54
+ it "try!" do
55
+ expect(described_class.try! { 1 }).to eq Success(1)
56
+ expect(described_class.try! { raise "error" }.inspect).to eq Failure(RuntimeError.new("error")).inspect
57
+ end
52
58
  end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ include Deterministic::Prelude::Result
4
+
5
+ Success(1).to_s # => "1"
6
+ Success(Success(1)) # => Success(1)
7
+
8
+ Failure(1).to_s # => "1"
9
+ Failure(Failure(1)) # => Failure(1)
10
+
11
+ Success(1).fmap { |v| v + 1} # => Success(2)
12
+ Failure(1).fmap { |v| v - 1} # => Failure(0)
13
+
14
+
15
+ Threenum = Deterministic::enum {
16
+ Nullary()
17
+ Unary(:a)
18
+ Binary(:a, :b)
19
+ }
20
+
21
+ Deterministic::impl(Threenum) {
22
+ def sum
23
+
24
+ match {
25
+ Nullary() { 0 }
26
+ Unary(u) { u }
27
+ Binary(a, b) { a + b }
28
+ }
29
+ end
30
+
31
+ def +(other)
32
+ match {
33
+ Nullary() { other.sum }
34
+ Unary(a) { |this| this.sum + other.sum }
35
+ Binary(a, b) { |this| this.sum + other.sum }
36
+ }
37
+ end
38
+ }
39
+
40
+ describe Threenum do
41
+ it "works" do
42
+ expect(Threenum.Nullary + Threenum.Unary(1)).to eq 1
43
+ expect(Threenum.Nullary + Threenum.Binary(2, 3)).to eq 5
44
+ expect(Threenum.Unary(1) + Threenum.Binary(2, 3)).to eq 6
45
+ expect(Threenum.Binary(2, 3) + Threenum.Binary(2, 3)).to eq 10
46
+ end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deterministic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.0
4
+ version: 0.15.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Zolnierek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-21 00:00:00.000000000 Z
11
+ date: 2014-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -160,6 +160,7 @@ files:
160
160
  - spec/lib/deterministic/result/success_spec.rb
161
161
  - spec/lib/deterministic/result_spec.rb
162
162
  - spec/lib/enum_spec.rb
163
+ - spec/readme_spec.rb
163
164
  - spec/spec_helper.rb
164
165
  homepage: http://github.com/pzol/deterministic
165
166
  licenses:
@@ -210,4 +211,5 @@ test_files:
210
211
  - spec/lib/deterministic/result/success_spec.rb
211
212
  - spec/lib/deterministic/result_spec.rb
212
213
  - spec/lib/enum_spec.rb
214
+ - spec/readme_spec.rb
213
215
  - spec/spec_helper.rb