Why functional programming should be the future of software development
-
It’s hard to learn, but your code will produce fewer nasty surprises
Won't you take me to func-y town?
-
It’s hard to learn, but your code will produce fewer nasty surprises
Won't you take me to func-y town?
Quote:
Back then, this was a radical idea, and many programmers resisted the loss of a statement that they had grown to rely on. The debate went on for more than a decade, but in the end, the GOTO went extinct, and no one today would argue for its return.
That's debatable. Here's some code from my Wordle solver that loops through the letters of a word, and if one of those letters matches a list of letters at that position which have been eliminated from the running it skips to the next word using a
goto
. I'm certain it could be done with another construct, but the time to figure out that other construct is a waste when this approach is so simple and directly mirrors the problem's solution:foreach (string str in currentPossibilities) { isPossibility = true; for (int i=0; iOf course I would never go back to the spaghetti-code `goto` use, and don't use it often. But when it comes to mind first, and is simple, use it and get on to the next problem.
-
Quote:
Back then, this was a radical idea, and many programmers resisted the loss of a statement that they had grown to rely on. The debate went on for more than a decade, but in the end, the GOTO went extinct, and no one today would argue for its return.
That's debatable. Here's some code from my Wordle solver that loops through the letters of a word, and if one of those letters matches a list of letters at that position which have been eliminated from the running it skips to the next word using a
goto
. I'm certain it could be done with another construct, but the time to figure out that other construct is a waste when this approach is so simple and directly mirrors the problem's solution:foreach (string str in currentPossibilities) { isPossibility = true; for (int i=0; iOf course I would never go back to the spaghetti-code `goto` use, and don't use it often. But when it comes to mind first, and is simple, use it and get on to the next problem.
I really can't stand the nay-sayer, consequently I have been very vocal about my, errmm... 3? goto statements in the last 20 years, haha! Not that I care much... I am just annoyed by the anti goto bigotry! ;P A few cool use for goto I can think of right away, one that doesn't break logical flow or understanding, is escaping multiple nested loops, ... also maybe goto in switch statement too? although that one might be confusing...
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
-
I really can't stand the nay-sayer, consequently I have been very vocal about my, errmm... 3? goto statements in the last 20 years, haha! Not that I care much... I am just annoyed by the anti goto bigotry! ;P A few cool use for goto I can think of right away, one that doesn't break logical flow or understanding, is escaping multiple nested loops, ... also maybe goto in switch statement too? although that one might be confusing...
A new .NET Serializer All in one Menu-Ribbon Bar Taking over the world since 1371!
goto theHeadOfTheClass;
Our Forgotten Astronomy | Object Oriented Programming with C++ | Wordle solver
-
Quote:
Back then, this was a radical idea, and many programmers resisted the loss of a statement that they had grown to rely on. The debate went on for more than a decade, but in the end, the GOTO went extinct, and no one today would argue for its return.
That's debatable. Here's some code from my Wordle solver that loops through the letters of a word, and if one of those letters matches a list of letters at that position which have been eliminated from the running it skips to the next word using a
goto
. I'm certain it could be done with another construct, but the time to figure out that other construct is a waste when this approach is so simple and directly mirrors the problem's solution:foreach (string str in currentPossibilities) { isPossibility = true; for (int i=0; iOf course I would never go back to the spaghetti-code `goto` use, and don't use it often. But when it comes to mind first, and is simple, use it and get on to the next problem.
Depending on the types, why not simply:
foreach (string str in currentPossibilities) {
isPossibility = !p1NonChar.Contains(str[0])
&& !p2NonChar.Contains(str[1])
&& !p3NonChar.Contains(str[2])
&& !p4NonChar.Contains(str[3])
&& !p5NonChar.Contains(str[4]);if (isPossibility) reducedPossibilities.Add(str);
}
If the non-char values are strings, and you're using .NET Framework which doesn't have the
Contains(char)
overload[^], you can useIndexOf
instead:foreach (string str in currentPossibilities) {
isPossibility = p1NonChar.IndexOf(str[0]) == -1
&& p2NonChar.IndexOf(str[1]) == -1
&& p3NonChar.IndexOf(str[2]) == -1
&& p4NonChar.IndexOf(str[3]) == -1
&& p5NonChar.IndexOf(str[4]) == -1;if (isPossibility) reducedPossibilities.Add(str);
}
The
&&
operator is short-circuiting, so it will stop testing when it reaches the first non-char.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Depending on the types, why not simply:
foreach (string str in currentPossibilities) {
isPossibility = !p1NonChar.Contains(str[0])
&& !p2NonChar.Contains(str[1])
&& !p3NonChar.Contains(str[2])
&& !p4NonChar.Contains(str[3])
&& !p5NonChar.Contains(str[4]);if (isPossibility) reducedPossibilities.Add(str);
}
If the non-char values are strings, and you're using .NET Framework which doesn't have the
Contains(char)
overload[^], you can useIndexOf
instead:foreach (string str in currentPossibilities) {
isPossibility = p1NonChar.IndexOf(str[0]) == -1
&& p2NonChar.IndexOf(str[1]) == -1
&& p3NonChar.IndexOf(str[2]) == -1
&& p4NonChar.IndexOf(str[3]) == -1
&& p5NonChar.IndexOf(str[4]) == -1;if (isPossibility) reducedPossibilities.Add(str);
}
The
&&
operator is short-circuiting, so it will stop testing when it reaches the first non-char.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
why not simply: ...
Did not know about those functions. If they are inline, great! And they can eliminate the boolean 'isPossibility' variable! Thanks for sharing!
Our Forgotten Astronomy | Object Oriented Programming with C++ | Wordle solver
-
It’s hard to learn, but your code will produce fewer nasty surprises
Won't you take me to func-y town?