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.
33 34 35 |
# File 'lib/lalka.rb', line 33 def initialize(&block) @computation = block end |
Class Method Details
.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
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 152 153 154 155 156 |
# File 'lib/lalka.rb', line 101 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
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/lalka.rb', line 75 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
52 53 54 55 56 57 |
# File 'lib/lalka.rb', line 52 def fork internal = InternalAsync.new yield internal internal.call(&@computation) nil end |
#fork_wait ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lalka.rb', line 37 def fork_wait queue = Queue.new internal = Internal.new(queue) if block_given? yield internal else internal.on_success { |v| v } internal.on_error { |e| e } end internal.call(&@computation) queue.pop end |
#map(*args, &block) ⇒ Object Also known as: fmap
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/lalka.rb', line 59 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 |