3 ms·
That'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
by voltagex_ 11y ago
That'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.