News for hackers. Stuff that matters.
Re:Writing a DSL with Python by pavelludiq on Sunday September 06, 2009 @07:01PM (#808256)
What exactly would people do with multi-line lambdas? I would definitely refactor any lambda with statements or more than one expression in it, give it a name, or use a list comprehension, so I've never seen a problem with them. Can anybody give me an example of a piece of code they think would look better if python had multi-line lambdas?
Re:Writing a DSL with Python by rw on Sunday September 06, 2009 @07:18PM (#808285)
If you give something a name, yet still only use it once, what's the point? It is just semantic clutter to do that (unless, of course, you can't use multiline lambdas...).
Re:Writing a DSL with Python by CaptSolo on Sunday September 06, 2009 @09:38PM (#808430)
There is no big harm in giving something a name. Giving a name also helps if you find a need to reuse it later. Plus a name can give a hint to what this code is for.
Re:Writing a DSL with Python by rw on Sunday September 06, 2009 @09:56PM (#808449)
Yet, if you look in functional programming code (e.g. Haskell, Scheme), you'll find many anonymous lambdas (that in python would be > 1 line). Why would good coders bother doing that?
Re:Writing a DSL with Python by eru on Monday September 07, 2009 @07:11AM (#808954)
If something is small and enough to be anonymous, it's usually simply enough to be expressed without binding a variable.

(See point-free style.)

Re:Writing a DSL with Python by rw on Monday September 07, 2009 @09:44AM (#809125)
Obviously. Show us how to do point-free style in Python. We're talking about Python's lame implementation of anonymous functions, not something that it doesn't have any implementation of.
Re:Writing a DSL with Python by eru on Monday September 07, 2009 @04:02PM (#809667)
Oh, I thought you were talking about programmers' habits in Haskell.
Re:Writing a DSL with Python by GeneralMaximus on Sunday September 06, 2009 @10:41PM (#808486)
In Python, you can easily define methods within methods, so there is little semantic clutter at the toplevel.

  def doSomething():
    def doSomethingElse():
      # Code goes here.
      pass
    
    eggs = filter(doSomethingElse, spam)
    for cheese in eggs:
      # Do something.
      pass
Re:Writing a DSL with Python by gaius on Monday September 07, 2009 @08:48AM (#809064)
Because functional languages discourage loops. This is just a really trivial example, but:

    # value x =[|1;2;3;4|];
    value x : array int = [|1; 2; 3; 4|]
    # for i = 0 to (Array.length x) - 1 do {
      Printf.printf "%d" x.(i);
      };
    1234- : unit = ()
    # Array.iter (fun y -> Printf.printf "%d" y) x;
    1234- : unit = ()
Basically the entire body of any for loop I would want to make an anonymous function. Then if I cared about the result I could do:

    # Array.map (fun y -> Printf.printf "%d" y) x;
    1234- : array unit = [|(); (); (); ()|]

Assuming the underlying platform supported it, and the function is pure, that's "free" parallelism.
Re:Writing a DSL with Python by hedgehog on Monday September 07, 2009 @01:39AM (#808654)
Those are just two of the complaints I hear most. A friend of mine actually went so far as to patch Python to use curly braces at the start of a project in Python. "Perfect Python" he called it. Personally I think deviating from PEP8 & standard idioms is likely to cause more pain in maintainability than you win in elegant syntax.
Re:Writing a DSL with Python by dkersten on Monday September 07, 2009 @05:05AM (#808846)
Ugh, he means imperfect python, right? Even if I hated the indention sytnax, which I dont (and it seems to me that it grows on most people after a few weeks of use), I think a patched python is the wrong way to go about it as you now deviate from the main python distributions, so cant make use of updates and bugfixes (and do all libraries play nice with a patched version? maybe - maybe not). If you absolutely must have braces, use #{ and #} :-D, you should be indenting anyway!

I really don't get why some people hate the indention-as-syntax. Sure, its different to C and Java, but it also results in (more) consistently formatted code and readable indention. It also only takes a day or two to get used to, even if you're a hardened C++ user (for the record, I have nothing against C/C++ syntax, I actually switched from years of C++ to Python as my main language and I just recently started a C++/Python hybrid project).