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

rryofs0p  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(152)

我是新的酏剂,我目前正在重写一个算法从C到酏剂。
我想以并行的方式调用一个递归。最后所有的结果应该加在一起。
我在文档中检查了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

顺祝商祺

vh0rcniy

vh0rcniy1#

Tuple类型的{:cont, {:cont, %{}}}未实现协议可枚举
例如:

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

相关问题