5 ms·
It exists in Ruby: [socket, window, file].each{|item| item.close()} http://codepad.org/GqNp94Iw http://codepad.org/GqNp94Iw It sort-of exists in Lisp-ins
by scythe 15y ago
It exists in Ruby:
[socket, window, file].each{|item| item.close()}
http://codepad.org/GqNp94Iw 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 http://codepad.org/SNWO1Y6K
- btilly 15y agoIt also exists with slightly different boilerplate in Perl: $_.close() for $socket, $window, $file;
- getsat 15y agoYou 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.
- gliese1337 15y agoIt'll work in JavaScript, too: [socket,window,file].forEach(function(o){o['close']();});
- michaelhoney 15y agoAnd all of these ways are longer and less clear than the original. Better to write code like Hemingway than like Proust.