Live data from Hacker News

When if is just a function

ryelang.org

61–70 of 111 posts

Re: When if is just a function

#61
post #38
post #37

Earlier quoted context omitted.

Or redefine the language provided 'if' statement, in the case that one wanted to do so.

You can't really redefine if because everything is a constant, but you can define if in your own context yes.

In Tcl you can redefine "if", or even delete it entirely if you're crazy enough :-)

Re: When if is just a function

#63
post #51

Earlier quoted context omitted.

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.

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

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

Re: When if is just a function

#64
Interesting that this article makes no mention of eager vs lazy evaluation - isn’t a big reason that if, for etc has to be special forms in an eagerly evaluated language that their arguments need to be lazily evaluated, which of course, deviates from the rule? Also, lazy evaluation is achieved in an eagerly evaluated language as simply wrapping a block of code in a function, which makes lazy evaluation isomorphic with the contents of the article

Re: When if is just a function

#65
post #14

This is interesting, but I'm not convinced it's better than the python it's being compared to. Memorizing and understanding the behavior of functions that perform control flow seems no easier than memorizing and understanding hardcoded syntax/keywords. The additional flexibility of making everything a first-class citizen allows people to write code that is too clever for its own good. I could be wrong but I think the…

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.

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

Re: When if is just a function

#66
When everything is an expression, like rust (I think), that's enough. Most of the control flow are used as statements, but if needed, they can be used directly as expression, and you get basically all the advantages. After seeing how effective this is, using a language that doesn't have this feature can be really annoying.

On the other hand, I would like to explore "when arithmetics is just a function". I think Elm does this well: operators are just functions with two arguments that can be written as "1 + 2", the familiar way, or "(+) 1 2". Then you can compose it like "map ((+) 2)" (currying) so you get a function that adds 2 to every item of a list, and so on.

Re: When if is just a function

#67
post #64

Interesting that this article makes no mention of eager vs lazy evaluation - isn’t a big reason that if, for etc has to be special forms in an eagerly evaluated language that their arguments need to be lazily evaluated, which of course, deviates from the rule? Also, lazy evaluation is achieved in an eagerly evaluated language as simply wrapping a block of code in a function, which makes lazy evaluation isomorphic wit…

> You might wonder: “Won’t the block execute immediately when passed as an argument?” Here’s the key insight: in Rye, code blocks { ... } are values. They don’t evaluate until you explicitly tell them to.

Re: When if is just a function

#68
post #53
post #44

I own the 'if' package on npm, which I wrote to be functions that can replace the if keyword, making no use of the if keyword in its definition.

interesting. Give us an example of it's usage ... or how you implemented it?

I looked at the code - it uses ternary expressions.

Re: When if is just a function

#69
post #54

For lambda calculus, the motto is "when everything is a function". The boolean true is the function λx.λy.x, while false is λx.λy.y. If b then x else y then simply becomes b x y. In a functional language like Haskell that is basically a typed lambda calculus with lots of syntactic sugar, we can replicate this with: type MyBool a = a -> a -> a myTrue :: MyBool a myTrue = \x y -> x myFalse :: MyBool a myFalse = \x y ->…

  yourIf == myId

Re: When if is just a function

#70
post #64

Interesting that this article makes no mention of eager vs lazy evaluation - isn’t a big reason that if, for etc has to be special forms in an eagerly evaluated language that their arguments need to be lazily evaluated, which of course, deviates from the rule? Also, lazy evaluation is achieved in an eagerly evaluated language as simply wrapping a block of code in a function, which makes lazy evaluation isomorphic wit…

> You might wonder: “Won’t the block execute immediately when passed as an argument?” Here’s the key insight: in Rye, code blocks { ... } are values. They don’t evaluate until you explicitly tell them to.

You're correct, that is lazy evaluation. The entire article talks about lazy evaluation without mentioning it, which was my point
Post reply on HN