lalka 0.2.0 → 0.3.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/README.md +11 -2
- data/lib/lalka.rb +34 -26
- data/lib/lalka/version.rb +1 -1
- 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: a2c8ed4e0c9d220a12dee163cd1bf8374345335d
|
4
|
+
data.tar.gz: 972691694aa8089be51371b55c1e19fcefacb395
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 81fc544ab2f9ba714e75d381ef1251dbbd60ae0209a21e33069a7b3c9f010216af8f158c95be505ef5f15a639e898b6b53a479e2c58154e6b92ffffdaaee2555
|
7
|
+
data.tar.gz: c605549c2e9b112d86358db31f0fcd4741d6284fe7a1025d1f56e35dddbe1776da3b1bbd781ea918f965a1b001c2cbe9170e9a4bd9ed1d89aa9c401d21c09387
|
data/README.md
CHANGED
@@ -12,6 +12,10 @@ TODO: Delete this and the text above, and describe your gem
|
|
12
12
|
t.resolve(value) # resolve task to a value
|
13
13
|
# or
|
14
14
|
t.reject(error) # reject task with an error
|
15
|
+
# or
|
16
|
+
t.try { 100 } # will resolve
|
17
|
+
# or
|
18
|
+
t.try { raise 'error' } # will reject
|
15
19
|
end
|
16
20
|
```
|
17
21
|
|
@@ -19,11 +23,13 @@ TODO: Delete this and the text above, and describe your gem
|
|
19
23
|
```ruby
|
20
24
|
resolved_task = Lalka::Task.resolve(value) # task which resolves to a value
|
21
25
|
rejected_task = Lalka::Task.reject(error) # task which rejects to a value
|
26
|
+
task = Lalka::Task.try { 100 } # will resolve
|
27
|
+
task = Lalka::Task.try { raise 'error' } # will reject
|
22
28
|
```
|
23
29
|
|
24
30
|
### fork:
|
25
31
|
```ruby
|
26
|
-
# fork is nonblocking, returns nil
|
32
|
+
# executes computation, fork is nonblocking, returns nil
|
27
33
|
task.fork do |t|
|
28
34
|
t.on_success do |value|
|
29
35
|
# do something with a value
|
@@ -37,7 +43,7 @@ TODO: Delete this and the text above, and describe your gem
|
|
37
43
|
|
38
44
|
### fork_wait:
|
39
45
|
```ruby
|
40
|
-
# fork_wait blocks and returns Either from "dry-monads" gem
|
46
|
+
# executes computation, fork_wait blocks and returns Either from "dry-monads" gem
|
41
47
|
task = Lalka::Task.resolve(99)
|
42
48
|
|
43
49
|
result = task.fork_wait do |t|
|
@@ -109,6 +115,9 @@ TODO: Delete this and the text above, and describe your gem
|
|
109
115
|
|
110
116
|
result = Lalka::Task.resolve(-> (x) { -> (y) { x + y } }).ap(task1).ap(task2).fork_wait
|
111
117
|
result # Right(100)
|
118
|
+
|
119
|
+
result = Lalka::Task.resolve(-> (x, y) { x + y }.curry).ap(task1).ap(task2).fork_wait
|
120
|
+
result # Right(100)
|
112
121
|
```
|
113
122
|
|
114
123
|
## Installation
|
data/lib/lalka.rb
CHANGED
@@ -13,12 +13,20 @@ module Lalka
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
alias of resolve
|
17
|
+
|
16
18
|
def reject(error)
|
17
19
|
new do |t|
|
18
20
|
t.reject(error)
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|
24
|
+
def try(&block)
|
25
|
+
new do |t|
|
26
|
+
t.try(&block)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
22
30
|
def id(internal)
|
23
31
|
internal.on_success { |v| v }
|
24
32
|
internal.on_error { |e| e }
|
@@ -92,22 +100,22 @@ module Lalka
|
|
92
100
|
Task.new do |t|
|
93
101
|
q = Queue.new
|
94
102
|
|
95
|
-
fork do |
|
96
|
-
|
97
|
-
q.push [:
|
103
|
+
other_task.fork do |other|
|
104
|
+
other.on_success do |value|
|
105
|
+
q.push [:arg, value]
|
98
106
|
end
|
99
107
|
|
100
|
-
|
108
|
+
other.on_error do |error|
|
101
109
|
q.push [:error, error]
|
102
110
|
end
|
103
111
|
end
|
104
112
|
|
105
|
-
|
106
|
-
|
107
|
-
q.push [:
|
113
|
+
fork do |this|
|
114
|
+
this.on_success do |fn|
|
115
|
+
q.push [:fn, fn]
|
108
116
|
end
|
109
117
|
|
110
|
-
|
118
|
+
this.on_error do |error|
|
111
119
|
q.push [:error, error]
|
112
120
|
end
|
113
121
|
end
|
@@ -139,15 +147,7 @@ module Lalka
|
|
139
147
|
end
|
140
148
|
end
|
141
149
|
|
142
|
-
class
|
143
|
-
def resolve(value)
|
144
|
-
@on_success.call(value)
|
145
|
-
end
|
146
|
-
|
147
|
-
def reject(error)
|
148
|
-
@on_error.call(error)
|
149
|
-
end
|
150
|
-
|
150
|
+
class InternalBase
|
151
151
|
def on_success(&block)
|
152
152
|
@on_success = block
|
153
153
|
nil
|
@@ -157,9 +157,25 @@ module Lalka
|
|
157
157
|
@on_error = block
|
158
158
|
nil
|
159
159
|
end
|
160
|
+
|
161
|
+
def try
|
162
|
+
resolve(yield)
|
163
|
+
rescue => e
|
164
|
+
reject(e)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
class InternalAsync < InternalBase
|
169
|
+
def resolve(value)
|
170
|
+
@on_success.call(value)
|
171
|
+
end
|
172
|
+
|
173
|
+
def reject(error)
|
174
|
+
@on_error.call(error)
|
175
|
+
end
|
160
176
|
end
|
161
177
|
|
162
|
-
class Internal
|
178
|
+
class Internal < InternalBase
|
163
179
|
def initialize(queue)
|
164
180
|
@queue = queue
|
165
181
|
end
|
@@ -173,13 +189,5 @@ module Lalka
|
|
173
189
|
result = @on_error.call(error)
|
174
190
|
@queue.push Dry::Monads.Left(result)
|
175
191
|
end
|
176
|
-
|
177
|
-
def on_success(&block)
|
178
|
-
@on_success = block
|
179
|
-
end
|
180
|
-
|
181
|
-
def on_error(&block)
|
182
|
-
@on_error = block
|
183
|
-
end
|
184
192
|
end
|
185
193
|
end
|
data/lib/lalka/version.rb
CHANGED