Class: Lalka::Task
- Inherits:
-
Object
- Object
- Lalka::Task
- Defined in:
- lib/lalka.rb
Overview
TODO: Invalidate resolve and reject at the same time
Class Method Summary collapse
- .id(internal) ⇒ Object
- .reject(error) ⇒ Object
- .resolve(value) ⇒ Object (also: of)
- .try(&block) ⇒ Object
Instance Method Summary collapse
- #ap(other_task) ⇒ Object
- #bind(*args, &block) ⇒ Object (also: #chain, #flat_map)
- #fork {|internal| ... } ⇒ Object
- #fork_wait ⇒ Object
-
#initialize(&block) ⇒ Task
constructor
A new instance of Task.
- #map(*args, &block) ⇒ Object (also: #fmap)
Constructor Details
#initialize(&block) ⇒ Task
Returns a new instance of Task.
38 39 40 |
# File 'lib/lalka.rb', line 38 def initialize(&block) @computation = block end |
Class Method Details
.id(internal) ⇒ Object
30 31 32 33 |
# File 'lib/lalka.rb', line 30 def id(internal) internal.on_success { |v| v } internal.on_error { |e| e } end |
.reject(error) ⇒ Object
18 19 20 21 22 |
# File 'lib/lalka.rb', line 18 def reject(error) new do |t| t.reject(error) end end |
.resolve(value) ⇒ Object Also known as: of
12 13 14 15 16 |
# File 'lib/lalka.rb', line 12 def resolve(value) new do |t| t.resolve(value) end end |
.try(&block) ⇒ Object
24 25 26 27 28 |
# File 'lib/lalka.rb', line 24 def try(&block) new do |t| t.try(&block) end end |
Instance Method Details
#ap(other_task) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/lalka.rb', line 105 def ap(other_task) Task.new do |t| atom = Concurrent::Atom.new(M.Right(fn: M.None(), arg: M.None())) atom.add_observer do |_, _, either| if either.right? value = either.value value[:fn].bind { |fn| value[:arg].fmap(fn) }.fmap do |result| t.resolve(result) atom.delete_observers end else error = either.value t.reject(error) atom.delete_observers end end fork do |this| this.on_success do |fn| atom.swap(fn) { |either, fn| either.bind { |struct| M.Right(struct.merge(fn: M.Some(fn))) } } end this.on_error do |error| atom.swap(error) { |either, error| either.bind { M.Left(error) } } end end other_task.fork do |other| other.on_success do |arg| atom.swap(arg) { |either, arg| either.bind { |struct| M.Right(struct.merge(arg: M.Some(arg))) } } end other.on_error do |error| atom.swap(error) { |either, error| either.bind { M.Left(error) } } end end end end |
#bind(*args, &block) ⇒ Object Also known as: chain, flat_map
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/lalka.rb', line 79 def bind(*args, &block) block = function_from_arguments(*args, &block) Task.new do |t| fork do |this| this.on_success do |first_value| other_task = block.call(first_value) other_task.fork do |other| other.on_success do |second_value| t.resolve(second_value) end other.on_error do |error| t.reject(error) end end end this.on_error do |error| t.reject(error) end end end end |
#fork {|internal| ... } ⇒ Object
56 57 58 59 60 61 |
# File 'lib/lalka.rb', line 56 def fork internal = InternalAsync.new yield internal internal.call(&@computation) nil end |
#fork_wait ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/lalka.rb', line 42 def fork_wait queue = Queue.new internal = Internal.new(queue) if block_given? yield internal else Task.id(internal) end internal.call(&@computation) queue.pop end |
#map(*args, &block) ⇒ Object Also known as: fmap
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lalka.rb', line 63 def map(*args, &block) block = function_from_arguments(*args, &block) Task.new do |t| fork do |this| this.on_success do |value| t.resolve(block.call(value)) end this.on_error do |error| t.reject(error) end end end end |