Show HN: Parallel and/or with short-circuit in Elixir

2 points by rsrsrs86 ↗ HN
Hi, this can terminate when either branch of and/or terminates early and short-circuits the operator.

    def or_(task1, task2) do
      parent = self
      pid1 = spawn(fn -> send parent, task1.() end)
      pid2 = spawn(fn -> send parent, task2.() end)
      receive do
        false ->
          receive do
            result2 -> 
              Process.exit pid1, :kill
              Process.exit pid2, :kill
              result2
          end
        true -> 
          Process.exit pid1, :kill
          Process.exit pid2, :kill
          true
      end
    end
https://gist.github.com/roehst/e98a9b0703ceb75d85f7b212b60005ee

0 comments

[ 4.0 ms ] story [ 5.4 ms ] thread

No comments yet.