Live data from Hacker News

Unpythonic Python

skien.cc

31–40 of 156 posts

Re: Unpythonic Python

#32
post #3

Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.

Here's mine:

['Fizz' * (not bool(i % 3)) + 'Buzz' * (not bool(i % 5)) for i in xrange(1,101) if i % 3 == 0 or i % 5==0]

EDIT: I hate lambdas. Also I'm not saying the above is pretty or a good idea, but I had some fun.

Re: Unpythonic Python

#34
post #7

This was what one of our Java coworkers wrote a while back: https://gist.github.com/1337/8155054

I don't think you need to even explicity say he's used to Java.

Wow... I've never felt the need to the use the factory technique.

Re: Unpythonic Python

#35
A highly Pythonic, Easier to Ask Forgiveness than Permission[1] version:

    FIZZ=3
    BUZZ=5
    cache = {}
    
    for i in range(FIZZ-1, FIZZ*BUZZ, FIZZ):
        cache[i] = 'Fizz'
        
    for i in range(BUZZ-1, FIZZ*BUZZ, BUZZ):
        try:
            cache[i] += 'Buzz'
        except KeyError:
            cache[i] = 'Buzz'
            
    for i in range(100):
        try:
            print cache[i%(FIZZ*BUZZ)]
        except KeyError:
            print i+1
[1] https://docs.python.org/2/glossary.html#term-eafp

A generator version:

    from itertools import izip, islice
    
    def fizzes():
        i=0
        while True:
            yield '' if i%3 else 'Fizz'
            i += 1
        
    def buzzes():
        i=0
        while True:
            yield '' if i%5 else 'Buzz'
            i += 1
        
    def numbers():
        i=0
        while True:
            yield str(i+1) if i%3 and i%5 else ''
            i += 1
            
    print "\n".join("".join(parts) for parts in islice(izip(fizzes(), buzzes(), numbers()), 100))

Re: Unpythonic Python

#37
post #3

Playing code golf with Python, this is the shortest I could get: https://gist.github.com/tghw/3702360 Definitely a little unpythonic.

Shorter and more pythonic, but not one per line:

' '.join(["FizzBuzz" if n%15==0 else "Fizz" if n%3==0 else "Buzz" if n%5==0 else str(n) for n in range(1,101)])

Re: Unpythonic Python

#38
post #14

Earlier quoted context omitted.

Something annoying about python is that the word "lambda" is so long for what are supposed to be one-off functions. You have f=lambda x,y=1,a='Fizz',b='Buzz': ... But it's actually shorter to say def f(x,y=1,a='Fizz',b='Buzz'): ... I much prefer Haskell's \x -> x Style lambdas.

I generally don't particularly like giving punctuation meaning that, well, isn't punctuating. The bottleneck when writing code is almost never keypresses. I think CoffeeScript gets it probably the most right of languages I've worked with, in that the choice of punctuation makes intuitive sense... "Hey! It's an Arrow. It goes from here to here". FWIW, I don't like the word "lambda" either... Naming things with one let…

So what about the '*' in C or even better the '.' in so many object oriented language.

> The bottleneck when writing code is almost never keypresses.

No, it's readability and that often comes with terseness. Every line break because I have to spell out lambda or every refactoring that removes the function from its original context so I can avoid a line break is bad for readability. In that aspect the '\' in Haskell does great. Also in Haskell anonymous functions are so important that they definitely deserve their own special character to shorten many expressions.

I only see this becoming a problem if it is used in excess, like in C++ and especially in C++11.

Re: Unpythonic Python

#39
post #25

Earlier quoted context omitted.

The reasoning behind using '\' for lambda in Haskell was because it sort of looks like a lambda character ( λ vs \ ) if you squint hard enough.

That's just horrible. I don't know what more to say about it than that. How did anyone think this would be a good idea on balance?

When you're writing Haskell in emacs (like you should be), you enable haskell-font-lock-symbols in haskell-mode, and it displays the lambda as it should be (plus a bunch of other symbol corrections for ::, ->, =>, (), ||, &&, etc.)

Full list here: https://github.com/haskell/haskell-mode/blob/master/haskell-...

Re: Unpythonic Python

#40
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.
Post reply on HN