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.
Unpythonic Python
121–130 of 156 posts
Re: Unpythonic Python
#122Re: Unpythonic Python
#123Earlier 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.
Re: Unpythonic Python
#124Earlier 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.
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
#125Earlier 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
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 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
doneRe: Unpythonic Python
#127Earlier 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…
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
#128Earlier 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" } ;
int i; char* a[] = { "%d\n", "Fizz\n", "Buzz\n", "FizzBuzz\n" };
for ( i = 1; i Re: Unpythonic Python
#129When 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.
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
#130Nobody 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
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