6 ms·
For a long time I've been looking for a guide to functional programming, applied to common real-world web examples, but still can't find one. E.g. when most of
by crazygringo 11y ago
For a long time I've been looking for a guide to functional programming, applied to common real-world web examples, but still can't find one.
E.g. when most of the code I write is database queries, performing business logic, calls to memcached, handling error cases/messages, validating input, and then outputting data in various formats (JSON, HTML templates, etc.) -- how/where does FP help with that?
Is FP more a paradigm for computationally intensive programming? Back-end web programming? Front-end? I come across "FP is good" all the time, but I've never found any kind of resource to help me understand the tradeoffs between FP and non-FP, where it makes sense to use it and where it doesn't. Or any kind of FP tutorial on how to build, for example, a basic web CRUD app.
- Retra 11y agoFunctional programming is not for back-end web programming or front-end web programming. That's like asking if calculus is for making better nails or for making better screws. Or if a 68K processor is for doing taxes or for doing word processing. It has nothing to do at all with what kind of thing you are building, it is a general means of expressing common patterns in programs. Much of the reason people like FP is because it provides a high degree of code reuse in standardized forms, and it does so with a good bit of terseness and flexibility. Just about everything else in the FP world is about optimizing safety, performance, and reliability without giving up these benefits. When someone says "FP is good," it's analogous to saying "well-commented code is good." As in, it's good everywhere you have the time to do it correctly. (There just happens to be a lot of debate about how to do it correctly.)
- edvinbesic 11y agoFor those of us non-yet enlightened, I keep reading these kind of comments and walk away more confused. Sure, I get what it is but what I don't get is how I would use it in my world of mutating data and side-effects.
- monknomo 11y agoI have only tried FP a little bit, but I personally found value in non mutating data and removing side effects, even in the portions of my code that are OO. It's refreshing getting rid of the whole class of bugs caused by developers using harmless sounding method names that subtly alter their data. It did require substantial rewriting of the basic units of the program I tried it in, so there is that...
- kwhitefoot 11y agoIf your world of mutating data and side effects is anything like mine taking a functional view should let you get rid of a lot of those side effects and mutating data. The functional program will be easier to debug. Unfortunately it might also be slower but this can be mitigated by memoization or caching.
- Retra 11y agoOne of the reasons FP has gotten so popular over the last 15 years is because we've figured out how to handle mutating data and side effects in a way that is compatible with standard FP plumbing. (Though there are probably a few roadblocks here and there that need work.)
- Lazare 11y agoSo...don't write code that mutates data and has side-effects? It is, again, like saying "I don't see how this helps me in my world of uncommented code". You could just comment your code, right? (Or if that's too much work, or it's a shared code base and you can't get buy-in, just start going forward?)
- edvinbesic 11y agoI belive database operations are considered a side-effect, so how would you propose I do away with those?
- Retra 11y agoDatabase operations are just state transformers, which are pure functions. It is the state you have to worry about, and you can wrap that state in a number of FP constructs.
- joe_the_user 11y agoThis just seems like fogging the issue. Calculus is not for economics but there are guides to calculus for economics majors. A given language isn't word processing but a "guide to writing a word process in Pascal" is not unimaginable. The parent asked for a guide to using functional programming for the web back end. I'm not sure why sure a thing couldn't exist.
- ignoramous 11y agoYou might already be using certain functional programming constructs without being aware of them. Contrary to popular misconception, OOP and functional programming don't have to be mutually exclusive. "Pure" functional programming (ala Haskell) and OOP most certainly are to an extent non-overlapping (since OOP has state+behaviour at its core), and pure fn-languages have no side-effects. High-order function in java don't exist, so one would do : new Thread(new Runnable() { ... }).start(); A builder pattern can be distilled down as a form of partial application (or, currying, if you like. They aren't the same thing, but are very similar): alertDialog .setMessage(...) .setPositiveButtonText(...) .setNegativeButtonText(...) .setPositiveButtonOnClickListener(...) .setNegativeButtonOnClickListener(...) .build() .show() Functors (in OCaml) can be viewed as Java's abstract-generic-classes with dependency injection [0]. Note that functors in different languages mean different things. Monads are tricky to understand [1], I have been led to believe. But if you use the promises API in javascript, then you're using a form of monad [2]. If you're familiar with javascript, have a look at: https://www.youtube.com/watch?v=m3svKOdZijA https://www.youtube.com/watch?v=m3svKOdZijA (Hey Underscore, you're doing it wrong!) I think the best way is to read a book on fn-programming. I've heard most people recommend these (pick a language, and choose a book; you cannot go wrong with any of these): Functional DS: http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf http://www.cs.cmu.edu/~rwh/theses/okasaki.pdf Real World Haskell: http://book.realworldhaskell.org/read/ http://book.realworldhaskell.org/read/ Real World OCaml: https://realworldocaml.org/v1/en/html/index.html https://realworldocaml.org/v1/en/html/index.html The Joy of Clojure: http://www.manning.com/fogus2/ http://www.manning.com/fogus2/ Learn You a Haskell for Greater Good: http://learnyouahaskell.com/chapters http://learnyouahaskell.com/chapters Learn You some Erlang for Greater Good: http://learnyousomeerlang.com/content http://learnyousomeerlang.com/content Systems Programming in OCaml: https://ocaml.github.io/ocamlunix/ https://ocaml.github.io/ocamlunix/ [0] https://monadmadness.wordpress.com/2015/01/02/monoids-functors-applicatives-and-monads-10-main-ideas/ https://monadmadness.wordpress.com/2015/01/02/monoids-functo... [1] https://realworldocaml.org/v1/en/html/functors.html https://realworldocaml.org/v1/en/html/functors.html [2] https://news.ycombinator.com/item?id=4189082 https://news.ycombinator.com/item?id=4189082
- RobertKerans 11y agoI assume you use SQL or similar? It's not exactly Haskell-level functional, but still, you're probably already using functional paradigms. In the case of JS, using functional techniques simplifies the code quite substantially. Testing becomes easier. Immutable data can be much easier to reason about. It's often orthogonal to OOP, functional techniques in the small, OOP to structure in the large. FRP is very useful for interactions front-end (form validation for example), but also is just all-round terrific for message passing. Recursion is ideal for certain scenarios. If you take something like handling errors, an example would be the way promises deal with them, with the ability to collect at the end of the chain, or deal on a case-by-case basis. I'm just listing stuff off the top of my head; with a general-purpose language [with the option of different paradigms] it's not so much a case of 'build a web app', it's more 'if you use these techniques, you may well find that your code is easier to reason about'. I think the metaphor I like is that with FP it's as if you have an assembly line of functions, and you pass your data down the line, with each function transforming in turn; each function can easily be ripped out and/or replaced and tested. There are multiple downsides and pitfalls - overly abstracted code being a very common one, but as a general approach it's often extremely useful. Books maybe worth a flick through (tried to pick stuff that is less...abstract, & I have specific interests, so am biased) - Functional JS by Michael Fogus, Programming Elixir by Dave Thomas, Learn You Some Erlang for Great Good by Fred Hébert & Programming Erlang by Joe Armstrong Railway Oriented Programming (F#) http://fsharpforfunandprofit.com/rop/ http://fsharpforfunandprofit.com/rop/ Netflix talking about FRP http://youtu.be/XRYN2xt11Ek http://youtu.be/XRYN2xt11Ek (sorry for the slightly rambling post, typing & copy/pasting ain't great on a phone)
- davedx 11y agoWe used chunks of FP in a large frontend application. Where it came in most useful was in the data layer of the app -- the bits from calling out to external web services, adapting that data for the frontend views, and (because we used React) also rendering those views. Functions (including quite a lot of mapping and some reducing here and there) all the way down. We then ended up mixing in some WebGL for high performance parts, and that turned out to work best as more object-oriented code. Probably partly due to the library we used.
- arms 11y agoYou may want to check out Clojure and ClojureScript. Both are functional languages that let you build the backend and front-end for web apps in a functional style. I personally like Eric Normand's "Web Development in Clojure" videos[0]. [0] http://www.purelyfunctional.tv/web-dev-in-clojure http://www.purelyfunctional.tv/web-dev-in-clojure
- codygman 11y ago> For a long time I've been looking for a guide to functional programming, applied to common real-world web examples, but still can't find one. http://realworldhaskell.org/ http://realworldhaskell.org/ (sometimes code is too old to compile) also good: https://nikita-volkov.github.io/a-taste-of-state-parsers-are-easy/ https://nikita-volkov.github.io/a-taste-of-state-parsers-are... > database queries https://www.fpcomplete.com/school/advanced-haskell/persistent-in-detail/existing-database https://www.fpcomplete.com/school/advanced-haskell/persisten... > performing business logic Give a more specific example. Preferably with code, and I'll respond with a Haskell/FP equivalent. > calls to memcached, import qualified Database.Memcache.Client as M mc <- M.newClient [M.ServerSpec "localhost" 11211 M.NoAuth] M.defaultOptions M.set mc "key" "value" 0 0 v <- M.get mc "key" More on memcache here: http://hackage.haskell.org/package/memcache http://hackage.haskell.org/package/memcache > handling error cases/messages, http://book.realworldhaskell.org/read/error-handling.html http://book.realworldhaskell.org/read/error-handling.html > validating input Give an example on input. Depending on complexity I'd most likely use attoparsec or maybe just stuff from the text library. > outputting data in various formats (JSON, HTML templates, etc.) JSON: http://hackage.haskell.org/package/aeson http://hackage.haskell.org/package/aeson HTML: Not sure about outputting html from a data type's structure, but this might be what you are looking for: http://hackage.haskell.org/package/blaze-html http://hackage.haskell.org/package/blaze-html > how/where does FP help with that? First I need to know what your definition of FP is. I think it's safe for me to say without knowing your definition that it doesn't help with those specifically as much as it does with tying them all together and having a greater handle on what their behavior/interaction with eachother will be. > Is FP more a paradigm for computationally intensive programming? Back-end web programming? Front-end? General purpose. > I come across "FP is good" all the time, but I've never found any kind of resource to help me understand the tradeoffs between FP and non-FP, where it makes sense to use it and where it doesn't. That's a shame and I couldn't find really good ones either. To be fair though, such a comparison wouldn't be the easiest to create. I'll add it to my "attempt to blog about" list and we'll see what happens. In the meantime this might help: http://phaazon.blogspot.com/2014/02/regular-imperative-statements-versus.html http://phaazon.blogspot.com/2014/02/regular-imperative-state... > Or any kind of FP tutorial on how to build, for example, a basic web CRUD app. https://www.fpcomplete.com/school/advanced-haskell/building-a-file-hosting-service-in-yesod/part%201 https://www.fpcomplete.com/school/advanced-haskell/building-... http://adit.io/posts/2013-04-15-making-a-website-with-haskell.html http://adit.io/posts/2013-04-15-making-a-website-with-haskel... https://vimeo.com/59109358 https://vimeo.com/59109358 https://github.com/ryantrinkle/memoise https://github.com/ryantrinkle/memoise https://news.ycombinator.com/item?id=9830431 https://news.ycombinator.com/item?id=9830431 I'm also in the middle of creating a Haskell version of the Django polls tutorial: https://docs.djangoproject.com/en/1.8/intro/tutorial01/ https://docs.djangoproject.com/en/1.8/intro/tutorial01/
- hasenj 11y agoThat's a very good point. We need more examples of something practical done in a functional programming manner. Like another commenter mentioned, FP is not a tool to use here but not there, it's a general method of abstraction that can reduce a lot of clutter and boilerplate, and make the code more terse, so that when you read it, you see what your are doing, rather than be entangled in all the details of how you are doing it. I personally utilize functional programming concepts all the time, to the point that I get very frustrated when I have to work in a language that doesn't support functional programming.