Is functional programming relevant to web development?

ClojureFunctional Programming

Clojure Problem Overview


I've been seeing so much recently about functional programming and Clojure looks particularly interesting. While I 'understand' the basic description of what it is, I can't figure out how I would use it on a day to day basis as a web developer, if I can at all. A lot of what I have read focuses on the maths side of functional programming rather then typical programming situations found in regular OO.

Have I got the wrong end of the stick? Is functional programming totally un-related to web development? If not, are there any examples of it being used 'for the web'?

Clojure Solutions


Solution 1 - Clojure

Functional programming matches web apps very well. The web app recieves a HTTP request and produces a HTML result. This could be considered a function from requests to pages.

Compare with desktop apps, where we typically have a long running process, a stateful UI and dataflow in several directions. This is more suited to OO which is concerned about objects with state and message passing.

Solution 2 - Clojure

A few examples off the top of my head:

  • Yahoo! Store is powered by Lisp (originally named Viaweb prior to acquisition)
  • Reddit was fully prototyped in Lisp, although they switched to Python in 2005
  • Hacker News is written entirely in Arc (a Lisp dialect)

Solution 3 - Clojure

I don't see why not - so long as you're delivering standards-compliant HTML to browsers, they don't care what you used to produce it, be that a functional language, an imperative language, or trained monkeys.

Solution 4 - Clojure

Pure functional programming might not map very well into the web programming environment. But the main impediment is just the lack of infrastructure (frameworks and APIs). It will be a long time (probably never, honestly) before a functional language has as rich a web programming environment as Java, Python, or Ruby.

That said, there are some options.

I don't have any experience with any of these. Maybe commenters can weigh in on what's worked well for them.

Solution 5 - Clojure

Twitter rewrote their backend in Scala, a JVM language that supports both the Object Oriented and Functional paradigms.

Also, the Lift web framework is written in Scala.

Solution 6 - Clojure

For Clojure, there's an interesting startup (TheDeadline) that developed using Clojure and Google App Engine. They have a good ppt on Slideshare and an interview on InfoQ.

For a good discussion on deploying Clojure with GAE: http://news.ycombinator.com/item?id=1239788

As far as I know, Clojure has a few web development libraries. Compojure Ring Conjure

