Skip to content

Comment on Stupid Languages

Comments

I think one of the reasons for the third argument (the array itself) is to permit chaining. Consider this:

    var publishedPosts = posts.filter(function(post){
        return post.published
    })

    var intervals = publishedPosts.map(function(post, i){
        var next = publishedPosts[i+1]
        return post.date - (next && next.date || 0)
    })
With the array reference you don't need an intermediate var:
    var intervals = posts.filter(function(post){
        return post.published
    }).map(function(post, i, arr){
        var next = arr[i+1]
        return post.date - (next && next.date || 0)
    })

I think in that case it would be better to make it variadic on input arrays, like map(makeInterval, array, array.tail())

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.