3 ms·
It would be silly to use a pipeline for x |> foo(). What's nice is being able to write: def main_loop(%Game{} = game) do game |> get_move()
by AlchemistCamp 1y ago
It would be silly to use a pipeline for x |> foo(). What's nice is being able to write:
def main_loop(%Game{} = game) do
game
|> get_move()
|> play_move()
|> win_check()
|> end_turn()
end
instead of the much harder to read:
def main_loop(%Game{} = game)
end_turn(win_check(play_move(get_move(game))))
end
For an example with multiple parameters, this pipeline:
schema
|> order_by(^constraint)
|> Repo.all()
|> Repo.preload(preload_opts)
would be identical to this:
Repo.preload(Repo.all(order_by(schema, ^constraint)), preload_opts)
To address your question above,
> if `foo(y)` actually gives you a function to apply to `x` prob you should write `x |> foo(y)()`
If foo(y) returned a function, then to call it with x, you would have to write foo(y).(x) or x |> foo(y).(), so the syntax around calling the anonymous function isn't affected by the pipe. Also, you're not generally going to be using pipelines with functions that return functions so much as with functions that return data which is then consumed as the first argument by the next function in the pipeline. See my previous comment on this thread for more on that point.
There's no inconsistency or ambiguity in the pipeline operator's behavior. It's just syntactic sugar that's handy for making your code easier to read.