6 ms·
Because it doesn't have enough advantages to justify the added complexity.
by xyzzy4 11y ago
Because it doesn't have enough advantages to justify the added complexity.
- the_cat_kittles 11y agoextremely testable, easier to reason about, automatically dry, parallelizable, automatically reusable, memoizable, composable, can be analyzed with some theoretical systems, and most important- you get stuff that works better (in my experience)
- voltagex_ 11y agoThat's great, but how do I write a functional version of https://github.com/voltagex/junkcode/blob/master/CSharp/spotprices/spotprices/Program.cs https://github.com/voltagex/junkcode/blob/master/CSharp/spot...? All this does is pull a list of "spot instance" prices from AWS EC2, sort them and filter them. I rewrote this from a python script that became too complex. The last time I looked at Haskell, it needed ~700MB of dependencies before I could even get started. Edit: Clojure looks like a good way to start, although https://github.com/mrowe/clj-aws-ec2 https://github.com/mrowe/clj-aws-ec2 isn't really complete.
- crusso 11y agoEither that or take a look at Elixir. All the 9 9's reliability and distributability of Erlang with a more Ruby-like syntax.
- tel 11y agoSorting and filtration are super bread-and-butter FP. The "hard" bits are the IO to get the results and to print them out. Solve the first with a nice HTTP client lib like Wreq, bring in the IO language to do it, finish it off using putStrLn to print. It's neither the same mechanism as you're used to not difficult.
- voltagex_ 11y agoLINQ in C# and lambda in Python are as close as I get to FP at the moment. I may have another look at F# and someone else in this thread has reminded me to look at Elixir.
- eru 11y ago> The last time I looked at Haskell, it needed ~700MB of dependencies before I could even get started. ghc is big, indeed. There are smaller alternate compilers and interpreters (that no one uses). Compiled executables are usually smaller than ghc, which is probably the biggest Haskell programs ever written.
- denibertovic 11y agoI suspect the 700MB dependency is Haskell Platform. Which you don't need and is not considered a good way to install Haskell. Just GHC and Cabal (package manager, sort of ) are nowhere near there. Or even better, use Stack. See how large the binaries are.
- rtpg 11y agoThere are ways to write things that are similar with Haskell. Just like there are design patterns for functional techniques in imperative languages, there are design patterns for imperative techniques in functional language.... So for example, here you could make a nice little EC2 DSL that would get your stuff (for the actual Haskellers: Free Monad): spotPrices :: Region -> EC2 [SpotPrice] spotPrices = GetSpotPrice -- DSL Leaf, executed by runEC2 spotPricesForAllRegions :: [Region] -> EC2 [SpotPrice] spotPricesForRegions regions = concat <$> MapM spotPrices regions Your spot price update function becomes something even simpler now: runEC2 :: EC2 a -> EC2Config -> IO a runEC2 = stuff -- this is where you call whatever EC2 -- lib is available updateSpotPriceFile :: [Region] -> IO [SpotPrice] updateSpotPriceFile regions = do spotPrices <- runEC2 (spotPricesForAllRegions regions) (myEC2Configuration) writeToFile (toJSON spotPrices) "spotprices.json" return spotPrices I'm not going to port all of it, but with a more functional version you could avoid having to keep on making EC2 clients all over the place, and you could keep your config in one place. The problem is that it's pretty obtuse if you don't know the right design patterns. There's a myth in FP circles that design patterns are for OOP only ("we have functions, we don't need observer patterns!"), but it's a myth. But if you get good, you can write some pretty concise code + get all the goodies. Haskell has the whole GHC thing, and also the issue of lack of good library documentation (or lack of libraries on certain stuff)...
- voltagex_ 11y agoThanks for this. The syntax is definitely taking a bit to get my head around, but it looks very tidy.
- hythloday 11y agoSomething like this in Scala: it doesn't have the json read/writing but only because I lost interest, not because it's intrinsically hard. https://gist.github.com/hythloday/6246a352c9e9d0150a4b https://gist.github.com/hythloday/6246a352c9e9d0150a4b
- voltagex_ 11y agoThanks! It really doesn't look that different to something in Python.
- codygman 11y agoIf I get some extra time I'd love to post a build able Haskell clone of this.
- xyzzy4 11y agoFunctional programming also emphasizes the use of recursion, which nearly always makes an algorithm much harder to understand.
- jackfoxy 11y agoNo, you may have to think about it a little more, especially when first getting acquainted with recursion, but the recursive solution is likely to be less complex and more robust. It's the repetition of a simpler idea, and so there are fewer ways to screw-up the implementation.
- xyzzy4 11y agoI disagree. Recursion makes a stack trace that should be 10 functions deep into one that's 1000 functions. It's difficult to debug and follow the code around, or even to understand the code if someone else wrote it. Also recursive functions tend to spiral out of control and have ridiculous exponential run times.
- eru 11y ago> I disagree. Recursion makes a stack trace that should be 10 functions deep into one that's 1000 functions. Recursion has nothing to do with stack traces. Stack are one way to implement some version of recursion in functions. Some other versions of recursion in functions can be implemented with loops. And, of course, not only function can be recursive, but data structures as well. (And a few other things.) You can traverse these data structures any way you feel like. > It's difficult to debug and follow the code around, or even to understand the code if someone else wrote it. I have that problem with loops (and mutation) much more than with recursion. Different things make different people's heads hurt. > Also recursive functions tend to spiral out of control and have ridiculous exponential run times. Oh, that's an artifact of how you've been taught (or self-taught). Loops are a special case that only works well for simple tasks. Recursion is more general and can express the simple case in a simple way. Of course, if you only reach for recursion in the complicated cases, all uses of recursion you see will be complicated. Simple observation bias.
- eru 11y agoIt's rather less complexity. But for switching all your legacy ecosystem (and accumulated knowledge!), FP doesn't have enough advantage for that at the moment. But, hey, even C++ got lambdas.