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

How do I optimize this (node.js)?

Name: Anonymous 2012-05-25 16:51

The loop seems unnecessary but I don't know how to get rid of it.


var a = [
            "Start ",
            function(){return "middle "},
            "end"
        ],
    c = "";
for(var i = 0; i < a.length; i++){
    var d = typeof a[i] === 'function' ? a[i]() : a[i];
    c = c.concat(d);
}
console.log(c);


var a is generated once at runtime from an array of strings and another array of functions. The functions obviously return dynamic content so doing something like this var a = a[0]+a[1]()+a[2]; by loop once at runtime is not an option since I'd be saving the result of the function and not the function itself.

I've seen some code where a tailor-made function function(){return a[0]+a[1]()+a[2];} is created at runtime that returns the strings and functions() correctly but I don't know how to achieve something like that. In the example I saw the JavaScript source stored in a "string" and editted via String methods and then eval() evaluated. I don't even know if it's worth it in that case.

Thoughts? Help?!

Name: Anonymous 2012-05-26 0:40

>>10
This actually reminds me of some Haskell code I've seen...
instance (Fractional a) => Fractional (x -> a) where
    f / g = liftM2 (/) f g
    recip = (recip .)
    fromRational = const . fromRational

average = sum / genericLength

fib = (round .) $ ap ((/) . (**) . (1 +)) ((. (2 **)) . (*)) $ sqrt 5

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