On Thu, Mar 24, 2011 at 5:07 PM, Jacob Beard <[email protected]> wrote:

>  Off the top of my head, I've thought of the following practical examples to
> discuss from my own experience:

> * Function closures to manage asynchronous behaviour, e.g. asynchronous
> network requests. Provide an example using XMLHTTPRequest.

An animation like yellow fade can show off the idea of a thunk (i.e. a
callback that takes no arguments, a thought that has already been
"thunk".).

var SS_yellowFade = function(el) {
  var b = 155;
  var f = function() {
    el.style.background = 'rgb(255,255,'+ (b+=4) +')';
    if (b < 255) {
      setTimeout(f, 40);
    }
  };
  f();
};


> * Higher-order functions and how they can enable patterns like MapReduce.
> This enables iteration to be abstracted out, so that it can be implemented
> in many ways while remaining transparent to the developer, e.g. run on a
> cluster, or split across multiple threads.  Provide an example using
> CouchDB's MapReduce.

WebWorkers would be an interesting way to show this in the browser and
a bit Erlang-like.

http://wearehugh.com/public/2010/08/html5-web-workers/




> * Using function closures for data hiding in languages that do not directly
> support things like private attributes on classes. Provide an example using
> the js module pattern.

I'd say your example doesn't match the previous sentence....

Closures also allow the creation of classes where the instances have
private variables. This is quite different then the module pattern.
This also is not pure functional programming...it is the opposite:
stateful object-oriented programming.

var makePerson = function(name, birthYear) {
    return {
        getName: function() {return name;},
       getAge: function(){return (new Date()).getFullYear()-birthYear;}
    };
};

You could add a setter for birth year that checks the birth year is
not in the future and so you've protected against bad data in the
instance by making birthYear private.

I believe the term "module pattern" is usually just to hide variables
and expose certain functions

var counter;
(function() {
    var count = 0;

    counter = function(x) {
        count += x;
        return count;
    };
})();

This can be written to allow multiple counters to be created

// count as a parameter allows setting start value
var makeCounter = function(count) {
    count = count || 0;
    return function(x) {
        count += x;
        return count;
    };
}

var counter = makeCounter(0);


This can also be written very class/OOP-like which gets us back to
something like makePerson

var makeCounter = function(count) {
    count = count || 0;

    return {
        add: function(x) {
            count += x;
            return count;
        },
        getCount: function() {
            return count;
        }
    };
};



> * Continuations and the continuation-passing style in JavaScript. Show how
> this is useful if you wish to stop and resume execution, without using
> threads, while maintaining the state of the stack. Provide an example of how
> this could be used in the context of programming an asynchronous HTTP
> server.
> If anyone has other ideas of suggestions for examples to present, I'd really
> like to hear them. Let me know what you think. Thanks,

You might find ideas here...

Oliver Steele has written about functional JavaScript
http://osteele.com/archives/category/javascript

Virtually any example in SICP can be converted to JavaScript with the
realization that only some engines provide the necessary proper tail
calls to conserve memory.

http://blog.jcoglan.com/2011/03/05/translation-from-haskell-to-javascript-of-selected-portions-of-the-best-introduction-to-monads-ive-ever-read/

and follow-up articles on coglan's blog.

Peter

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to