osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tutor] beginning to code


Chris Angelico wrote:
> Rick Johnson wrote:
> > Ruby:
> >     farray = [1.5, 1.9, 2.0, 1.0]
> >     uniqueIntegers = farray.map{|f| f.to_i()}.uniq.length
> >
> > Python:
> >     flist = [1.5, 1.9, 2.0, 1.0]
> >     uniqueIntegers = len(set(map(lambda f:int(f), flist)))
>
> Python:
>
> floats = [1.5, 1.9, 2.0, 1.0]
> unique_integers = len(set(int(f) for f in floats))
>
> or:
>
> unique_integers = len(set(map(int, floats))
>
> If you're going to use Python, at least use it right.

Okay, you've replaced my map function with an implicit list
comprehension (aka: generator expression)... so how is
either more "right" than the other? What is your
justification that your example is "right", and mine is not?

(1) Is is a speed issue? Then prove it.

(2) Is it a readability issue? If so, then that's an opinion
_you_ get to have.

(3) Is a matter of "python purity"?  If so, then map should be
removed from the language. And don't forget to remove reduce
and filter while you're at it. And then, you may want to
grab a shield, because the functional fanboys will be all
over you like white on rice!

(4) Something else...?