Hope this answers some of your questions =) (I'm just starting out too..)

Best, Ryan

Solution 7 - Clojure

It is not totally unrelated to web development. The app sitting on the server can very well take advantage of functional features like closures, higher-order functions, immutability, referential transparency... for instance, you sure have collections that you need to transform or manipulate in whatever way. Functional programming helps here, and it is for a reason that its idioms are penetrating mainstream languages. Functional features help in conciseness, testability, parallelization, and they can also provide native solutions to problems you would otherwise solve with patterns.

Update: there are web frameworks for functional languages too. Weblocks for Common Lisp, Lift for Scala. These are the ones I've heard of, there might be more... however you don't necessarily have to be purely functional -- for example Scala is not pure and should work with any Java framework, you'd still be able to use functional programming for the business layer, etc.

Solution 8 - Clojure

Check out Ur/Web. It's very fast and its static type system knows about things like HTML and SQL so it can guarantee all sorts of nice things about security.

Solution 9 - Clojure

Erlang seems to be getting a lot of use in some of the infrastructure for scalable web apps. the CouchDB and Riak databases are written mostly in Erlang, as is the RabbitMQ message queueing server. One of the keys to its success is that it handles concurrency through message-passing, with no shared mutable state. It's this way of thinking about problems that is useful, more than any particular functional programming language.

Or, look at MapReduce. It's a very functional way of looking at computation, even if your map and reduce functions happen to be internally stateful -- and for that reason, it's a very nice fit to querying large datasets in a fault-tolerant, distributed way.

By all means, go with whatever seems most practical for you. But always keep functional programming in the back of your mind, because you never know when it might come in handy.

Solution 10 - Clojure

They are doing some pretty cool things at Edinburgh University with functional programming for the web.

Solution 11 - Clojure

Here is a web developer experience in building web apps using Haskell. Although functional languages are very type safe and have good concurrency, they always lacked best of the breed api's since it has been darling of the academia since long and yet to be strongly embraced in the real world. I hope it's not too far away. Erlang had already got into it.

Solution 12 - Clojure

Language and paradigm doesn't matter for web apps, they're all equally good and bad. If you are looking for a reason to learn a new paradigm, just dive in. My advice would be to analyze the problem you're trying to solve and select the appropriate toolset.

Solution 13 - Clojure

Javascript (the language of the FE portion of the web and increasingly the BE as well) isn't functional per se but functions are first order functional

Solution 14 - Clojure

We have just launched an online spreadsheet where the backend is written entirely in Erlang.

http://hypernumbers.com

By any standards this is one of the most complex web-apps you can build with a ginormous GUI with mental complexity.

Solution 15 - Clojure

The reason why functional programming is popular with web programming is that it explicits shared and changing state and allows the programmer to express the purely functional parts as pure functions. Pure functions have the advantage of being very simple to run in parallel - as they have no side-effects.

At least that's my reason.

Solution 16 - Clojure

Another short answer: http://www.mlstate.com -- a full web development platform, based on FP. The clean semantics of the language permit all sorts of automated safety and security analysis, optimizations, etc.

Caveat: I work there.

Solution 17 - Clojure

Functional languages may not be directly useful for building great apps but we use functional programming paradigm heavily for building our Apps. Pure functional programming puts the constraint of "no side effects". This ensures that pure functional calls will yield same result in what ever order they are called. This is not ideal for web-development but if functional programming is combined with a state-changing system, a robust web app can be built. Have a look at my paper for more details: FAST Server Also these slides.

Solution 18 - Clojure

Yes, as functional programming can be done in any language, you can use it as a web developeron a day to day basis.

Should you? it depends upon the problem you are solving. Functional programming is a programming paradigm and where you should use it depends upon the problem you are solving.

To make the decision simpler, think whether it is easier to solve some problem by using OOPs concepts, where encapsulation, polymorphism, inheritance like features can make your life easier?

If yes, do not go for functional programming there and simply use OOPs. If your application is going to perform complex computation / calculation / business logic and involving heavily concurrent processing, Functional programming can offer lots of tools and advantages in such cases.

These are just different styles of building the structure and elements of programs, so it’s all about using right tool for the right job.rest anything can be done using anything.

Functional programming in web development:

JavaScript supports functional programming and it is very much supportive when we are in context of web development.React framework is heavily influenced by functional programming principles and used in many web applications out there.

Also, you can find many web applications built and running with frameworks developed on functional programming languages listed below:

• WebSharper (F#)

• Snap (Haskell)

• Lift (Scala)

• Ocsigen (OCaml)

• Chicago Boss, Zotonic (erLang)

Hope So my answer will help anyone.

Solution 19 - Clojure

You probably won't use it and shouldn't use it but when you say that someone will always find an exception to the rule (Viaweb, etc.). Basically there is no "super language" there are only working lines of code, usually in "Blub." Even Paul Graham says that the main (in fact only) benefit to a Lisp is one's ability to rapidly prototype.

Also "super languages" usually impinge rather than increase code readability, meaning that the one "genius" who wrote it has to maintain it forever since no one else can understand it, especially since he is likely to write it in his own modified dialect. This decreases the possible scope of any project, which means that even if new, innovative things can be done, they are not extensible and so remain at a relatively small scale (like Hacker News in Arc).

That's not to say that someone can't have a genius idea and implement it in a incomprehensible style that then can be re-written in Blub and extended so that lots of people can benefit from it. Actually, that's exactly what has happened in all of the Lisp success stories, not to mention every famous philosopher that's ever lived. But of course, if you are a "genius" you might also be able to prototype your product some other way.

As for FP on the JVM, there are limited but cool things possible. Although I would personally use it for prototyping only, it is possible you might have a use case (usually something to do with multi-threading) where it provides some improvement.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHates_View Question on Stackoverflow
Solution 1 - ClojureJacquesBView Answer on Stackoverflow
Solution 2 - ClojureGreg CaseView Answer on Stackoverflow
Solution 3 - ClojureSherm PendleyView Answer on Stackoverflow
Solution 4 - ClojureChris ConwayView Answer on Stackoverflow
Solution 5 - ClojureJames McMahonView Answer on Stackoverflow
Solution 6 - ClojureRyan TeoView Answer on Stackoverflow
Solution 7 - ClojureGermánView Answer on Stackoverflow
Solution 8 - ClojureCyrus OmarView Answer on Stackoverflow
Solution 9 - ClojurePeter ScottView Answer on Stackoverflow
Solution 10 - ClojureAli AfsharView Answer on Stackoverflow
Solution 11 - ClojureA_VarView Answer on Stackoverflow
Solution 12 - ClojureChuck McKnightView Answer on Stackoverflow
Solution 13 - ClojureMarcus WestinView Answer on Stackoverflow
Solution 14 - ClojureGordon GuthrieView Answer on Stackoverflow
Solution 15 - ClojurerbanffyView Answer on Stackoverflow
Solution 16 - ClojureYoricView Answer on Stackoverflow
Solution 17 - ClojuregopiView Answer on Stackoverflow
Solution 18 - ClojureAmit AgrawalView Answer on Stackoverflow
Solution 19 - ClojureJoelView Answer on Stackoverflow