News for hackers. Stuff that matters.
Re:Unpythonic Python by exDM69 on Wednesday April 09, 2014 @03:24PM (#7561679)
When you write too much Haskell, your Python code starts to look like this:

    print('\n'.join(
        'FizzBuzz' if x%5==0 and x%3==0
        else 'Fizz' if x%3==0
        else 'Buzz' if x%5==0
        else str(x)
        for x in range(1, 101)))
I would really like to have a "let" expression in Python to avoid having to write a new function with a def statement when you could get away with a simple lambda or generator expression.
Re:Unpythonic Python by acqq on Thursday April 10, 2014 @03:03AM (#7564571)
And when you write C carefully, the C code looks like this (thanks seanjensengrey and rhth54656 for ideas):

    int i; static char* a[] = { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" };
    for ( i = 1; i < 101; i++ )
        printf( a[ (i%5==0)*2 + (i%3==0) ], i );
The loosely similar Python:

   for i in range( 1, 101 ):
        print( [ i,'Fizz','Buzz','FizzBuzz' ][ (i%5==0)*2 + (i%3==0) ] )
Re:Unpythonic Python by acqq on Thursday April 10, 2014 @03:51AM (#7564728)
And my Visual C 6 compiles the above C to this x86 asm with a single conditional jump, just for the loop:

    mov	edi, DWORD PTR __imp__printf
    mov	esi, 1
   L1:
    mov	eax, esi
    cdq
    mov	ecx, 5
    idiv	ecx
    mov	eax, esi
    mov	ebx, 3
    push	esi
    mov	ecx, edx
    neg	ecx
    sbb	ecx, ecx
    cdq
    idiv	ebx
    inc	ecx
    neg	edx
    sbb	edx, edx
    inc	edx
    lea	edx, DWORD PTR [edx+ecx*2]
    mov	eax, DWORD PTR arr[edx*4]
    push	eax
    call	edi
    add	esp, 8
    inc	esi
    cmp	esi, 101
    jl	SHORT L1
The magic is in the neg sbb combination: The neg changes reg to two's complement but also sets or clears the CF if the argument was != 0 then sbb reg,reg effectively moves CF to the reg avoiding conditional jump for != 0.
Re:Unpythonic Python by yew on Thursday April 10, 2014 @02:47PM (#7568772)
Taking that a little further,

  const char *fb[][2] = {{"%d\n", "Fizz\n"}, {"Buzz\n", "FizzBuzz\n"}};
  int i;
  for (i = 1; i <= 100; ++i)
          printf(fb[i % 5 == 0][i % 3 == 0], i);
(It's a pity we have to specify the second dimension manually . . .)
Re:Unpythonic Python by loqi on Wednesday April 09, 2014 @06:51PM (#7563063)
In the generator case you can define a poor man's let like this:

    def let(x):
        yield x
And use it by "iterating":

    ...
    for x in range(1, 101)
    for divisible_by_5 in let(x % 5 == 0)
Still not as nice as having actual syntax for it.
Re:Unpythonic Python by piinbinary on Wednesday April 09, 2014 @07:00PM (#7563103)
You can also hack the with statement to get a let block:

    import contextlib

    @contextlib.contextmanager
    def let(*values):
        yield values

    with let(1, 2, 3) as (a, b, c):
        print(a, b, c)
Re:Unpythonic Python by gbog on Wednesday April 09, 2014 @07:44PM (#7563291)
I also tend to wrap loops in expressions like that. Then, in almost every interesting loop, I suddenly want to log something or add intermediary computation, and have to refactor into the good old boring for loop.
Re:Unpythonic Python by Too on Thursday April 10, 2014 @12:53AM (#7564267)
Pipeline your generator through a logging generator :)
Re:Unpythonic Python by clickok on Wednesday April 09, 2014 @03:35PM (#7561773)
"let = lambda" would be the same thing, wouldn't it?
Re:Unpythonic Python by exDM69 on Wednesday April 09, 2014 @03:39PM (#7561792)
Yes, but in an incredibly verbose way. And a simple let->lambda conversion can't express things that Scheme's let* or letrec expressions let you do (in Haskell, a "let" or "where" expression is a "letrec").
Re:Unpythonic Python by aaren on Wednesday April 09, 2014 @04:11PM (#7562008)

    >>> let = lambda
      File "<stdin>", line 1
        let = lambda
               ^
    SyntaxError: invalid syntax
Am I misunderstanding you?
Re:Unpythonic Python by farinasa on Wednesday April 09, 2014 @04:17PM (#7562048)
>>> let = lambda x: 2*x

>>> print let(4)

8

Edit: Awesome, downvoted for helping.

Re:Unpythonic Python by Ideka on Wednesday April 09, 2014 @06:35PM (#7562968)
That's not what they mean. The usage they are discussing is more like this, I believe. In a functional context, instead of doing:

    print expensive_computation(), expensive_computation()
You can do:

    (lambda x: print x, x)(expensive_computation())
Which would be equivalent to, with an imaginary let syntax:

    (let x be expensive_computation(): print x, x)
Re:Unpythonic Python by msravi on Thursday April 10, 2014 @02:49AM (#7564537)
Julia:

print([(x%3==0) && (x%5==0) ? "FizzBuzz" : (x%3==0) ? "Fizz" : (x%5==0) ? "Buzz" : x for x in 1:100])

Re:Unpythonic Python by akbarnama on Thursday April 10, 2014 @12:04PM (#7567337)
Cool!
Re:Unpythonic Python by eriktaubeneck on Wednesday April 09, 2014 @05:20PM (#7562497)
I like this one. I don't know enough Haskell to have come up with it on my own (but I should!)