6

I'm reading through a book that is discussing built in JavaScript functions compose() and pipe() for combining functions in functional style programming.

One of the examples seems to suggest running the following as front-end code:

const shuffle =
    (deck, randomizer) => {
        const doShuffle = pipe(
            addRandom(randomizer),
            sortByRandom,
            map(card => card.card)
        );
        return doShuffle(deck);
    };

But when I try running it in the Google Developer console I get back an error saying:

Uncaught ReferenceError: pipe is not defined

I've also tried in Firefox with the same results. What version of JavaScript is needed to make use of these functions?

Am I right to think these functions can only be used with node.js or some kind of pre-compiler like Babel?

Note: I'm running Ubuntu and have tried Chromium and Firefox.

2
  • 3
    pipe and compose are not native JavaScript functions. It sounds to me like you were reading a functional programming doc from a library like Ramda (ramdajs.com). Just include that library in your page, and your code will work. Commented Oct 15, 2019 at 18:51
  • @PhilipKirkbride In fact it is, but you clearly haven't conflated the two, so I retract my comment! Commented Oct 15, 2019 at 23:43

1 Answer 1

8

pipe and compose aren't native functions. You'll have to add them to your javascript document in order to utilize them. This is an ES6 pipe function that you can use in modern browsers:

const pipe = (...fns) => x => fns.reduce((v, f) => f(v), x)

Here's a compose function:

const compose = (...fns) => x => fns.reduceRight((v, f) => f(v), x);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It was the case that he did define these somewhere else in the document, but the text was written in a way which made them seem like native functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.