shawndumas http://blog.shawndumas.com javascript posterous.com Fri, 02 Dec 2011 10:06:00 -0800 Set An Element's Style http://blog.shawndumas.com/set-an-elements-style http://blog.shawndumas.com/set-an-elements-style

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//setStyle(
// document.getElementById('divId'),
// 'height: 180px; text-align: left; overflow-y: scroll;'
//);

var setStyle = function (elm, style) {
    var rules = (function (style) {
        var css, v,
                rules = {},
                div = document.createElement('div');

        div.innerHTML = '<div style="' + style + '"></div>';
        css = div.childNodes[0].style;
        for (var i in css) {
            (
                i !== 'cssText' &&
                (typeof i === 'string' || i instanceof String)
            ) &&
            (v = css[i]) &&
            (rules[i] = v);
        }

        return rules;
    })(style);

    for (var rule in rules) if (rules.hasOwnProperty(rule)) {
        elm.style[rule] = rules[rule];
    }

    return elm;
};

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Tue, 04 Oct 2011 13:13:00 -0700 American Soundex in JavaScript http://blog.shawndumas.com/soundex-in-javascript http://blog.shawndumas.com/soundex-in-javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var soundex = function (s) {
     var a = s
             .substring(1, s.length)
             .toLowerCase()
             .split(''),
         r = '',
         codes = {
             a: '', e: '', i: '', o: '', u: '',
             b: 1, f: 1, p: 1, v: 1,
             c: 2, g: 2, j: 2, k: 2, q: 2, s: 2, x: 2, z: 2,
             d: 3, t: 3,
             l: 4,
             m: 5, n: 5,
             r: 6
         };
 
     r = s[0].toUpperCase() +
         a
         .filter(function (v, i, a) { return v !== a[i + 1]; })
         .map(function (v, i, a) { return codes[v] }).join('');
 
     return (r + '000').slice(0, 4);
};

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Fri, 16 Sep 2011 05:36:00 -0700 Quick (Partial) XSS Fix In ASP.NET http://blog.shawndumas.com/70596695 http://blog.shawndumas.com/70596695

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
' In the web.config
'
'<pages>
' <tagMapping>
' <clear />
' <add tagType="System.Web.UI.WebControls.TextBox"
' mappedTagType="XSSTextBox"/>
' </tagMapping>
'</pages>

Imports Microsoft.Security.Application.Sanitizer

Public Class XSSTextBox
    Inherits TextBox

    Public Overrides Property Text() As String
        Get
            Return GetSafeHtmlFragment(MyBase.Text)
        End Get
        Set(ByVal value As String)
            MyBase.Text = value
        End Set
    End Property

End Class

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Fri, 09 Sep 2011 11:29:00 -0700 mkrepo.sh http://blog.shawndumas.com/mkreposh http://blog.shawndumas.com/mkreposh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/sh

CURRENT_DIR=${PWD##*/}

git init
git add .
git commit -m "first commit"
pushd g:/
mkdir "$CURRENT_DIR.git"
cd "$CURRENT_DIR.git"
git init --bare
popd
git remote add origin "g:/$CURRENT_DIR.git"
git push origin master
git branch --set-upstream master origin/master

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Fri, 12 Aug 2011 05:47:00 -0700 Cross Browser DOMReady http://blog.shawndumas.com/cross-browser-domready http://blog.shawndumas.com/cross-browser-domready

1
2
3
4
5
6
// http://www.dustindiaz.com/smallest-domready-ever
var ready = function (f) {
  (/in/.test(document.readyState)) ?
    setTimeout('ready(' + f + ')', 9) :
    f();
};

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Wed, 10 Aug 2011 16:23:00 -0700 Surefire Type Detection http://blog.shawndumas.com/surefire-type-detection http://blog.shawndumas.com/surefire-type-detection

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var typs = {};

(function (self) {
     var values = [
               'Function',
               'Object',
               'Array',
               'String',
               'Number',
               'Date',
               'RegExp',
               'Boolean',
               'Null',
               'Error'
          ];

     for (var i in values) if (values.hasOwnProperty(i)) {
          var value = values[i];

          self['is' + value] = (function (v) {
               return function (o) {
                      var r = '';

                      try {
                          r = (o === null) ?
                                   'Null' :
                                   Object
                                       .prototype
                                       .toString
                                       .call(o)
                                       .replace(/^\[object\s(\w+)\]$/, '$1');
                      } catch (e) {
                          r = 'Undefined';
                      }

                      return !!(r === v);
               };
          })(value);
     }
})(typs);

alert(typs.isFunction(Object)); // --> true

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 08 Aug 2011 08:50:00 -0700 A JavaScript Implementation of TreeWalker http://blog.shawndumas.com/a-javascript-implementation-of-treewalker http://blog.shawndumas.com/a-javascript-implementation-of-treewalker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
var NodeFilter = {
    FILTER_ACCEPT: 1,
    FILTER_REJECT: 2,
    FILTER_SKIP: 3,
    SHOW_ALL: -1,
    SHOW_ELEMENT: 1,
    SHOW_ATTRIBUTE: 2,
    SHOW_TEXT: 4,
    SHOW_CDATA_SECTION: 8,
    SHOW_ENTITY_REFERENCE: 16,
    SHOW_ENTITY: 32,
    SHOW_PROCESSING_INSTRUCTIONS: 64,
    SHOW_COMMENT: 128,
    SHOW_DOCUMENT: 256,
    SHOW_DOCUMENT_TYPE: 512,
    SHOW_DOCUMENT_FRAGMENT: 1024,
    SHOW_NOTATION: 2048
};

var TreeWalker = function (root, whatToShow, filter, expandEntityReferences) {
    this.root = root;
    this.whatToShow = whatToShow;
    this.filter = filter;
    this.expandEntityReferences = expandEntityReferences;
    this.currentNode = root;
    this.NodeFilter = NodeFilter;
};

TreeWalker.prototype.parentNode = function () {
    var testNode = this.currentNode;

    do {
        if (
            testNode !== this.root &&
            testNode.parentNode &&
            testNode.parentNode !== this.root
        ) {
            testNode = testNode.parentNode;
        } else {
            return null;
        }
    } while (this._getFilteredStatus(testNode) !== this.NodeFilter.FILTER_ACCEPT);
    (testNode) && (this.currentNode = testNode);

    return testNode;
};

TreeWalker.prototype.firstChild = function () {
    var testNode = this.currentNode.firstChild;

    while(testNode) {
        if(this._getFilteredStatus(testNode) === this.NodeFilter.FILTER_ACCEPT) {
            break;
        }
        testNode = testNode.nextSibling;
    }
    (testNode) && (this.currentNode = testNode);

    return testNode;
};

TreeWalker.prototype.lastChild = function () {
    var testNode = this.currentNode.lastChild;

    while (testNode) {
        if(this._getFilteredStatus(testNode) === this.NodeFilter.FILTER_ACCEPT) {
            break;
        }
        testNode = testNode.previousSibling;
    }
    (testNode) && (this.currentNode = testNode);

    return testNode;
};

TreeWalker.prototype.nextNode = function () {
    var testNode = this.currentNode;

    while (testNode) {
        if (testNode.childNodes.length !== 0) {
            testNode = testNode.firstChild;
        } else if (testNode.nextSibling) {
            testNode = testNode.nextSibling;
        } else {
            while (testNode) {
                if (testNode.parentNode && testNode.parentNode !== this.root) {
                    if (testNode.parentNode.nextSibling) {
                        testNode = testNode.parentNode.nextSibling;
                        break;
                    } else {
                        testNode = testNode.parentNode;
                    }
                }
                else return null;
            }
        }
        if (testNode && this._getFilteredStatus(testNode) === this.NodeFilter.FILTER_ACCEPT) {
            break;
        }
    }
    (testNode) && (this.currentNode = testNode);

    return testNode;
};

TreeWalker.prototype.previousNode = function () {
    var testNode = this.currentNode;

    while (testNode) {
        if (testNode.previousSibling) {
            testNode = testNode.previousSibling;
            while (testNode.lastChild) {
                testNode = testNode.lastChild;
            }
        }
        else {
            if (testNode.parentNode && testNode.parentNode !== this.root) {
                testNode = testNode.parentNode;
            }
            else testNode = null;
        }
        if (testNode && this._getFilteredStatus(testNode) === this.NodeFilter.FILTER_ACCEPT) {
            break;
        }
    }
    (testNode) && (this.currentNode = testNode);

    return testNode;
};

TreeWalker.prototype.nextSibling = function () {
    var testNode = this.currentNode;

    while(testNode) {
        (testNode.nextSibling) && (testNode = testNode.nextSibling);
        if(this._getFilteredStatus(testNode) === this.NodeFilter.FILTER_ACCEPT) {
            break;
        }
    }
    (testNode) && (this.currentNode = testNode);

    return testNode;
};

TreeWalker.prototype.previousSibling = function () {
    var testNode = this.currentNode;

    while(testNode) {
        (testNode.previousSibling) && (testNode = testNode.previousSibling);
        if(this._getFilteredStatus(testNode) == this.NodeFilter.FILTER_ACCEPT) {
            break;
        }
    }
    (testNode) && (this.currentNode = testNode);

    return testNode;
};

TreeWalker.prototype._getFilteredStatus = function (node) {
    var mask = ({
            /* ELEMENT_NODE */ 1: this.NodeFilter.SHOW_ELEMENT,
            /* ATTRIBUTE_NODE */ 2: this.NodeFilter.SHOW_ATTRIBUTE,
            /* TEXT_NODE */ 3: this.NodeFilter.SHOW_TEXT,
            /* CDATA_SECTION_NODE */ 4: this.NodeFilter.SHOW_CDATA_SECTION,
            /* ENTITY_REFERENCE_NODE */ 5: this.NodeFilter.SHOW_ENTITY_REFERENCE,
            /* ENTITY_NODE */ 6: this.NodeFilter.SHOW_PROCESSING_INSTRUCTION,
            /* PROCESSING_INSTRUCTION_NODE */ 7: this.NodeFilter.SHOW_PROCESSING_INSTRUCTION,
            /* COMMENT_NODE */ 8: this.NodeFilter.SHOW_COMMENT,
            /* DOCUMENT_NODE */ 9: this.NodeFilter.SHOW_DOCUMENT,
            /* DOCUMENT_TYPE_NODE */ 10: this.NodeFilter.SHOW_DOCUMENT_TYPE,
            /* DOCUMENT_FRAGMENT_NODE */ 11: this.NodeFilter.SHOW_DOCUMENT_FRAGMENT,
            /* NOTATION_NODE */ 12: this.NodeFilter.SHOW_NOTATION
        })[node.nodeType];

    return (
        (mask && (this.whatToShow & mask) == 0) ?
            this.NodeFilter.FILTER_REJECT :
            (this.filter && this.filter.acceptNode) ?
                this.filter.acceptNode(node) :
                this.NodeFilter.FILTER_ACCEPT
    );
};

if (!document.createTreeWalker) {
    document.createTreeWalker = function (root, whatToShow, filter, expandEntityReferences) {
        return new TreeWalker(root, whatToShow, filter, expandEntityReferences);
    };
}

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 08 Aug 2011 06:10:00 -0700 Logic Gates: In F# (Starting with NAND only...) http://blog.shawndumas.com/logic-gates-in-f-starting-with-nand-only http://blog.shawndumas.com/logic-gates-in-f-starting-with-nand-only

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
type LogicGate =
    | ON
    | OFF
    | NAND of LogicGate * LogicGate
    | NOT of LogicGate
    | AND of LogicGate * LogicGate
    | OR of LogicGate * LogicGate
    | NOR of LogicGate * LogicGate
    | XOR of LogicGate * LogicGate
    | XNOR of LogicGate * LogicGate

let rec evaluate input =
    match input with
    | ON -> true
    | OFF -> false
    | NAND(a, b) -> not (evaluate a && evaluate b)
    | NOT(a) -> evaluate (NAND(a, a))
    | AND(a, b) -> evaluate (NOT(NAND(a, b)))
    | OR(a, b) -> evaluate (NAND(NOT(a), NOT(b)))
    | NOR(a, b) -> evaluate (NOT(OR(a, b)))
    | XOR(a, b) -> evaluate (AND(NAND(a, b), OR(a, b)))
    | XNOR(a, b) -> evaluate (NOT(XOR(a, b)))

[
    NAND(OFF, OFF);
    NAND(OFF, ON);
    NAND(ON, OFF);
    NAND(ON, ON)
] |> List.map (fun x -> printfn (evaluate x))

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 08 Aug 2011 06:06:00 -0700 Logic Gates: In JavaScript (Starting with NAND only...) http://blog.shawndumas.com/logic-gates-in-javascript http://blog.shawndumas.com/logic-gates-in-javascript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     var logicGates = {
        nand: function (a, b) {
                return !(a && b);
        },
        not: function (a) {
                return this.nand(a, a);
        },
        and: function (a, b) {
                return this.not(this.nand(a, b));
        },
        or: function (a, b) {
                return this.nand(this.not(a), this.not(b));
        },
        nor: function (a, b) {
                return this.not(this.or(a, b));
        },
        xor: function (a, b) {
                return this.and(this.nand(a, b), this.or(a, b));
        },
        xnor: function (a, b) {
                return this.not(this.xor(a, b));
        }
    };

    [
        {a: false, b: false},
        {a: false, b: true},
        {a: true, b: false},
        {a: true, b: true},
    ].map(function (x) { return logicGates.nand(x.a, x.b); });

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 08 Aug 2011 05:49:00 -0700 Thrown Continuation-passing http://blog.shawndumas.com/thrown-continuation-passing http://blog.shawndumas.com/thrown-continuation-passing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  // Pseudo Tail Call Optimization.
  // Try/Catch Trampoline.

  Function.prototype.tco = function () {
      var that = this;

      return function () {
          for (
              var caller = arguments.callee.caller;
                  caller;
                  caller = caller.caller
          ) {
              if (caller === arguments.callee) {
                  throw { args: arguments, self: this };
              }
          }

          var args = arguments,
              self = this;
              
          while (true) {
              try {
                  return that.apply(self, args);
              } catch (e) {
                  if (!e.args) { throw e; }
                  args = e.args; self = e.self;
              }
          }
      };
  };

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Thu, 08 Jul 2010 13:02:00 -0700 My Invoke Method http://blog.shawndumas.com/invoke-method http://blog.shawndumas.com/invoke-method

