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 Func.tap #107

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Add Func.tap #107

wants to merge 4 commits into from

Conversation

cprecioso
Copy link

@cprecioso cprecioso commented Jul 31, 2016

Description

tap applies a function only for its side effects and discards the result, returning the given argument.

Reasoning

tap would be really useful for use in big chains of pipes, compositions and partial application, without the syntactic overhead of a cascade in an explicit function.

Implementation

tap = (f, x) --> f x; return x

Example 1

In this example, we convert a date object with a month in human form (one-indexed) for use in moment.js, which requires zero-indexed months.

Before:

get-date # e.g. {year: 2016, month: 7, day: 31}
|> ->
  it.month--
  return it # We have to return the given argument to continue chaining
|> moment

Now:

get-date
|> tap (.month--) # :D
|> moment

Example 2

Here we use a console.log for debug purposes.

get-whatever-data!
|> process-that-data
|> tap (.1) >> console~log
|> keep-processing-that-data

@cprecioso
Copy link
Author

cprecioso commented Jul 31, 2016

The name comes from a Q Promise method that does the same but for promises (and is incredibly useful), though I'm not sure whether that's the best name for it.

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

Successfully merging this pull request may close these issues.

1 participant