如何在 Elixir 中调用递归函数并行

How to call a recursive-Function parallel in Elixir

我是 Elixir 的新手,目前正在将算法从 C++ 重写到 Elixir。

我想以并行方式调用递归。最后应将所有结果加在一起。

我检查了文档中的 Task,因为有一些类似于 C++ 中存在的东西。但不幸的是我不能再进一步了。

使用我的代码,我得到以下异常

13:22:31.158 [error] GenServer #PID<0.964.0> terminating
** (Protocol.UndefinedError) protocol Enumerable not implemented for {:cont, {:cont, %{}}} of type Tuple, Flow attempted to convert the stage accumulator into events but failed, to explicit convert your current state into events use on_trigger/2. This protocol is implemented for the following type(s): Flow, Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
    (elixir 1.10.3) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir 1.10.3) lib/enum.ex:141: Enumerable.reduce/3
    (elixir 1.10.3) lib/enum.ex:3383: Enum.reverse/1
    (elixir 1.10.3) lib/enum.ex:2984: Enum.to_list/1
    (flow 1.0.0) lib/flow/materialize.ex:642: anonymous fn/3 in Flow.Materialize.build_trigger/1
    (flow 1.0.0) lib/flow/map_reducer.ex:87: Flow.MapReducer.done_status/4
    (flow 1.0.0) lib/flow/map_reducer.ex:35: Flow.MapReducer.handle_cancel/3
    (gen_stage 1.0.0) lib/gen_stage.ex:2096: GenStage.noreply_callback/3
Last message: {:DOWN, #Reference<0.1582286220.1330905089.229281>, :process, #PID<0.963.0>, :normal}
State: {%{#Reference<0.1582286220.1330905089.229281> => nil}, %{done?: false, producers: %{#Reference<0.1582286220.1330905089.229281> => #PID<0.963.0>}, trigger: #Function<2.13930487/3 in Flow.Window.Global.materialize/5>}, {0, 12}, {:cont, {:cont, %{}}}, #Function<4.74501935/4 in Flow.Materialize.build_reducer/2>}

13:22:31.158 [error] GenServer #PID<0.948.0> terminating
** (stop) exited in: GenStage.close_stream(%{})
    ** (EXIT) an exception was raised:
        ** (Protocol.UndefinedError) protocol Enumerable not implemented for {:cont, {:cont, %{}}} of type Tuple, Flow attempted to convert the stage accumulator into events but failed, to explicit convert your current state into events use on_trigger/2. This protocol is implemented for the following type(s): Flow, Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
            (elixir 1.10.3) lib/enum.ex:1: Enumerable.impl_for!/1
            (elixir 1.10.3) lib/enum.ex:141: Enumerable.reduce/3
            (elixir 1.10.3) lib/enum.ex:3383: Enum.reverse/1
            (elixir 1.10.3) lib/enum.ex:2984: Enum.to_list/1
            (flow 1.0.0) lib/flow/materialize.ex:642: anonymous fn/3 in Flow.Materialize.build_trigger/1
            (flow 1.0.0) lib/flow/map_reducer.ex:87: Flow.MapReducer.done_status/4
            (flow 1.0.0) lib/flow/map_reducer.ex:35: Flow.MapReducer.handle_cancel/3
            (gen_stage 1.0.0) lib/gen_stage.ex:2096: GenStage.noreply_callback/3
    (gen_stage 1.0.0) lib/gen_stage/stream.ex:160: GenStage.Stream.close_stream/1
    (elixir 1.10.3) lib/stream.ex:1415: Stream.do_resource/5
    (elixir 1.10.3) lib/stream.ex:880: Stream.do_transform/5
    (elixir 1.10.3) lib/enum.ex:605: Enum.count/1
    (pva 0.1.0) langfordSequence.ex:47: anonymous fn/4 in LangfordSequence.langfordSequence_p/2
    (flow 1.0.0) lib/flow/materialize.ex:620: Flow.Materialize."-build_reducer/2-lists^foldl/2-0-"/3
    (flow 1.0.0) lib/flow/materialize.ex:620: anonymous fn/5 in Flow.Materialize.build_reducer/2
    (flow 1.0.0) lib/flow/map_reducer.ex:59: Flow.MapReducer.handle_events/3
Last message: {:"$gen_consumer", {#PID<0.947.0>, #Reference<0.1582286220.1330905090.228864>}, [[1, 0, 0, 0, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 0, 1]]}
State: {%{#Reference<0.1582286220.1330905090.228864> => nil}, %{done?: false, producers: %{#Reference<0.1582286220.1330905090.228864> => #PID<0.947.0>}, trigger: #Function<2.13930487/3 in Flow.Window.Global.materialize/5>}, {0, 12}, %{}, #Function<4.74501935/4 in Flow.Materialize.build_reducer/2>}
** (exit) exited in: GenStage.close_stream(%{})
    ** (EXIT) exited in: GenStage.close_stream(%{})
        ** (EXIT) an exception was raised:
            ** (Protocol.UndefinedError) protocol Enumerable not implemented for {:cont, {:cont, %{}}} of type Tuple, Flow attempted to convert the stage accumulator into events but failed, to explicit convert your current state into events use on_trigger/2. This protocol is implemented for the following type(s): Flow, Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
                (elixir 1.10.3) lib/enum.ex:1: Enumerable.impl_for!/1
                (elixir 1.10.3) lib/enum.ex:141: Enumerable.reduce/3
                (elixir 1.10.3) lib/enum.ex:3383: Enum.reverse/1
                (elixir 1.10.3) lib/enum.ex:2984: Enum.to_list/1
                (flow 1.0.0) lib/flow/materialize.ex:642: anonymous fn/3 in Flow.Materialize.build_trigger/1
                (flow 1.0.0) lib/flow/map_reducer.ex:87: Flow.MapReducer.done_status/4
                (flow 1.0.0) lib/flow/map_reducer.ex:35: Flow.MapReducer.handle_cancel/3
                (gen_stage 1.0.0) lib/gen_stage.ex:2096: GenStage.noreply_callback/3
    (gen_stage 1.0.0) lib/gen_stage/stream.ex:160: GenStage.Stream.close_stream/1
    (elixir 1.10.3) lib/stream.ex:1415: Stream.do_resource/5
    (elixir 1.10.3) lib/stream.ex:880: Stream.do_transform/5
    (elixir 1.10.3) lib/enum.ex:605: Enum.count/1

此致

protocol Enumerable not implemented for {:cont, {:cont, %{}}} of type Tuple

例如:

iex(1)> t = {1, {:a, :b, :c}}
{1, {:a, :b, :c}}


iex(2)> Enum.any?(t, fn x -> x==1 end)
** (Protocol.UndefinedError) protocol Enumerable not implemented for {1, {:a, :b, :c}}
    (elixir) lib/enum.ex:1: Enumerable.impl_for!/1
    (elixir) lib/enum.ex:141: Enumerable.reduce/3
    (elixir) lib/enum.ex:349: Enum.any?/2