5 ms·
Just curious, is that taken from an actual language? Because I've wanted a feature like that for ages.
by SlyShy 15y ago
Just curious, is that taken from an actual language? Because I've wanted a feature like that for ages.
- scythe 15y agoIt 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.
- gredman 15y agoI believe it exists in Cω via "generalized member access" without recourse to map/foreach-type constructs.
- vorg 15y agoWhen I wrote that I meant it to be pseudocode, but it does seem to be valid Groovy syntax: class A{ def close(){println "closed"} } def a1= new A() def a2= new A() [a1, a2]*.close()