函数式编程与网页开发相关吗?

我最近看到了很多关于函数式编程的内容,Clojure 看起来特别有趣。虽然我“理解”了它的基本描述,但我不知道作为一个网页开发者,如果我可以的话,我将如何使用它。我读到的很多内容都集中在函数式编程的数学方面,而不是常规面向对象中的典型编程情况。

我是不是搞错了?函数式编程与 Web 开发完全无关吗?如果没有,是否有例子说明它被用于“网络”?

25525 次浏览

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)

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.

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.

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.

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

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.

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.

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.

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.

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.

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.

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.

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.

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

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

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.

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.

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.

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.