Activate your free membership today | Log-in

Friday, June 20th, 2008

Algebraic Data Types in JavaScript

Category: JavaScript

>Sjoerd Visscher has written a library that lets you create algebraic data types in JavaScript for use in your functional programming world.

ADT.js is written in JavaScript 1.8 "which means that as of this writing it only runs in Firefox 3.0. I have chosen to keep it 1.8, because the code is a lot cleaner, thanks to the new expression closures. But there is nothing that cannot be made to work in ECMAScript edition 3. And I have no doubt that the same thing could be done in Python or Ruby."

Now you have the library, you can create types like this (with Haskell to compare):

JAVASCRIPT:
  1.  
  2. // Haskell:
  3. // data Color = Red | Green | Blue | Yellow
  4. // data Point = Pt Float Float Color
  5. Color = Data(function() ({ Red: {}, Green: {}, Blue: {}, Yellow: {} }))
  6. Point = Data(function() ({ Pt: { x: Number, y: Number, color: Color } }))
  7.  
  8. // Haskell:
  9. // data Attrs n v = Attr n v
  10. // data Node  n v = Elem n (List (Attrs n v)) (List (Node n v)) | Text v
  11. Attrs = Data(function(_, n, v) ({ Attr: {name: n, value: v} }))
  12. Node = Data(function(node, n, v) ({
  13.   Elem: { name: n, attributes: List(Attrs(n, v)), childNodes: List(node) },
  14.   Text: v
  15. }))
  16.  

Now you have the types, you can do things to them:

JAVASCRIPT:
  1.  
  2. // With unfold you can easily generate algebraic data structures from other data. The following example shows how to generate a decreasing list of numbers.
  3. var counter = List.unfold(function(n, c) n ? c.Cons(n, n - 1) : c.Nil)
  4.  
  5. // Fold is the reverse of unfold. It destructs data into a return value. Here is an example that multiplies a list of numbers.
  6. var prod = List.fold({ Nil: 1, Cons: function(h, t) h * t })
  7.  
  8. // With map you provide a function for each type parameter. Here are 2 examples using the XML data types
  9. var addPrefix = Node.map(function (name) "x:" + name, id)
  10. var normalizeSpace = Node.map(id, function (value) value.replace(/^s+|s+$/g, ""))
  11.  

And, a lot more.

Related Content:

  • algebraic number
    An algebraic number is any real number that is a solution of some single-variable polynomial equation whose coefficients are all...
  • JavaScript IntelliSense: Visual Studio 2008 Learning Guide
    This section of the Visual Studio 2008 Learning Guide examines JavaScript IntelliSense, which aims to make life easier for ASP.NET AJAX...
  • CFX
    Casio Algebra FX add-in...
  • Asynchronous JavaScript and XML
    Ajax (Asynchronous JavaScript and XML) is a method of building interactive applications for the Web that process user requests immediately. Ajax...
  • Ajax (Asynchronous JavaScript and XML)
    Ajax (Asynchronous JavaScript and XML) is a method of building interactive applications for the Web that process user requests immediately. Ajax...

Posted by Dion Almaer at 6:12 am
Comment here

+++--
3.9 rating from 15 votes

Comments Here »

Comments feed TrackBack URI

Leave a comment

You must be logged in to post a comment.