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
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.
32 33 34 |
# File 'lib/lalka.rb', line 32 def initialize(&block) @computation = block end |
Class Method Details
.reject(error) ⇒ Object
17 18 19 20 21 |
# File 'lib/lalka.rb', line 17 def reject(error) new do |t| t.reject(error) end end |
.resolve(value) ⇒ Object Also known as: of
11 12 13 14 15 |
# File 'lib/lalka.rb', line 11 def resolve(value) new do |t| t.resolve(value) end end |
.try(&block) ⇒ Object
23 24 25 26 27 |
# File 'lib/lalka.rb', line 23 def try(&block) new do |t| t.try(&block) end end |
Instance Method Details
#ap(other_task) ⇒ Object
96 97 98 99 100 101 102 103 104 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 146 147 148 149 150 151 |
# File 'lib/lalka.rb', line 96 def ap(other_task) Task.new do |t| mutex = Mutex.new completed = false either = M.Right([M.None(), M.None()]) complete_task = lambda do |&block| mutex.synchronize do return if completed either = block.call(either) if either.right? (e_fn, e_arg) = either.value e_fn.bind { |fn| e_arg.fmap(fn) }.fmap do |value| t.resolve(value) completed = true end else error = either.value t.reject(error) completed = true end end end fork do |this| this.on_success do |fn| complete_task.call do |either| either.bind { |_, e_arg| M.Right([M.Some(fn), e_arg]) } end end this.on_error do |error| complete_task.call do |either| either.bind { M.Left(error) } end end end other_task.fork do |other| other.on_success do |arg| complete_task.call do |either| either.bind { |e_fn, _| M.Right([e_fn, M.Some(arg)]) } end end other.on_error do |error| complete_task.call do |either| either.bind { M.Left(error) } end end end end end |
#bind(*args, &block) ⇒ Object Also known as: chain, flat_map
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lalka.rb', line 70 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
47 48 49 50 51 52 |
# File 'lib/lalka.rb', line 47 def fork internal = InternalAsync.new yield internal internal.call(&@computation) nil end |
#fork_wait ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lalka.rb', line 36 def fork_wait queue = Queue.new internal = Internal.new(queue) internal.on_success { |v| v } internal.on_error { |e| e } internal.call(&@computation) queue.pop end |
#map(*args, &block) ⇒ Object Also known as: fmap
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/lalka.rb', line 54 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 |