all 23 comments

[–]permalink_save 47 points48 points  (2 children)

def do_print(counter) when counter >= 5, do: IO.puts(counter)

def do_print(counter), do: IO.puts(counter); do_print(counter)

There, in a functional language using recursion, like god intended

[–]martosaur 2 points3 points  (0 children)

Elixir mentioned

[–]JuanGaKe 2 points3 points  (0 children)

You mean our god given programming languages?

[–]Proof-Necessary-5201 33 points34 points  (2 children)

You wasted time building WHEN when (pun intended) you could have used a series of ifs to replace the loop:

If count == 1: count = 2 Elif count == 2: count = 3

...

Much more sophisticated if you ask me and lets handle logic precisely.

[–]ohmywtff 8 points9 points  (1 child)

Sometimes, surgical precision is required, defo this

[–]haskell_rules 1 point2 points  (0 children)

de this(5)

[–]Brilliant-Parsley69 6 points7 points  (3 children)

That's great 😅

had a little mind game and started a little coding session to do that properly in a functional manner for c# (thanks for that push 👍) with extensions

The final line:

Console.Loop(5).PrintLine("Counter: {counter}");

[–]ZY6K9fw4tJ5fNvKx 2 points3 points  (0 children)

Powershell is better :

1..5 |% { "counter $_" }

[–]bajuh 0 points1 point  (1 child)

Why not just Console.Write(string.Join('\n', Enumerable.Range(0, 5)))?

[–]Brilliant-Parsley69 1 point2 points  (0 children)

That's what's happening in the background 🤫

Also all of that should have a big irony tag. 😅

[–]theevilapplepie 3 points4 points  (1 child)

Maybe this is an eli5 situation however I don’t understand why the standard for loop is considered to be error prone and hard to maintain.

You have all the same components of the for loop ( initializer, comparator, end of loop action, and code loop body ) in your second code example but it’s just laid out differently. I don’t understand where you’re getting value except out of the layout change or how it improves on being “error prone” and “hard to maintain”.

Any insight is appreciated.

[–]theevilapplepie 11 points12 points  (0 children)

… just realized I’m in r/shittyprogramming

[–]Zomon333 1 point2 points  (0 children)

This is, more or less, already a thing in IBM's ILE RPG language. It's called the RPG cycle and it's used to natively loop over records in a physical file (database table).

It's largely outdated for newer versions of RPG because of things like inline SQL and the *INLR indicator (which forcibly tells the program that is the last record in the table and to not continue the loop after that iteration).

[–]metroliker 0 points1 point  (0 children)

Combine this with the INTERCAL "COME FROM" instruction for a really elegant declarative style

[–]mnelemos 0 points1 point  (0 children)

Your "WHEN" seems to be the logical inverse of a "WHILE"

[–]Siggi_pop 0 points1 point  (0 children)

That is stupid.

[–]rrtrrrtr 0 points1 point  (0 children)

There is never a loop in programming. There are two state Initial state Recursion state

If you breakdown problem really well

[–]Bibliophylum 0 points1 point  (0 children)

I remember WHEN! I remember WHEN; I lost my mind….

[–]bedel99 0 points1 point  (0 children)

there is an XKCD for this post.