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

Better prose is:

"He rushed to the exit, leaped down the stairs, and ran for the train."

Better programming is to factor out the action:

    [socket, window, file]*.close()


Just curious, is that taken from an actual language? Because I've wanted a feature like that for ages.


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.


When 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()


I believe it exists in Cω via "generalized member access" without recourse to map/foreach-type constructs.


Indeed. Programming is more like writing math than natural language.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

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

Search: