Live data from Hacker News

Why I Prefer Functional Programming

morgenthum.dev

11–20 of 163 posts

Re: Why I Prefer Functional Programming

#11

I think this most effectively demonstrates why I like a lot of OOP: it can be verbose. This is example function is relatively illegible: alignCenter :: [String] -> [String] alignCenter xs = map (\x -> replicate (div (n - length x) 2) ' ' ++ x) xs where n = maximum (map length xs) One of the most verbose languages I've used, Objective C, has made this a best practice. Despite the brackets (which scare people off), it…

As a lisp programmer who never wrote in Haskell the snippet you quoted is crystal clear to me, while with explicit loops I always have to double check, because I am pattern matching the code "ah, a map or reduce was meant" but I still have to double check assignments and the abort condition (off by one, incrementing the wrong index etc).

Re: Why I Prefer Functional Programming

#12
What I dislike about functional programming is that when objects are not supported or they are avoided I have seen poorly documented and complex hashes/collections data structures that end up reinventing the oop wheel (and the problem the wheel was solving in the first place).

It feels like a false choice and you can have rich FP capabilities in a language that supports objects and relationships between objects.

Re: Why I Prefer Functional Programming

#13

I don't see why "Object Orientated Programming" requires for loops, over map / streams (which Java has). Is there a definition of "OOP" which requires using for/while? This doesn't really sure any OOP at all. A better example would be to show a case where OOP would be useful, say having a base class and deriving it several times (iostreams for example). Show me how FP does somewhere where (traditionally) OOP is consi…

It depends what you mean by "OOP". Generally most mainstreams programming languages are mixed: that is they support more than one paradigm (e.g. procedural, class based OOP, functional, etc).

Though I've noticed that programmers these days often equate OOP with classes. Therefore anything with classes is OOP whereas anything without isn't. This comes up a lot with languages, such as Rust, that are OOP but lack the class keyword.

Re: Why I Prefer Functional Programming

#14
I still don't get the point of immutability. Sure state change is a problem. How about a log like data structure that stores all modifications of the data?

I'm sticking to procedural coding for the next 10 years and I will try my best to unwash young coders from poop.

Re: Why I Prefer Functional Programming

#17

I think this most effectively demonstrates why I like a lot of OOP: it can be verbose. This is example function is relatively illegible: alignCenter :: [String] -> [String] alignCenter xs = map (\x -> replicate (div (n - length x) 2) ' ' ++ x) xs where n = maximum (map length xs) One of the most verbose languages I've used, Objective C, has made this a best practice. Despite the brackets (which scare people off), it…

To me this looks like perl golfing, but for some reason perl golfing is bad but writing extremely terse functional programs is not.

Personally I like to combine both approaches. In swift I would write something like this:

    let l = ["abc", "ab", "abcdef", "abcdefgh"]
    width = l.reduce(0, { max($0, $1.count) })
    let centered = l.map({
     (line: String) -> String in 
      var padding = (width - line.count) / 2
      return String(repeating:" ", count: padding) + line
    })

Re: Why I Prefer Functional Programming

#19

I think this most effectively demonstrates why I like a lot of OOP: it can be verbose. This is example function is relatively illegible: alignCenter :: [String] -> [String] alignCenter xs = map (\x -> replicate (div (n - length x) 2) ' ' ++ x) xs where n = maximum (map length xs) One of the most verbose languages I've used, Objective C, has made this a best practice. Despite the brackets (which scare people off), it…

But the example you posted has more to do with whether a language or author has the aesthetics of preferring succinctness and symbols (Perl-like) to words (Ruby-like).

Re: Why I Prefer Functional Programming

#20

I prefer functional programming because referentially transparent functions are easier to reason about and test. Brevity of control flow hasn't brought me much benefit.

Couldn't agree more! ... I just had to laugh to myself at how this, THIS is why functional programming is so... rarely adopted. Like, "referentially transparent functions"?! What happened to `website.run.now!`?!
Post reply on HN