yopt 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +1 -0
- data/README.md +18 -10
- data/docs/README.template.md +18 -10
- data/lib/yopt.rb +5 -3
- data/lib/yopt/version.rb +1 -1
- data/yopt.gemspec +6 -4
- metadata +12 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b2b1eaa4ca004e729d902f06b93fca46dd526175
|
4
|
+
data.tar.gz: 920c701a92b2f4a74698b376e81035d049d9bc74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fe9eed7ba9150bef20625b851109cfa872ed1b961ef3e9552126c200f841e45faf978b6d9f2539e516b236ef9a42ae224ccd260363430f75be15a63ae2f3d73
|
7
|
+
data.tar.gz: caff9b6961f3217f9d1894bda7bf30227026d86ff28fe043a20da52c05955c9837b93b9ce058a70d1e73583ba5b0294b9e18e1773190c898b73d49d7795f624d
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -48,8 +48,16 @@ none.each {|value| puts value} # does not print anything
|
|
48
48
|
We can safely retrieve the optional value by passing a default value to `Option#get_or_else`
|
49
49
|
|
50
50
|
```ruby
|
51
|
-
some.get_or_else 0 # returns 42
|
52
|
-
none.get_or_else 0 # returns 0
|
51
|
+
some.get_or_else {0} # returns 42
|
52
|
+
none.get_or_else {0} # returns 0
|
53
|
+
```
|
54
|
+
|
55
|
+
Notice how we are passing a block rather than an argument. This makes the evaluation of the default value lazy. In other words the block will only be evaluated if the caller in None.
|
56
|
+
|
57
|
+
This gives us the possibility to react in a special way to a None value without breaking the API fluency, e.g.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
opt.each {|v| do_something_with(v)}.get_or_else {log_failure}
|
53
61
|
```
|
54
62
|
|
55
63
|
We can also filter the optional value depending on how it evaluates against a block via `Option#select`
|
@@ -60,7 +68,7 @@ none.select {|value| value < 0} # returns None
|
|
60
68
|
some.select {|value| value > 0} # returns Some(42)
|
61
69
|
```
|
62
70
|
|
63
|
-
We can easily turn any object into an Option by means of `Option.call` - aliased to `Option
|
71
|
+
We can easily turn any object into an Option by means of `Option.call` - aliased to `Option[]` for convenience.
|
64
72
|
For instance, this is useful when dealing with functions that might return `nil` to express the absence of a result.
|
65
73
|
|
66
74
|
```ruby
|
@@ -87,7 +95,7 @@ returns `c` if `opt` is `None`, and `f.(c, opt)` otherwise.
|
|
87
95
|
|
88
96
|
This is a shortcut to
|
89
97
|
```
|
90
|
-
opt.map{|v| f.(c,v)}.get_or_else
|
98
|
+
opt.map{|v| f.(c,v)}.get_or_else {c}`
|
91
99
|
```
|
92
100
|
|
93
101
|
|
@@ -137,15 +145,15 @@ email_opt.zip(captcha_opt).each{|(email,_)| send_pass_recovery(email)}
|
|
137
145
|
|
138
146
|
`Option#zip` returns `None` if any of the arguments is `None` or if the caller is `None`
|
139
147
|
```ruby
|
140
|
-
Yopt::None.zip Option
|
141
|
-
Option
|
142
|
-
Option
|
148
|
+
Yopt::None.zip Option[42] # None
|
149
|
+
Option[42].zip Yopt::None # None
|
150
|
+
Option[42].zip Option[0], Yopt::None, Option[-1] # None
|
143
151
|
```
|
144
152
|
|
145
153
|
When both the caller and all the arguments are defined then `zip` collects all the values in an Array wrapped in a `Yopt::Some`
|
146
154
|
|
147
155
|
```ruby
|
148
|
-
Option
|
156
|
+
Option[42].zip Option[0], Option["str"] # Some([42, 0, "str"])
|
149
157
|
```
|
150
158
|
|
151
159
|
|
@@ -177,11 +185,11 @@ opt.filter(&is_positive).map {|v| Math.log(v)}
|
|
177
185
|
|
178
186
|
Some (None?) might enjoy a comparison with Haskell's [Maybe](https://hackage.haskell.org/package/base/docs/Data-Maybe.html). Here is how the Data.Maybe API translate to Yopt.
|
179
187
|
```ruby
|
180
|
-
maybe default f opt -> opt.map(&f).get_or_else
|
188
|
+
maybe default f opt -> opt.map(&f).get_or_else {default}
|
181
189
|
isJust opt -> not opt.empty?
|
182
190
|
isNothing opt -> opt.empty?
|
183
191
|
fromJust opt -> opt.get
|
184
|
-
fromMaybe default opt -> opt.get_or_else default
|
192
|
+
fromMaybe default opt -> opt.get_or_else {default}
|
185
193
|
listToMaybe list -> Option.ary_to_type list
|
186
194
|
maybeToList opt -> opt.to_a
|
187
195
|
catMaybes listOfOptions -> listOfOptions.flatten
|
data/docs/README.template.md
CHANGED
@@ -48,8 +48,16 @@ none.each {|value| puts value} # does not print anything
|
|
48
48
|
We can safely retrieve the optional value by passing a default value to `Option#get_or_else`
|
49
49
|
|
50
50
|
```ruby
|
51
|
-
some.get_or_else 0 # returns 42
|
52
|
-
none.get_or_else 0 # returns 0
|
51
|
+
some.get_or_else {0} # returns 42
|
52
|
+
none.get_or_else {0} # returns 0
|
53
|
+
```
|
54
|
+
|
55
|
+
Notice how we are passing a block rather than an argument. This makes the evaluation of the default value lazy. In other words the block will only be evaluated if the caller in None.
|
56
|
+
|
57
|
+
This gives us the possibility to react in a special way to a None value without breaking the API fluency, e.g.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
opt.each {|v| do_something_with(v)}.get_or_else {log_failure}
|
53
61
|
```
|
54
62
|
|
55
63
|
We can also filter the optional value depending on how it evaluates against a block via `Option#select`
|
@@ -60,7 +68,7 @@ none.select {|value| value < 0} # returns None
|
|
60
68
|
some.select {|value| value > 0} # returns Some(42)
|
61
69
|
```
|
62
70
|
|
63
|
-
We can easily turn any object into an Option by means of `Option.call` - aliased to `Option
|
71
|
+
We can easily turn any object into an Option by means of `Option.call` - aliased to `Option[]` for convenience.
|
64
72
|
For instance, this is useful when dealing with functions that might return `nil` to express the absence of a result.
|
65
73
|
|
66
74
|
```ruby
|
@@ -87,7 +95,7 @@ returns `c` if `opt` is `None`, and `f.(c, opt)` otherwise.
|
|
87
95
|
|
88
96
|
This is a shortcut to
|
89
97
|
```
|
90
|
-
opt.map{|v| f.(c,v)}.get_or_else
|
98
|
+
opt.map{|v| f.(c,v)}.get_or_else {c}`
|
91
99
|
```
|
92
100
|
|
93
101
|
|
@@ -137,15 +145,15 @@ email_opt.zip(captcha_opt).each{|(email,_)| send_pass_recovery(email)}
|
|
137
145
|
|
138
146
|
`Option#zip` returns `None` if any of the arguments is `None` or if the caller is `None`
|
139
147
|
```ruby
|
140
|
-
Yopt::None.zip Option
|
141
|
-
Option
|
142
|
-
Option
|
148
|
+
Yopt::None.zip Option[42] # None
|
149
|
+
Option[42].zip Yopt::None # None
|
150
|
+
Option[42].zip Option[0], Yopt::None, Option[-1] # None
|
143
151
|
```
|
144
152
|
|
145
153
|
When both the caller and all the arguments are defined then `zip` collects all the values in an Array wrapped in a `Yopt::Some`
|
146
154
|
|
147
155
|
```ruby
|
148
|
-
Option
|
156
|
+
Option[42].zip Option[0], Option["str"] # Some([42, 0, "str"])
|
149
157
|
```
|
150
158
|
|
151
159
|
|
@@ -177,11 +185,11 @@ opt.filter(&is_positive).map {|v| Math.log(v)}
|
|
177
185
|
|
178
186
|
Some (None?) might enjoy a comparison with Haskell's [Maybe](https://hackage.haskell.org/package/base/docs/Data-Maybe.html). Here is how the Data.Maybe API translate to Yopt.
|
179
187
|
```ruby
|
180
|
-
maybe default f opt -> opt.map(&f).get_or_else
|
188
|
+
maybe default f opt -> opt.map(&f).get_or_else {default}
|
181
189
|
isJust opt -> not opt.empty?
|
182
190
|
isNothing opt -> opt.empty?
|
183
191
|
fromJust opt -> opt.get
|
184
|
-
fromMaybe default opt -> opt.get_or_else default
|
192
|
+
fromMaybe default opt -> opt.get_or_else {default}
|
185
193
|
listToMaybe list -> Option.ary_to_type list
|
186
194
|
maybeToList opt -> opt.to_a
|
187
195
|
catMaybes listOfOptions -> listOfOptions.flatten
|
data/lib/yopt.rb
CHANGED
@@ -47,11 +47,13 @@ module Yopt
|
|
47
47
|
raise Option.invalid_argument('an Option', other) unless other.is_a? Option
|
48
48
|
if empty? then other else self end
|
49
49
|
end
|
50
|
-
def get_or_else
|
51
|
-
|
50
|
+
def get_or_else
|
51
|
+
raise ArgumentError, 'missing block' unless block_given?
|
52
|
+
return self.get unless empty?
|
53
|
+
yield
|
52
54
|
end
|
53
55
|
def or_nil
|
54
|
-
get_or_else nil
|
56
|
+
get_or_else {nil}
|
55
57
|
end
|
56
58
|
def inspect() to_s end
|
57
59
|
private
|
data/lib/yopt/version.rb
CHANGED
data/yopt.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = Yopt::VERSION
|
9
9
|
spec.authors = ["lorenzo.barasti"]
|
10
10
|
|
11
|
-
spec.summary = %q{Scala-inspired Options for the idiomatic Rubyist
|
12
|
-
spec.description = %q{
|
13
|
-
spec.homepage = "
|
11
|
+
spec.summary = %q{Scala-inspired Options for the idiomatic Rubyist}
|
12
|
+
spec.description = %q{The Option type models the possible absence of a value. It lets us deal with the uncertainty related to such a value being there without having to resort to errors or conditional blocks.}
|
13
|
+
spec.homepage = "http://lbarasti.github.io/yopt"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
@@ -18,7 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.required_ruby_version = '>= 2.0'
|
22
|
+
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.10"
|
22
24
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
-
spec.add_development_dependency "minitest"
|
25
|
+
spec.add_development_dependency "minitest", "~> 5.8"
|
24
26
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yopt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- lorenzo.barasti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-02-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -42,18 +42,19 @@ dependencies:
|
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '5.8'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
55
|
-
description:
|
56
|
-
|
54
|
+
version: '5.8'
|
55
|
+
description: The Option type models the possible absence of a value. It lets us deal
|
56
|
+
with the uncertainty related to such a value being there without having to resort
|
57
|
+
to errors or conditional blocks.
|
57
58
|
email:
|
58
59
|
executables: []
|
59
60
|
extensions: []
|
@@ -79,7 +80,7 @@ files:
|
|
79
80
|
- lib/yopt.rb
|
80
81
|
- lib/yopt/version.rb
|
81
82
|
- yopt.gemspec
|
82
|
-
homepage:
|
83
|
+
homepage: http://lbarasti.github.io/yopt
|
83
84
|
licenses:
|
84
85
|
- MIT
|
85
86
|
metadata: {}
|
@@ -91,7 +92,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
92
|
requirements:
|
92
93
|
- - ">="
|
93
94
|
- !ruby/object:Gem::Version
|
94
|
-
version: '0'
|
95
|
+
version: '2.0'
|
95
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
97
|
requirements:
|
97
98
|
- - ">="
|
@@ -102,6 +103,6 @@ rubyforge_project:
|
|
102
103
|
rubygems_version: 2.4.5
|
103
104
|
signing_key:
|
104
105
|
specification_version: 4
|
105
|
-
summary: Scala-inspired Options for the idiomatic Rubyist
|
106
|
+
summary: Scala-inspired Options for the idiomatic Rubyist
|
106
107
|
test_files: []
|
107
108
|
has_rdoc:
|