See Prototype’s invoke.

Head over to remy sharp’s JS Bin and play with mine.

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Sat, 12 Jun 2010 07:29:00 -0700 Pub/Sub (Dustin Diaz's) http://blog.shawndumas.com/pubsub-dustin-diazs http://blog.shawndumas.com/pubsub-dustin-diazs

See Diaz’s article.

Head over to remy sharp’s JS Bin and play.

function Publisher(name) {
    this.name = name;
    this.subscribers = [];
}
Publisher.prototype.deliver = function (data, scope) {
    scope = scope || window;

    var self = this;

    this.subscribers.forEach(
        function (value) {
            value.call(scope, self.name, data);
        }
    );

    return this;
};
Function.prototype.subscribe = function (publisher) {
    var self = this,
        isExistent = publisher.subscribers.some(
            function (value) {
                return (value === self);
            }
        );

    if (!isExistent) {
        publisher.subscribers.push(this);
    }

    return this;
};
Function.prototype.unsubscribe = function (publisher) {
    var self = this;

    publisher.subscribers = publisher.subscribers.filter(
        function(value) {
            if (value !== self) {
                return value;
            }
        }
    );

    return this;
};

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Wed, 09 Jun 2010 08:07:00 -0700 More FizzBuzz http://blog.shawndumas.com/more-fizzbuzz http://blog.shawndumas.com/more-fizzbuzz

A functional version (won’t work in IE).

Head over to remy sharp’s JS Bin and play.

See my original FizzBuzz.

(function (n) {
    var r = [];

    while (n--) {
        r.push(n + 1);
    }

    return r.reverse();
})(100).map(function (n) {
    return !(n % 15) ?
        'FizzBuzz' :
        !(n % 3) ?
            'Fizz' :
            !(n % 5) ?
                'Buzz' :
                n;
});

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 07 Jun 2010 20:18:00 -0700 Playing With node.js http://blog.shawndumas.com/playing-with-nodejs http://blog.shawndumas.com/playing-with-nodejs

Streaming a PNG in node.js

var sys = require('sys'),
    http = require('http'),
    url = require('url');

http.createServer(function (request, response) {
    var f = (url.parse(request.url, true)).query.file
        fs = require('fs'),
        path = './' + f,
        rs = fs.createReadStream(path);

    response.writeHead(200, { 'Content-Type': 'image/png' });
    rs.addListener('data', function (b) {
        response.write(b, 'binary');
    });
    rs.addListener('end', function () { response.end(); })
}).listen(8000);

sys.puts('Server running at http://127.0.0.1:8000/');

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Sun, 06 Jun 2010 18:05:00 -0700 My FizzBuzz http://blog.shawndumas.com/my-fizzbuzz http://blog.shawndumas.com/my-fizzbuzz

See Jeff Atwood’s article.

Head over to remy sharp’s JS Bin and play.

var fizzBuzz = function (n) {
  n = n || 100;

  var i = 0,
      r = [];

  while (i++ < n) {
    r.push(
      (!(i % 3) &&  (i % 5)) && ('Fizz (' + i + ')')     ||
      ( (i % 3) && !(i % 5)) && ('Buzz (' + i + ')')     ||
      (!(i % 3) && !(i % 5)) && ('FizzBuzz (' + i + ')') ||
      i
    );
  }

  return r.join('<br />');
};

document.getElementById('result').innerHTML = fizzBuzz();

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Mon, 31 May 2010 04:39:00 -0700 Async Method Queue Chaining (Dustin Diaz) http://blog.shawndumas.com/async-method-queue-chaining-dustin-diaz http://blog.shawndumas.com/async-method-queue-chaining-dustin-diaz

See Diaz’s article.

Head over to remy sharp’s JS Bin and play.

var Queue = function () {
  if (!(this instanceof arguments.callee)) {
      return new arguments.callee();
  }
  this._methods = [];
  this._response = null;
  this._flushed = false;
}

Queue.prototype = {
  add: function(fn) {
    (this._flushed) ?
      fn(this._response) :
      this._methods.push(fn);
  },
  flush: function(resp) {
    (this._flushed) && (return);
    this._response = resp;
    this._flushed = true;
    while (this._methods[0]) {
      this._methods.shift()(resp);
    }
  }
};

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Sun, 30 May 2010 05:06:00 -0700 My "waitUntil" method http://blog.shawndumas.com/click-here-to-set-a-title-846 http://blog.shawndumas.com/click-here-to-set-a-title-846

Head over to remy sharp’s JS Bin and play.

Function.prototype.waitUntil = function (condition, interval) {
    interval = interval || 100;

    var fn = this,
        shell = function () {
            var timer = setInterval(
                function () {
                    var check;

                    try {
                        check = !!(condition());
                    } catch (e) {
                        check = false;
                    }

                    if (check) {
                        clearInterval(timer);
                        delete timer;
                        fn();
                    }
                },
                interval
            );
        };

    return shell;
};

var done = false;

(function () {
    document.getElementById('result').innerHTML = 'I waited...';
}).waitUntil(function () { return !!(done); })();

setTimeout(function () { done = true; }, 2000);

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas
Wed, 26 May 2010 19:37:00 -0700 The Y Combinator (Crockford) http://blog.shawndumas.com/click-here-to-set-a-title-289 http://blog.shawndumas.com/click-here-to-set-a-title-289

See Crockford’s article.

Head over to remy sharp’s JS Bin and play.

var Y = function (le) {
  return ((function (f) {
    return f(f);
  })(function (f) {
    return le(function (x) {
      return f(f)(x);
    });
  }));
};

var factorial = Y(function (fac) {
  return function (n) {
    return (n <= 2) ? n : n * fac(n - 1);
  };
});

!!(factorial(5) === 120); //true

Permalink | Leave a comment  »

]]>
http://files.posterous.com/user_profile_pics/1397506/me__croped_.png http://posterous.com/users/5AGhjA1rqzhT Shawn Dumas shawndumas Shawn Dumas