Live data from Hacker News

Unpythonic Python

skien.cc

121–130 of 156 posts

Re: Unpythonic Python

#121
post #110
post #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.

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.

Pipeline your generator through a logging generator :)

Re: Unpythonic Python

#123

Earlier quoted context omitted.

It's not just more complexity per line, it's also a higher level of complexity, using language-specific features that people who aren't fluent in python wouldn't be familiar with (multiplying a string by a boolean)

By that logic, no one should write anything in idiomatic French because anyone who isn't fluent in French wouldn't be able to read it.

I was curious about that. Is multiplying a string by a boolean idiomatic Python? I don't write nearly enough Python to know, but it strikes me that this might be more like writing French using lots of obscure words.

Re: Unpythonic Python

#124

Earlier quoted context omitted.

It's not just more complexity per line, it's also a higher level of complexity, using language-specific features that people who aren't fluent in python wouldn't be familiar with (multiplying a string by a boolean)

By that logic, no one should write anything in idiomatic French because anyone who isn't fluent in French wouldn't be able to read it.

Well, that example is a bit different. You're ignoring who the audience is. When writing code, the audience is usually at least people on the same team, if not the person writing in the first place.

If writing in French for people who are not fluent, I think it would be a good idea to avoid idiomatic language.

Re: Unpythonic Python

#125
post #114
post #108

Earlier quoted context omitted.

Good idea. Enhancing: If, lambda, logical operators except == and string concatenation free version (Python 3) for x in range( 1, 101 ): print( [ x,'Buzz','Fizz','FizzBuzz' ][ (x%3==0)*2 + (x%5==0) ] )

And also C based on the same idea as my Python (but this has to ? for 0s): int i; char* a[] = { 0, "Buzz", "Fizz", "FizzBuzz" }; char* f[] = { "%s\n", "%d\n" }; for ( i = 1; i

Don't ruin that version with an if statement.

Use this and avoid it( and the second array as well ):

  char * a[] = { "%d\n" , "Fizz\n" , "Buzz\n" , "FizzBuzz\n" } ;

Re: Unpythonic Python

#126
My short yet readable Python version:

    for i in range(1,101):
        if not i % 15: print 'FizzBuzz'
        elif not i % 5: print 'Buzz'
        elif not i % 3: print 'Fizz'
        else: print i
The same in Bash:

    for i in {1..100}
    do
        let "$i % 15" || { echo "FizzBuzz"; continue; }
        let "$i % 5" || { echo "Buzz"; continue; }
        let "$i % 3" || { echo "Fizz"; continue; }
        echo $i
    done

Re: Unpythonic Python

#127
post #45
post #24

Earlier quoted context omitted.

I cannot agree enough. Someone give me a call when a Haskell-like language with readable syntax appears. I really like the ideas behind Haskell, but the syntax is so unfriendly that I cannot be bothered to really start using it. Less special characters, please.

There really aren't that many special characters (certainly no requirement for non-ascii) in the core language - perhaps the sin is allowing almost unlimited user defined operators in code, which leads to monstrosities like ekmett's lens infix operators: http://hackage.haskell.org/package/lens-4.1.2/docs/Control-L... As a lisper, I prefer names and prefix operators - there's a few exceptions where infix operators add…

I really like scheme & clojure and I think I have done a lot more scheme and clojure than haskell. (And even a alot more of Python). However, I think that Haskell by itself has a great syntax (with some legacy cruft, granted).

What is really great is how Haskell enables you to write extremely succinct code by providing powerful abstractions and tools. The fact that you have a type annotation already gives enough meta information in the code that you often can omit a long name. The fact that Haskell functions tend to be short in lines gives you something that is closer to an equation than a command language.

I don't know how to put this in words, but I'll try: Naming things well in programming is the most difficult task after mastering the very basics. I think this is because names are not fundamental for algorithms and programs but are like bookmarks for the programmer and their human brain. In C, a function with one-letter argument names is a nightmare, in Haskell, it actually improves readablity over a verbose version with whole-word names. And then, I have seen a lot of functions that were named very specific in terms of a problem the programmer tried to solve at the time, that however where quite generic, or could have been very generic (think of `sort_customers_by_name()` instead of a `sort_by()`.

I share your dislike of extreme operator usage. It is a problem that whatever permutation of +-!@#! are used as operators in libraries for functionality that the user rarely needs. But the general approach of haskell for succinctness is a good one.

Re: Unpythonic Python

#128
post #114

Earlier quoted context omitted.

And also C based on the same idea as my Python (but this has to ? for 0s): int i; char* a[] = { 0, "Buzz", "Fizz", "FizzBuzz" }; char* f[] = { "%s\n", "%d\n" }; for ( i = 1; i

Don't ruin that version with an if statement. Use this and avoid it( and the second array as well ): char * a[] = { "%d\n" , "Fizz\n" , "Buzz\n" , "FizzBuzz\n" } ;

Brilliant, thanks! The C version is now:

    int i; char* a[] = { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" };
    for ( i = 1; i 

Re: Unpythonic Python

#129
post #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.

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

#130

Nobody ever seems to go for the general solution: words = ( (3, 'Fizz'), (5, 'Buzz') ) def fizzbuzz(num): for value, word in words: if i % value == 0: yield word for i in range(1, 101): print ''.join(fizzbuzz(i)) or i

It works even though it should be:

    words = ((3, 'Fizz'), (5, 'Buzz'))


    def fizzbuzz(num):
        for value, word in words:
            if num % value == 0:
                yield word

    for i in range(1, 101):
        print ''.join(fizzbuzz(i)) or i
Post reply on HN