Dan Fichter napisaĆ(a):
> I'd like to convince some Python developers that Clojure is not foreign and
> does many things better than Python. I'd appreciate whatever suggestions
> you have about what I've written.
[...]
> Check whether every object in the collection x can be called like a
> function.
>
> [Clojure]
>
> (defn foo [x]
> (every? ifn? x))
>
> [Python]
>
> def foo(x):
> for y in x:
> if not hasattr(y, "__call__"):
> return False
> return True
>
> def foo(x):
> return x == [y for y in x if hasattr(y, "__call__")]
>
> def foo(x):
> return x == filter(lambda y: hasattr(y, "__call__"), x)
>
> Both the Python foo and Clojure foo will work on any type of collection x.
> Clojure has two things Python doesn't: a higher-order function every? that
> returns whether a predicate (true/false-returning) function returns true for
> every element in a collection, and a predicate function ifn? that tells you
> whether its argument implements the function interface.
Small correction here. I would write this function like this:
def foo(x):
return all(callable(f) for f in x)
or like this:
def foo(x):
return all(map(callable, x))
Python does have higher order functions "all" and "any".
They don't take predicates, but using lazy generator expressions
is pretty easy here.
Besides using "callable" instead of "hasattr" is imho more pythonic.
Your comparison is great in showing that Lisp not necessarily
has to be unreadable and incomprehensible for Pythonistas.
In fact Clojure is very clean and readable and my pythonic
background didn't stand in the way of understanding it.
The problem begins when it comes to understand algorithms
written in functional style. For example using "reduce" for Clojurians
seems to be very straightforward and common. For people coming from
imperative world it is not so easy to use and understand.
It would be nice to have something like "Clojure for Python
programmers"
with many examples of algorithms written in Python and Clojure style.
Br,
Rob
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---