Public API
Monadic.@monadic
— Macromyflatmap(f, x) = Iterators.flatten(map(f, x))
iteratorresult = @monadic map myflatmap begin
x = 1:3
y = [1, 6]
@pure x + y
end
collect(iteratorresult) # [2, 7, 3, 8, 4, 9]
The @monadic
macro allows a syntax where containers and other contexts are treated rather as values, hiding the respective well-defined side-effects. Each line without @pure is regarded as a container, each line with @pure is treated as normal code which should be inlined.
For the example above you see that the side-effect semantics of iterables are the same as for nested for loops. With the crucial distinction, that the @monadic syntax has a return value.
@monadic
can take a third argument wrapper
in order to first apply a custom function before executing the @monadic code.
mywrapper(n::Int) = 1:n
mywrapper(any) = any
myflatmap(f, x) = Iterators.flatten(map(f, x))
iteratorresult = @monadic map myflatmap mywrapper begin
x = 3
y = [1, 6]
@pure x + y
end
collect(iteratorresult) # [2, 7, 3, 8, 4, 9]
Monadic.@pure
— MacroMark code to contain non-monadic code.
This can be thought of as generalized version of pure
typeclass.