Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It exists in Ruby:

    [socket, window, file].each{|item| item.close()}
http://codepad.org/GqNp94Iw

It sort-of exists in Lisp-inspired languages (anything from Arc to Lua to Io) with appropriate boilerplate, for example:

    function apply(tbl, func)
       for key, val in ipairs(tbl) do func(val) end
    end

    apply({socket, window, file}, function (item) item.close() end)
http://codepad.org/SNWO1Y6K


It also exists with slightly different boilerplate in Perl:

$_.close() for $socket, $window, $file;


It'll work in JavaScript, too:

   [socket,window,file].forEach(function(o){o['close']();});


And all of these ways are longer and less clear than the original. Better to write code like Hemingway than like Proust.


You can also:

  [socket, window, file].map(&:close)
That just sends the :close message to each object in the set and returns a set of the results of each object evaluating that message. Yay, Smalltalk.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: