Using pipe() and compose() functions in JavaScript? - Stack Overflow

I'm reading through a book that is discussing built in JavaScript functions pose() and pipe() for

I'm reading through a book that is discussing built in JavaScript functions pose() and pipe() for bining 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-piler like Babel?

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

I'm reading through a book that is discussing built in JavaScript functions pose() and pipe() for bining 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-piler like Babel?

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

Share Improve this question asked Oct 15, 2019 at 18:47 Philip KirkbridePhilip Kirkbride 22.9k39 gold badges131 silver badges237 bronze badges 2
  • 3 pipe and pose are not native JavaScript functions. It sounds to me like you were reading a functional programming doc from a library like Ramda (ramdajs.). Just include that library in your page, and your code will work. – Traveling Tech Guy Commented Oct 15, 2019 at 18:51
  • @PhilipKirkbride In fact it is, but you clearly haven't conflated the two, so I retract my ment! – msanford Commented Oct 15, 2019 at 23:43
Add a ment  | 

1 Answer 1

Reset to default 8

pipe and pose 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 pose function:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745216847a4617064.html

相关推荐

  • Using pipe() and compose() functions in JavaScript? - Stack Overflow

    I'm reading through a book that is discussing built in JavaScript functions pose() and pipe() for

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信