1

I am new to JavaScript Functional programming. In code below, compose can't work without setInterval outside it and clear as the first argument also does't give the compose initial value.

So my question is how can compose work without the setInterval?

const clear = () => console.clear()
const f1 = () => 2
const log = message => console.log(message)

const compose = (...fns) =>
    arg =>
        fns.reduce(
            (composed, f) => f(composed),
            arg
        )

setInterval(
    compose(clear, f1, log), 1000
)

1
  • FYI, compose has nothing to do with this. Please refer to the tag wiki in the future for usage. Commented Dec 13, 2017 at 8:02

1 Answer 1

3

compose(...fns) returns a function. When used with setInterval, it is being called implicitly by the JavaScript engine.

If you want to use it directly, you can do something like:

const clear = () => console.clear()
const f1 = () => 2
const log = message => console.log(message)

const compose = (...fns) =>
    arg =>
        fns.reduce(
            (composed, f) => f(composed),
            arg
        )

compose(clear, f1, log)();

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

Comments

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.