最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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);

发布评论

评论列表(0)

  1. 暂无评论