Re:Rust and Go
by burntsushi on Saturday November 08, 2014 @12:17AM
(#8576107)
The Haskell programmer in me can't resist---eta reduce your functions! :P
from functools import partial
def odd_count(letters, ch):
return letters.count(ch) % 2
def anagram_of_palindrome(letters):
return sum(map(partial(odd_count, letters), set(letters))) <= 1A more modern take would use collections.Counter(letters) to extract the letter counts, that avoids traversing the string for each letter count:
def anagram_of_palindrome(letters):
return sum(c % 2 for c in Counter(letters).itervalues()) <= 1
But really, unless you are deriving programs by doing algebra you are missing the point of things like map() and reduce() (as far as I know no one is actually doing Functional Programing the way Backus described. Am I wrong? I'd love to be wrong.)
Go read Backus' Turing Award paper: http://web.stanford.edu/class/cs242/readings/backus.pdf