Live data from Hacker News

Destroy All Ifs – A Perspective from Functional Programming

degoes.net

71–80 of 226 posts

Re: Destroy All Ifs – A Perspective from Functional Programming

#71
Paul Blasucci had a good talk on Active Patterns (an F# language feature):

https://github.com/pblasucci/DeepDive_ActivePatterns

This feature allows to encapsulate conditional matching on arbitrary input and dispatching.

For those who know ML, it is making the concept of pattern matching extensible to any construct.

Re: Destroy All Ifs – A Perspective from Functional Programming

#72
i can't think of use of 'if' in a math function; however, if is implicitly used in input, say 0i 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.

Re: Destroy All Ifs – A Perspective from Functional Programming

#74

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.

Lots of math functions are defined with 'if' -- the absolute value, the Heaviside step function, etc.

Re: Destroy All Ifs – A Perspective from Functional Programming

#76
post #6

The idea that each type has its own control flow primitives is bothersome. It's taken over Rust: argv.nth(1) .ok_or("Please give at least one argument".to_owned()) .and_then(|arg| arg.parse:: ().map_err(|err| err.to_string())) .map(|n| 2 * n) I'm waiting for date.if_weekday(|arg| ...) Reading this kind of thing is hard. All those subexpressions are nameless, and usually comment-less. This isn't pure functional progra…

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()).

The method whose to_string method you want to reference isn't in scope. You need a function that calls the method on the argument it's called with.

Why add a feature for this - worse syntax, if you can just use an anonymous function?

Rust is already not the simplest of languages, adding further syntax and features of questionable benefit won't make the language any simpler or easier to understand.

Re: Destroy All Ifs – A Perspective from Functional Programming

#77
post #35

Earlier quoted context omitted.

Someone else addressed the details of your counter argument, but I'd like to respond to it generally. It seems like every time someone writes an article on how to write better code, there are responses about how it doesn't make sense when taken to some logical extreme, or some special case, as if that invalidates the argument. (FP techniques in particular seem to provoke this.) But code design is like other design di…

The article isn't balanced. It's suggesting that the direction of the refactoring is an unalloyed good, as I read it. I disagree. I've seen junior devs take this kind of stuff literally and over-apply it, like it's a religious ritual that they get a pious buzz from adhering to. I'd prefer people to think first before regurgitating what they most recently learned.

I agree wholeheartedly. I think the boolean blindness concept that's in the background of this article is incredibly important. But if you're going to propose an actual concrete solution, you need to assess whether it will be right all of the time, most of the time, or situationally (edit: and any of those answers is ok--it's fine to have a pattern that sometimes works if its presented as such). That requires looking at the ways it can go wrong. And this article just didn't do that.

Re: Destroy All Ifs – A Perspective from Functional Programming

#78
post #6

The idea that each type has its own control flow primitives is bothersome. It's taken over Rust: argv.nth(1) .ok_or("Please give at least one argument".to_owned()) .and_then(|arg| arg.parse:: ().map_err(|err| err.to_string())) .map(|n| 2 * n) I'm waiting for date.if_weekday(|arg| ...) Reading this kind of thing is hard. All those subexpressions are nameless, and usually comment-less. This isn't pure functional progra…

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 }

Re: Destroy All Ifs – A Perspective from Functional Programming

#80

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…

All valid points.

If you're going to do this sort of thing with much success, you really need to have a language with a fairly powerful type system. If function pointers are your only option for higher-order programming, I wouldn't even try. First class functions or interface polymorphism help, but I'd also want to have a language that makes it relatively easy to create (and enforce) types so that your extension points don't end up being overly generic.

Post reply on HN