You can write this
echo "Edward,Chris,Gabriel,Erika,David" | tr , \n | grep -e "^E" | xargs | tr \ ,
Or create a reusable alias
alias getNamesStartingWithE="tr , \\\n | grep -e \"^E\" | xargs | tr \ ,"
And use it
echo "Elaine,Jerry,George,Eric" | getNamesStartingWithE
Suppose we have two functions f
and g
Composing them means we first compute y = g(x)
, and then
use y
to compute z = f(y)
We can do this
const y = g(x);
const z = f(y);
Or if those two functions are useful together frequently
const foo = y => f(g(y));
const z = foo(x);
const z2 = foo(x2);
// ... etc
Or
const foo = compose(f, g);
// foo is the `f` of `g` of something
In Haskell
foo = f . g
compose
consumes the functions right-to-left, pipe
does it left-to-right
Which reads a bit closer to what we are used to in both JavaScript and Bash
const foo = pipe(g, f);
// foo applies `g`, then `f` to something
ESNext Proposal: The Pipeline Operator
It's a backwards-compatible way of streamlining chained function calls in a readable, functional manner, and provides a practical alternative to extending built-in prototypes.
Your functions need to be:
- Predictable.
const double = x => x * x;
const toUpper = x => x.toUpperCase();
- Unary
const map = fn => list => list.map(fn);
const filter = fn => list => list.filter(fn);
const doubleAll = map(double);
// doubleAll is a function ready to receive an array
Not all functions are unary.
Partial application of a variadic function into a fixed number of values
const replace = curry(
(exp, replacement, str) => str.replace(exp, replacement)
);
const replaceWords = replace(/\w+/g);
const bananafy = replaceWords(
(word, i) => word.length === 6
? 'banana'
: word
);
const bananasburg = bananafy(`
Four score and seven years ago our fathers brought forth on this continent, a
new nation, conceived in Liberty, and dedicated to the proposition that all
men are created equal.
`);
Containers or wrappers with shared methods following a specific set of rules
-
Ramda Like lodash, but all functions are curried and pointfree.
-
Folktale A standard library for functional programming in JavaScript
-
Crocks A collection of well known Monadic Containers for your utter enjoyment.
-
Professor Frisby's Mostly Adequate Guide to Functional Programming A full guide for FP in JS.
-
The Elm Architecture Shh, just read it. Trust me.
-
Learn You a Haskell for Great Good A beginner's guide.