Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

WTF Javascript?

Name: Anonymous 2012-02-29 2:35

function map(proc, lst) {
    var res = [];
    for (var obj in lst)
        res.push(proc(obj));
    return res;
}

function filter(pred, lst) {
    var res = [];
    for (var obj in lst)
        if (pred(obj))
            res.push(obj);
    return res;
}

ls = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

ls = map(function(x) {
    return x * x;
}, ls);

print(ls);

ls = filter(function (x) {
    return x % 2 == 0;
}, ls);

print(ls);


What I expect:
0,1,4,9,16,25,36,49,64,81
0,4,16,36,64


What I get:
0,1,4,9,16,25,36,49,64,81
1,3,5,7,9


WHY?

Name: Anonymous 2012-02-29 4:14

>>3
Yeah. Furthermore, if you were to use it on a DOM structure, I would give you an index for each leaf in the tree. It's almost never safe to use.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List