2

I have lots of experience building enterprise apps using Java/C# and have become accustomed to all the trappings that come with object-oriented, statically typed languages. Specifically, I've become quite adept at dealing with system complexity by using the standard tools of the trade:

  • interfaces/abstract types
  • object composition
  • dependency inversion

I'm being asked to engineer a fairly complex back-end message processing system using a dynamic, functional language (Lua). Functional languages are all the rage these days (NodeJs, JavaScript etc), so I'm happy to use this as an opportunity to jump on said bandwagon.

Can anyone suggest a sample application or architecture I can use to learn about using things such as first class functions, closures, currying to build a complex, loosely coupled system?

Many thanks!

Mitch A
  • 2,050
  • 1
  • 21
  • 41
  • 1
    Lua is not a functional language. It has elements of functional programming, but merely having functions as first-class objects does not mean it is a functional programming language. – Nicol Bolas Sep 08 '12 at 02:02
  • 1
    @NicolBolas, in that case would you say Scheme is also not a functional language? – finnw Sep 08 '12 at 12:47
  • @finnw: I don't know Scheme very well. But I do know what functional programming is, and I know that Lua isn't it. Just because a language has closures doesn't make it functional. [Functional programming](http://en.wikipedia.org/wiki/Functional_programming) includes immutable state and so forth, which Lua is pretty terrible at. It is very difficult to write a pure function in Lua, and functional programming should not make writing pure functions hard. – Nicol Bolas Sep 08 '12 at 15:08
  • 1
    @NicolBolas, I see this has been discussed on SO before, e.g. http://stackoverflow.com/q/2291742, http://stackoverflow.com/q/214913. I guess Scheme is "grandfathered" since it was considered a functional language when it appeared, but it would not meet the stricter definition that is used today (by people who grew up with Haskell, Clojure etc.) – finnw Sep 08 '12 at 15:52
  • @finnw Scheme allows one to write pure functions, avoid mutable state, etc. It doesn't *require* it, however, and it doesn't really use anything fancy to segregate IO. – Marcin Sep 08 '12 at 18:38
  • I'm not sure why people are voting to close: this is a programming question, and it's not easy to find great resources on using functional programming or dynamic languages to build large systems. – Marcin Sep 08 '12 at 20:00
  • Why are people voting to close? I'm asking for resources on design patterns I can apply in dynamic/scripting languages. How does that qualify as a question "which will likely solicit debate, arguments, polling, or extended discussion"? – Mitch A Sep 09 '12 at 16:49

2 Answers2

2

I will suggest looking on the libs/frameworks below, they are really well designed, keep in mind that javascript and lua are very similar, just replace objects with tables add coroutines and "nice" syntax and you have Lua.

Lua

node.js

  • Express micro web framework.
  • Mocha Unit testing framework.
2

I've done a quite a bit of research on "design patterns" that can be applied in dynamic languages with first class function support, and here are my findings.

Currying == Dependency Injection. Currying allows you to take a function and repackage it as a new function with one or more of its parameter values already assigned. This is very similar to an IoC container instantiating a class "bootstrapped" with all it's dependencies and ready for consumption by clients.

First Class Functions == Command pattern. Since first class functions can be passed around like values, you basically get the Command pattern for free and without the overhead.

References:

First Class Functions == Command pattern

Functional Dependency Injection via Currying

Community
  • 1
  • 1
Mitch A
  • 2,050
  • 1
  • 21
  • 41