News for hackers. Stuff that matters.
Re:Curing Python's Neglect by grandalf on Friday May 29, 2009 @05:57PM (#632899)
I've found that code is way more readable, even though I've only been using the language for a short time.

I wonder what a whitespace sensitive ruby would be like :)

  %w(a b c d e f g).each do |i|:
       puts i

  foo = lambda do |x|:
       x + 2

  foo 3
Re:Curing Python's Neglect by natrius on Friday May 29, 2009 @06:31PM (#632963)
That's one of the problems: Inline blocks are fundamentally incompatible with significant whitespace, or at least no one has thought of a good way to combine the two.
Re:Curing Python's Neglect by nathanic on Friday May 29, 2009 @07:05PM (#633020)
I personally like how Haskell can combine the two. You can have:

  myFunc = do
    this
    that
    other
Or equivalently:

  myFunc = do { this; that; other; }
Re:Curing Python's Neglect by natrius on Friday May 29, 2009 @07:47PM (#633050)
How does the first form work out as an inline function argument? I'm not familiar with Haskell's syntax.
Re:Curing Python's Neglect by nathanic on Friday May 29, 2009 @08:07PM (#633068)
I think I misunderstood you before. I thought you were talking about putting multiple statements on a line.

But Haskell can pass around code blocks as well, with or without significant whitespace.

For example, I can define a function called 'forever' to run a sequence of actions forever like so:

  forever x = x >> forever x
Where 'x' is any sequence of actions. You can pronounce '>>' as "followed by". (These are monadic actions, actually, but nevermind that.)

I can invoke it like so:

  myFunc = forever (do {this; that; other})
or:

  myFunc = forever $ do
    this
    that
    other
The dollar sign is a function that lets you dispense with some parentheses. Think of it like an opening paren that closes at the end of the subexpression it's in.

If the block is a function rather than a sequence of actions, Haskell uses '\' as a lambda for introducing anonymous functions.

For example, the function 'map' takes a unary function and calls it with each value from a list. Below I'll call it with an anonymous function which returns double its argument:

  map (\x -> x * 2) [1,2,3]
This results in a new list [2,4,6].
Re:Curing Python's Neglect by shrughes on Friday May 29, 2009 @11:30PM (#633223)
Another good example:

    forM_ [1..9] $ \i -> do
      this i
      that
      other
Re:Curing Python's Neglect by jules on Friday May 29, 2009 @07:10PM (#633024)
Easy. The rule is that every time you see : like this:

    bla bla foo:
       x
       y
    z
It's the same as this:

    bla bla foo
       x
       y
    end
    z
i.e. insert `end` when the indentation ends.
Re:Curing Python's Neglect by natrius on Friday May 29, 2009 @07:46PM (#633049)
How about when the function is being passed as an argument to a function? Does the ')' just go on the next line by itself? What if it's not the last argument? There are many opportunities for ambiguity and/or ugliness.
Re:Curing Python's Neglect by jules on Tuesday June 02, 2009 @08:09AM (#637664)

    collection.each(lambda x:
      ...
      ...
    )
Or if the language doesn't require () to call a method, and uses `do` instead of `lambda`:

    collection.each do x:
       ...
       ...
If it's not the first argument:

    map(lambda x:
      ...
      ...
    , collection)
Yes this is ugly, but using a multi line anonymous function in the middle of an argument list will always be ugly.
Re:Curing Python's Neglect by grandalf on Saturday May 30, 2009 @02:22PM (#633957)
in Python, it works like:

  foo(a, b, lambda x: x+2)
or

  foo2(a, b, foo):
      foo() # this is like js

The lambda is closed by the next ) or ,
Re:Curing Python's Neglect by natrius on Saturday May 30, 2009 @08:05PM (#634220)
Right, but you can't have multi-line anonymous functions, which are pleasant to use in Ruby and JavaScript.
Re:Curing Python's Neglect by grandalf on Saturday May 30, 2009 @11:21PM (#634383)
True, but you can just write a non-anyonymous function in the current scope, which can be passed as boo or called as boo():

  def test():
    def boo(x):
      x += 2
      x += 3
      return x
    something_else(boo, 33, "hi")

  test()
This is equivalent to how procs are often used in Ruby...