Live data from Hacker News

Destroy All Ifs – A Perspective from Functional Programming

degoes.net

151–160 of 226 posts

Re: Destroy All Ifs – A Perspective from Functional Programming

#151
post #5

Define true as a lambda taking two lazy values that returns the first, and false as one that returns the second, and you can turn all booleans into lambdas with no increase in code clarity. The straw man in the post - talking about a case-sensitive matcher that selectively called one of two different functions based on a boolean - is indeed trivially converted into calling a single function passed as an argument, but…

#destroyallifs

#notallifs

Re: Destroy All Ifs – A Perspective from Functional Programming

#153
post #139

Earlier quoted context omitted.

It's true. Personally, I took that to be a bit tongue in cheek. I would compare it to "GOTO Considered Harmful"-- where the author wants you to imagine a world without such a technique in order to expand your abilities. (Even though there are probably edge cases where such usage is justifiable.)

The stance is rather different though - "GOTO Considered Harmful" as a phrase is both inviting a discussion and making a limited statement. "Destroy all ifs" is definitive; the argument is over at the end of the phrase and there will be no negotiation or concessions. I know that this is trivial in this case, but I think it would help discourse in the world generally if we could move away from this kind of position ta…

Absolutely.

Simply replacing "ifs" with anything else.

I think it's clear how this is simply juvenile hyperbolic invective.

Re: Destroy All Ifs – A Perspective from Functional Programming

#154
post #151
post #5

Define true as a lambda taking two lazy values that returns the first, and false as one that returns the second, and you can turn all booleans into lambdas with no increase in code clarity. The straw man in the post - talking about a case-sensitive matcher that selectively called one of two different functions based on a boolean - is indeed trivially converted into calling a single function passed as an argument, but…

#destroyallifs #notallifs

#carefulwiththoseifseugene

Re: Destroy All Ifs – A Perspective from Functional Programming

#155

Earlier quoted context omitted.

The annoying part there is the repeated "|x| x.". Rust should have syntax to reference a method of an object, instead of having to write a wrapper. So it'd look like .map_err(???.to_string()).

Groovy and Kotlin use the implicit "it" parameter for lambdas that take just one parameter, which is very convenient: listOf(1, 2, 3, 4).filter { it % 2 == 0 }

Scala allows underscores, and sequential underscores refer to the next element, so you can do e.g.

    list(1, 2, 3, 4).reduce(_ + _) == 10

Re: Destroy All Ifs – A Perspective from Functional Programming

#156
post #137

Earlier quoted context omitted.

Another alternative is "table-oriented programming", where you define the "classes" and "methods" as an m-by-n structure of code pointers; to add either "methods" or "classes", you would just add a new row/column to the table along with the appropriate code definitions. and because "expression problem" is not a very catchy name. It's also not particularly descriptive either, but the page mentions that it's a form of…

Is that you, TopMind? I used to be on Wiki years ago too...

As a side note, I was pretty surprised that the name isn't as in "I have a great mind" but rather "I have a table oriented programming mind". At first, I had a knee jerk "wow arrogant" reaction and then felt guilty when I realised!

Re: Destroy All Ifs – A Perspective from Functional Programming

#157

Earlier quoted context omitted.

The annoying part there is the repeated "|x| x.". Rust should have syntax to reference a method of an object, instead of having to write a wrapper. So it'd look like .map_err(???.to_string()).

It does. This could have been written .map_err(ToString::to_string) as well. Works just fine with methods.

Why can't it be written as:

    .map_err(to_string)
When using the lambda the type is inferred, so why should there be a need for the ToString?

Re: Destroy All Ifs – A Perspective from Functional Programming

#158
post #11

Earlier quoted context omitted.

Most of this should be obviated when the ? operator is ready. But until then, there is no primitive for 'work on the type you wrapped in Option, short-circuiting and returning None at the first sign of failure', so it has to be done in the library.

If this is an accepted idiom, people will be using it for years to come. Sometimes only to be cool. With "?" and "try!()", Rust is sort of emulating exceptions in a weird way.

I like it, because you get somehow the best of both worlds, on one side you're getting explicit error handling and on the other the convenience of exceptions, that you can just "raise" them and they propagate upwards in the calling stack.

Re: Destroy All Ifs – A Perspective from Functional Programming

#159
post #127

Problem is, a decision has to be made somewhere about which function to pass into that "if-free" block of code. The if-like decision has just moved elsewhere. That is a win if it reduces duplication: if a lambda can be decided upon and then used in several places, that's better than making the same Boolean decision in those several places. Programs that are full of function indirection aren't necessarily easier to un…

In a language like Haskell you wouldn't want to prove the absence of recursion, but that all recursions in the program fit into a handful of patterns. (Eg 'structural-recursion' or 'tail-recursion'.) Some type systems are strong enough to put that kind of analysis / constraints directly into the language. (Haskell might already be strong enough with GADTs and other language extensions enabled.) In any case, the Adden…

Tee hee, Haskell doesn't have tail recursion (e.g. foldl takes linear space), and structural recursion in Haskell isn't guaranteed to terminate (e.g. if you're given an infinite list).

If I were in charge of developing a safety critical system, and someone came to me with a proposal to write it in Haskell, I'd be very skeptical.

Re: Destroy All Ifs – A Perspective from Functional Programming

#160

i can't think of use of 'if' in a math function; however, if is implicitly used in input, say 0 i see a lot of loop though, summation is so a double integral is loop within loop. i can't think a code analogue with derivative fta, i take that if in function body makes an ugly code.

That's because you are thinking of continuous functions. You have actually gave an example of a non-continuous one, it's just that you think of it as two functions connected with input conditional.

Other replier already gave you some common example, but let me add another one: signum(x), which returns if number is negative, positive or zero.

Post reply on HN