`flat_map/2` is needed
dobashi opened this issue · 2 comments
dobashi commented
I wanna use Either/Maybe with flat_map function like this.
func1(2)
|> flat_map(& func2(&1))
|> flat_map(& func3(&1))
All functions return Either.Right|Left. I defined flat_map/2 in my project.
defp flat_map(%Right{right: x}, f), do: f.(x)
defp flat_map(%Left{left: x}, f), do: x
But I think Either/Maybe have to define definst ... def flat_map()
. I tried to add flat_map to Witchcraft.Functor and define then in Algae.Either but it occurs an error.
== Compilation error in file lib/algae/either.ex ==
** (UndefinedFunctionError) function Witchcraft.Functor.Proto.Algae.Either.Left.map/2 is undefined or private, but the behaviour Witchcraft.Functor.Proto expects it to be present
Witchcraft.Functor.Proto.Algae.Either.Left.map(%Algae.Either.Left{left: [266, <<29, 23, 14, 62>>, -1.7183673469387755, <<48, 56, 69, 24>>]}, #Function<2.93944398/1 in Witchcraft.Functor.Property.composition/1>)
Does anyone have an idea to add flat_map/2 to Either|Maybe?
hariroshan commented
you don't have to define your own functions. flat_map/2
is same as bind/2
or chain/2
which is defined in Witchcraft.Chain
. Most of the functions are already defined in Witchcraft.
simply look read the witchcraft docs.
example
>> import Witchcraft.Chain
>> Algae.Either.Right.new(100) |> bind(& Algae.Either.Right.new(&1 + 10))
>> %Algae.Either.Right{right: 110}
dobashi commented
Oh I see. You're right. Sorry for late reply.