onyx-lang icon indicating copy to clipboard operation
onyx-lang copied to clipboard

Feature suggestion: suffix while

Open stugol opened this issue 9 years ago • 5 comments

Strongly suggest a value-collecting suffix while:

text = "abcdef"
results = text.shift while !text.empty?
puts results            -- ["a", "b", "c", "d", "e", "f"]
results = text.shift while !text.empty?
puts results            -- [ ]

Also a suffix until, which would run at least once.

stugol avatar Feb 21 '16 23:02 stugol

Hrrm, this discussion has been up in an issued in crystal, and I was very much for ditching suffix whiles because of how different expectations people seem to have on them.

I've only instinctively reached for the construct once, coding in the parser, and when getting the error was like "ah, right, my bad, hehe", copy n paste + one enter and done. It's not too much to fuzz about.

Now, of course, this is a bit of a different matter, with the collecting involved. When it comes to that I really think it's something that should be implemented in user space "functional" method-style. Then it's crystal clear to the code reader what to expect, and the code will be as terse, and easier to combine and chain.

I instincitvely feel this is not a candidate for "core'ing" -, but please, if you will, give some slighty more real-world(ish) examples along with "method-style" implementation to compare with so it's easier to evaluate the level of generality and usefulness of it?

ozra avatar Feb 24 '16 20:02 ozra

It can't really be implemented in user space.

I can't think of a good real-world example. I'm not very good at doing that. But lisp has a value-collecting loop.

stugol avatar Feb 24 '16 23:02 stugol

We'll have to let this linger then, nothing gets time spent on if it hasn't got a real use case, and benefits it.

ozra avatar Feb 25 '16 15:02 ozra

FWIW, it seems LiveScript supports a value-capturing suffix while ;)

stugol avatar Mar 23 '16 03:03 stugol

while and for, and booooy have they caused problems! Involuntary list generations when used last in functions, etc. One change in a program for a customer once made the web-app turn in to a crawl. For the life of me couldn't figure why. Took me hours until I looked at the generated code and realized what I had "really done". Of course I was a bit new to the quirks of the language back then, but I do not find it to be good constructs.

Imperative looping constructs should be seen as a luxury power construct imo. Expanding on them for more complex flows instead of promoting functional coding in those cases will not lead to better programs. The less looping magic used and the more functional, the better. I do however find loops to be necessary when really needed for low level high performance code (which I often work on).

ozra avatar Mar 23 '16 12:03 ozra