Remix.run Logo
hshdhdhehd a day ago

I agree. In any somewhat functional language (I.e. all the mainstream ones) you can wrap "if" in a function if you please.

E.g.

    function funif (b, f) {
       return (b && f())
    }

If you want to do clever stuff. I never feel the need as I would rather abstract over bigger things.
andriamanitra 20 hours ago | parent | next [-]

You may not want a fresh scope for control flow as you often want to use variables from the outer scope inside the if statement. Imagine you wanted to do something like this with your if statement implemented with a function (this is how the syntax would look like using a block argument in Ruby):

    state = "inactive"
    if_func(condition) {
        state = "active"
        activate_button.disabled = true
        deactivate_button.disabled = false
    }
In many languages you would need to wrap `state` in something that can be passed by reference, and make the function take multiple parameters. For example in JavaScript it would turn into something like this mess:

    let state = ["inactive"];
    if_func(condition, ({state, activate_button, deactivate_button}) => {
        state[0] = "active";
        activate_button.disabled = true;
        deactivate_button.disabled = false;
    }, {state, activate_button, deactivate_button});
middayc a day ago | parent | prev [-]

You can do it, but that is not how the (default) control structures work in those languages. There is usually also some syntax cost.

hshdhdhehd 20 hours ago | parent [-]

Thats a good point. Idiomatics are important and not following them makes incompatible code.