On Nov 3, 2006, at 5:25 PM, Jacob Fugal wrote:
I think the first bullet has to do not with the structure of the
statement, just the syntax. It does appear rather hard to follow to
me, and python is a language that prides itself (and I think rightly
so, in most cases) in readability. Compare the same approach -- even
if not equivalent in code -- in Ruby:
begin
range = (1...nums.length)
nums = nums.map{ |item| Integer(item) }
nums.inject{ |x,y| (range === (x - y).abs) ? y : raise)
print "match"
rescue
print "not a match"
end
I much prefer the use of the ternary operator (in limited cases like
this) over an "x and y or z" approach. I also prefer the explicit
exception, like you.
Clearly, breaking the steps out into intermediate calculations makes
it look a little cleaner, but it doesn't really change the syntax
used (aside from adding variable assignment syntax, which is rather
non-functional). Python doesn't have the ternary operator (what I
called a conditional expression). This means you pretty much have to
use and/or for inline conditionals, as far as I know.
What this boils down to is that Python isn't terribly friendly to a
functional programming style, even though it has a lot of the
features that make it possible. It just starts to get a little ugly,
though I think it's probably still fairly clear if you're used to it.
On the second bullet, why not something like:
print(value and 'match' or 'not a match')
That's probably clearer and faster, but the original didn't strike me
as shooting offense. :)
--Levi
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/