jashkenas / jashkenas/underscore
Shorthand for piping a value through a bunch of functions
- Dominant language
- JavaScript
- Stars
- 27.3k
- Forks
- 5.4k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 1
Description
Let's say you want to pipe some value `a` through 3 functions `f1`, `f2`, `f3`, in that order. How would you do it?
Well, you can do it the "normal" way (pure JS):
```javascript
f3(f2(f1(a)))
```
Or you can use `_.compose()`:
```javascript
_.compose( f3
, f2
, f1
)(a)
```
But what I'd _really like_ to write is:
```javascript
_.pipe( a
, f1
, f2
, f3
)
```
Both JS's function composition syntax and `_.compose()` make me write the function list in the reverse order. Clojure has [threading macros](https://clojure.org/guides/threading_macros); in Elm, you could write this using the reverse apply operator:
```elm
a
|> f1
|> f2
|> f3
```
This `_.pipe()` function I'm suggesting would be a way of doing something similar in JS.
Contributor guide
Research direction
Start by reviewing the existing _.compose entry point and the surrounding utility tests. Determine how the proposed _.pipe(a, f1, f2, f3) should handle the value and function order, then add coverage showing the functions run in the requested sequence.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 55/100