Earlier quoted context omitted.
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(repeati…
Personally, I’d replace width = l.reduce(0, { max($0, $1.count) }) with l.map { $0.count }.max()
Why I Prefer Functional Programming
121–130 of 163 posts
Re: Why I Prefer Functional Programming
#122Earlier quoted context omitted.
I think this is a question that's orthogonal to OOP-versus-functional-programming, but is rather about programming style. Haskell programmers like being terse, but there's also nothing stopping you from writing Haskell like this: alignCenter :: [String] -> [String] alignCenter lines = let maxLineLength = maximum (map length lines) in [ leftPadding ++ line | line You could have verbose and easy-to-read functional code…
Honestly...how is that more readable? It's more verbose sure but the first example is much more readable and straightforward. Here, you have to remember Haskell's list comprehension syntax and you have to scan up and down a few times to keep track of the variables.
But bad naming practices have nothing to do with FP per se. Although it does seem to be fashionable in part of the FP community to have everything as terse as possible including variable names.
Re: Why I Prefer Functional Programming
#123Earlier quoted context omitted.
Haskell is perfectly capable of expressing imperative algorithms and mutable state. Such algorithms are still expressed using pure functions, pure functions that return IO actions.
And x86 assembler is perfectly capable of expressing pure functions. Such algorithms are still expressed using subroutines, subroutines that save and restore all state to its start state. The question isn't whether a language can express an algorithm, Church and Turing already proved that. The question is whether the language can do so scalably. We want to see nontrivial examples of FP applied to messy, side-effect-l…
Re: Why I Prefer Functional Programming
#124Earlier quoted context omitted.
I haven't touched C++ for several years at this point, so bear with me a bit, but in the category of "just really annoying, not really restrictive", there is the fact that you have to constantly re-type the types for everything, like `MyType x = new MyType()`, but my understanding is that has been addressed by the `auto` keyword. Is there some equivalent to a monad in C++? As in, something that will "pollute" the fun…
You almost never use `new` in modern C++, and you would never use it as you suggest because it wouldn't compile, you would simply say: MyType x; C++ is nothing at all like Java.
https://docs.microsoft.com/en-us/cpp/mfc/class-library-overv...
https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-a...
http://docwiki.embarcadero.com/Libraries/Rio/en/Main_Page
https://docs.unrealengine.com/en-US/Programming/Introduction...
https://docs.cryengine.com/display/SDKDOC2/Home
There is the "Modern C++" described at C++ conference talks and then what everyone else uses.
Re: Why I Prefer Functional Programming
#125Earlier quoted context omitted.
Sorry, you'd need to use the MyType* x to use the `new` keyword; it's been awhile. It doesn't really deter from my point though; in order to heap-allocate something you have to use a pointer, and you end up doing something not that dissimilar from Java . > C++ is nothing at all like Java. Is that supposed to be a joke? Java was marketed specifically towards C++ engineers...
> in order to heap-allocate something you have to use a pointer Usually the heap allocation is hidden behind a handle.
I would already be happy when I am allowed to re-write some of it into C++11.
Re: Why I Prefer Functional Programming
#126Earlier quoted context omitted.
Obligatory C#: var input = new string[] { "abc", "ab", "abcdef", "abcdefgh" }; var maxLength = input.Select(e => e.Length).Max(); var output = input.Select(e => { var padding = (maxLength - e.Length) / 2; return new String(' ', padding) + e; }); Most modern `oop` languages these days all support functional constructs and achieve the same thing in same amount of code & style. Language and oop/functional style are not…
This looks like the Java if the Java was written by someone who knows Java: List alignText(List texts) { int maxLength = texts.stream().mapToInt(String::length).max().orElse(0); return texts.stream().map(text -> { var spaceCount = (maxLength - text.length()) / 2; return " ".repeat(spaceCount) + text; }).collect(Collectors.toList()); }
Re: Why I Prefer Functional Programming
#127I 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(repeati…
fun alignLines(lines: List): List {
val maxLength = lines.map { it.length }.max()!!
return lines.map { " ".repeat((maxLength - it.length) / 2) + it }
}Re: Why I Prefer Functional Programming
#128I like FP, but cautious as there aren't many big applications (open source or private) that are written that way. Am I wrong?
Also, Walmart has been using Clojure "at scale" since before 2015. And based on a related tweet, the project at that time had 66 modules, 3000+ files, 560,000+ LOC (of Clojure). Last measure, Walmart was the largest US retailer at $374 billion annual revenue. I'd say their dependence on Clojure is a pretty good validation of the language's capability.
Re: Why I Prefer Functional Programming
#129I 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…
This opinion is flameworthy and stereotypes heavily: what seems to happen in FP is that the overall community is substantially math-IQ smarter than the imperative languages. Alas, that ALSO means the overall community loses social-IQ in the process. This leads to: 1) higher barrier to entry for the general programmers, in language semantics, documentation, examples... 2) a tendency to overabstract, underdocument, and…
Re: Why I Prefer Functional Programming
#130Earlier quoted context omitted.
This opinion is flameworthy and stereotypes heavily: what seems to happen in FP is that the overall community is substantially math-IQ smarter than the imperative languages. Alas, that ALSO means the overall community loses social-IQ in the process. This leads to: 1) higher barrier to entry for the general programmers, in language semantics, documentation, examples... 2) a tendency to overabstract, underdocument, and…
Maybe we just need to have more math training for people who want to become Computer Programmers.