Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Kernel.tap/2 and Kernel.then/2 #10602

Closed
josevalim opened this issue Dec 29, 2020 · 4 comments
Closed

Add Kernel.tap/2 and Kernel.then/2 #10602

josevalim opened this issue Dec 29, 2020 · 4 comments

Comments

@josevalim
Copy link
Member

tap/2 is for side-effects in pipelines, then/2 is for anonymous functions:

def tap(x, fun) do
  fun.(x)
  x
end

def then(x, fun), do: fun.(x)

We should probably improve the error message in Macro.pipe to link to refer to Kernel.then/2.

@v0idpwn
Copy link
Contributor

v0idpwn commented Dec 29, 2020

Can I take this one? Bonus: are these the definitive names?

@Eiji7
Copy link
Contributor

Eiji7 commented Dec 29, 2020

Sorry @v0idpwn, I started working on Kernel.then/2 before you asked. 😈

@lukaszsamson
Copy link
Contributor

I wonder if those functions should handle awaitable %Task{} specially.

Task.async(fn -> some_io_bound_op() end)
|> tap(fn res -> do_sth(res) end)
|> then(fn res -> Task.async(fn -> do_sth(res) end) end)
|> Task.await
# awaited do_sth(res)

@josevalim
Copy link
Member Author

Please see mailing list discussion @lukaszsamson. Overall, It is not the goal of these functions to be polymorphic, modules could provide variants but I don’t think there are reasonable implementations for those in task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants