3 ms·
So far I believe side-effects as we think of them are unavoidable[1]. But they can be reduce to a minimum (the maximum being writing programs as a big set of gl
by agumonkey 11y ago
So far I believe side-effects as we think of them are unavoidable[1]. But they can be reduce to a minimum (the maximum being writing programs as a big set of global variables, and jumping GOTOs between mutations). Using functional style avoid a lot of nasty side effects like unnecessary temporaries, uninitialized values and their modification afterward (local assignments or across procedures). This is a thing one can do in C, whereas in FP languages, you're constrained to write this way, giving a culture of combinators and patterns (tree rec, map, fold, streams, monads, ..) hugely solid and tiny, thus reusable.
I understand what you mean, because even though I love FP a lot, I still struggle to conceive whole systems (other than simple map-filter dataflow ones) this way. So far I keep the FP principles for small pieces, hoping later on I'll find a way to close the gaps.
[1] John Backus tried to design purely functional languages a long time ago and he hit the IO wall (he mentions it in some article). Haskellers had issues at first and found that Monads were a nice functional way to express IO-like logic. It's still something weird, a lot less clean than purely functional expressions to be evaluated.
- edvinbesic 11y agoThe 'IO wall' as you put it is the main thing I have not been able to wrap my head around. Most of the examples I have seen of FP-implementation-of-X leave this bit out, or at best is a passing mention left as a 'reader exercise'. Most of us probably already follow best practices in terms of small functions, no global state, promises, composability etc. But at the end of every chain I have a db query and most of those mutate some data or other. Hopefully some day it clicks.
- codygman 11y ago> Hopefully some day it clicks. While you wait for that, try writing useful code despite not fully understanding it. Tell me what you think of the following code examples: > sum . take 2 $ [1,2,3,4,5] 3 > -- now lets pretend we have a list of numbers we got from the DB > let mockDBResponse = return [1,2,3,4,5] :: IO [Int] > -- first lets try the exact same code as above > sum . take 2 $ [1,2,3,4,5] 3 > sum . take 2 $ mockDBResponse <interactive>:46:16: Couldn't match expected type ‘[c]’ with actual type ‘IO [Int]’ Relevant bindings include it :: c (bound at <interactive>:46:1) In the second argument of ‘($)’, namely ‘mockDBResponse’ In the expression: sum . take 2 $ mockDBResponse > -- what to do? Use <$> > import Control.Applicative ((<$>)) > sum . take 2 <$> mockDBResponse 3 1) Applying pure functions to pure functions sum . take 2 $ [1,2,3,4,5] 2) Applying pure functions to monadic functions that implement fmap of the Functor typeclass sum . take 2 <$> [1,2,3,4,5] When I noticed that adding brackets around $ allowed me to apply pure functions to monadic ones a lot more made sense to me.
- atrilumen 11y agoImmutability is useful everywhere in the stack, even the database. See "Turning the database inside out with Apache Samza" by Martin Kleppmann: https://www.youtube.com/watch?v=fU9hR3kiOK0 https://www.youtube.com/watch?v=fU9hR3kiOK0