Live data from Hacker News

When if is just a function

ryelang.org

101–110 of 111 posts

Re: When if is just a function

#101

There's more to this that I'd absolutely love to see in a language, and I can't tell if rye supports. If you want an ergonomic `if` you need its scope aspects too. Consider this example x = ... if foo: y = ... else: x = ... y = ... Critical parts of the ergonomics are that a) in each branch, we have everything in scope that comes from b) in , we have everything in scope that was assigned or reassigned in the executed…

Sorry, but I don't understand the example. In Rye, as in REBOL all first level block evaluations (do, if, for, loop, ...) happend in current scope (context).

Rye has first class contexts and it's one of more exciting things to me about it, but I'm not sure it's related to what you describe above. More on it here:

https://ryelang.org/meet_rye/specifics/context/

One thing that pops out at the example above is that I wouldn't want to define a y inside if block, because after the if you have y defined or not defined at all, at least to my understanding of your code.

Rye is constant by default, most things don't change and you need less veriables because it's expression based and it has left to right "flow" option. So you only make variables where you really need them, which is less common and specific, it should be obvious why you need a variable.

Re: When if is just a function

#102
post #38

Earlier quoted context omitted.

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

You can't delete word binding in Rye. I will have to add some solution just for REPL, because sometimes you instantiate something and make a bug and need to reiterate, but in the language itself no.

Re: When if is just a function

#103

In R: > `if` = function(a, b) { print(a); print(b) } > if (2 (Rye also looks like a nice language, though!)

I used R for a while to do visualizations, but I never got to it's more homoiconic side, but I saw since then that it has it.

Can you also put code into { 6 }, like { print(6) }?

Does this overwrite R's if or how does the scoping in R work?

Re: When if is just a function

#104
post #76

Earlier quoted context omitted.

That is sort of like saying all visual art projects want to become "the million dollar / pixel homepage" so providing an empty canvas and full color palete will just enable people to create visual sewage because nothing stops them from doing so. I never programmed in a team, so my experience of programming is probably very different from yours. You probably want something like electric cattle fencing (if I borrow you…

That analogy may not be suitable for this case because value proposition between the aesthetics vs the function is different for visual art projects compared to software. There is also the maintainability factor where most aged software (especially the closed source ones in private sector) change maintainers every few years. Old maintainers most often lose access to the source code and become unreachable after leavin…

True, there is the "old maintainer" aspect, that differs.

But what exactly does this mean in relation that what are special forms in other languages are function calls in Rye?

Is the problem that you somebody could make their own control-structure-like functions? All these function calls have exactly the same evaluation rules, which is not something you can say about "special forms" in "normal" languages because special forms are exactly rules that break the regular evaluation rules.

Rye already has a lot of control-structure-like functions in it's standard library and many other functions that accept blocks of code and aren't related to control-structures. Yes, a "stupid" person can write "stupid" code in Rye, but you don't need much flexibility in any language to write stupid code.

I fully admit there are languages that are more suited for teams, and languages that are more suited for solo developers, for this reason.

That was also my point mentioning "million pixel website" because that "art" is directionless and crowdsourced and a painter can use the same pixels or even more flexible options to create beautiful images.

Re: When if is just a function

#105
post #99

The Trade-offs section doesn't list the biggest one. Secretly, all code wants to be spaghetti. You and your team have to put a conscious effort into prevent that from happening. Degrading the core of the language like this is like inoculating your homebrew with sewage and expecting it not to go wrong.

> degrading the core > experimental These ideas have been tried and tested for 60 years now and result in less spaghetti.

Yes, a lot of REBOL ideas, that Rye took, or various functional, homoiconic langauges implement (haskell, lips, clojure, scheme, Io, Factor) come from search for greater internal consistency than your ALGOL-derived status-quo languages provide. :)

So degrading the core doesn't make much sense if you are not more specific. This is the core.

Re: When if is just a function

#106

In R: > `if` = function(a, b) { print(a); print(b) } > if (2 (Rye also looks like a nice language, though!)

I used R for a while to do visualizations, but I never got to it's more homoiconic side, but I saw since then that it has it. Can you also put code into { 6 }, like { print(6) }? Does this overwrite R's if or how does the scoping in R work?

The `6` is the code, I just put the first thing that came to my mind there :D In my example, if you replace it with a more sophisticated code block, R will evaluate it and print out the result of the evaluation.

Typically, you'd want to parse the unevaluated code, though:

    > `if` = function(a, b) { print(substitute(a)); print(substitute(b)) }
    > if (foo) { print("bar") }
    foo
    {
        print("bar")
    }
It overwrites `if`, in the current scope, of course.

Re: When if is just a function

#107
post #70

Earlier quoted context omitted.

> 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

Lazy evaluation typically is a language-wide calling convention choice right? With that in mind I'm guessing the first-class block aspect of Rye might make it feel too loaded of a term to use to the author, because Rye's blocks-as-values are more flexible than just that. But maybe one mention of "this is kind of like lazy evaluation" would help.

Re: When if is just a function

#108
post #83

Earlier quoted context omitted.

> Python was used as an example because people know Python and reader can compare it to something known. I don't think you understood the point I made. My point is that Python supports conditional expressions for years. Support for conditional expressions already means that in Python indeed "if is just a function". Therefore, the whole premise of the article is null and void, and the article is thus pointless. This i…

If Python has special syntax for `if` and so makes if an expression, it doesn't mean that `if` is a function. If `if` is a function why don't you call it like other functions in Python? if( .... ) Functions are first class in Python now AFAIK. Can you assign `if` to a variable? Also, blog-posts is not just about `if`, is `for` a function in Python, `def`, `return`, `class`?

> If `if` is a function why don't you call it like other functions in Python?

The whole point is that yes, you can call it like a function, because Python supports it as an expression.

I seriously recommend you take the time to go through a Python tutoria.

> Also, blog-posts is not just about `if`, is `for` a function in Python, `def`, `return`, `class`?

Python supports list comprehension, so yes it is also about `for`.

Once you discover that Python supports conditional expressions and list comprehension, do you honestly believe the blog post has any merit? The blog post even shows a failure to do basic homework to verify what Python actually supports and not supports.

Re: When if is just a function

#109
post #3

Combinatory programing offers functional control flow. (Here is a straight forward explanation: https://blog.zdsmith.com/series/combinatory-programming.html ) I was inspired to write `better-cond` in Janet: (defn better-cond [& pairs] (fn [& arg] (label result (defn argy [f] (if (> (length arg) 0) (f ;arg) (f arg))) # naming is hard (each [pred body] (partition 2 pairs) (when (argy pred) (return result (if (function?…

This looks great, I'm still learning Janet and couldn't write it myself.

I only know about: https://github.com/Engelberg/better-cond in clojure which is different it adds syntax enhancement + control flow convenience.

Similar better-cond can be written in clojure too:

  (defn better-cond [& clauses]
    (fn [& args]
      (some (fn [[pred result]]
              (when (apply pred args)
                (if (fn? result)
                  (apply result args)
                  result)))
            (partition 2 clauses))))
But it's not composable as Janet's version, it will fail when mapped over, because it may return a plain value instead of a callable one. In Janet, all values can naturally participate in higher-order contexts due to its uniform treatment of callables, while in Clojure, only actual functions can be composed or mapped.

Re: When if is just a function

#110

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 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 = fals…

I think that part could use some improvement. References vs values should have different syntax, like C++. I think the interpreted languages and Java botched this.
Post reply on HN