4 ms·
The thing that really made my jaw drop the first time I saw it was list comprehensions. allowed_usernames = [user.username for user in all_users if user.is
by stevelosh 16y ago
The thing that really made my jaw drop the first time I saw it was list comprehensions.
allowed_usernames = [user.username for user in all_users if user.is_admin()]
This Stack Overflow answer makes it seem like Java developers should be at least impressed with them as I was: http://stackoverflow.com/questions/899138/python-like-list-comprehension-in-java http://stackoverflow.com/questions/899138/python-like-list-c...
- th0ma5 16y agoone the few reasons i like jython too!
- DanielRibeiro 16y agoIn Ruby, with ruby underscore (http://bit.ly/9dwWAX http://bit.ly/9dwWAX), you can write it as: all_users.select(_.is_admin).map _.username Other languages have similar ways to write it concisely as well (http://bit.ly/a6QmgL http://bit.ly/a6QmgL). Coffeescript (which compiles to javascript, and therefore, can run on Rhinon inside the jvm) in particular: allowed_usernames = user.username for user in all_users when user.is_admin() Yeah. Reminds me of http://gag.googlecode.com/svn/trunk/javadoc/com/google/gag/annotation/remark/ThisWouldBeOneLineIn.html http://gag.googlecode.com/svn/trunk/javadoc/com/google/gag/a...
- generalk 16y agoIn Ruby 1.9, or Ruby 1.8 w/ ActiveSupport, you can use the & operator, which coerces it's argument (in this case, a symbol) to a Proc: all_users.select(&:is_admin).map(&:username) This is a common idiom with a fantastic writeup at http://weblog.raganwald.com/2008/02/1100inject.html http://weblog.raganwald.com/2008/02/1100inject.html
- DanielRibeiro 16y agoActually on ruby 1.8.7 the proc coercion is default. No need for active support. And yeah, ruby underscore is overkill for this trivial example.
- swannodette 16y agoThis feature is of course lifted from Haskell - another jaw dropping language.
- stevelosh 16y agoYep. Python has a wonderful habit of taking the awesome features of other languages and expanding on them. List comprehensions were added in 2.0, generator comprehensions were added in 2.4, and dictionary comprehensions came in 2.7
- nickbarnwell 16y agoJust found a reason to upgrade to 2.7...
- stralep 16y ago"Haskell is a scripting language inspired by Python." :) http://www.haskell.org/pipermail/haskell-cafe/2010-November/085925.html http://www.haskell.org/pipermail/haskell-cafe/2010-November/... (joke)
- InclinedPlane 16y ago.NET languages (C#, powershell, for example) have list comprehensions as well. They seem to be unappreciated languages amongst the "cool kids" due to their Microsoftian origin.
- patrickaljord 16y agoOr maybe it's because powershell and C# only run on windows and are not open source and we're not interested in using a technology that is playing catch up with a moving target (mono).
- InclinedPlane 16y agoSince you claim C# only runs on Windows and yet mention mono in the same sentence I'll pretend you said instead that mono is not production ready (which I think I could buy, at least for SaaS). As far as "playing catch up with a moving target", I don't see how that's terribly relevant, the end result is still a platform that continues to improve and advance. If the C# code that runs on Windows and mono is still more advanced than, say, Java or Perl, and continues to advance (even though not as fast as C# on Windows alone) then that's still a benefit. Given the advent of monotouch and the coming of monodroid it seems like even a hard-core MS hater could come to appreciate C#.
- patrickaljord 16y ago> If the C# code that runs on Windows and mono is still more advanced than, say, Java or Perl The problem is that it's not. You can do anything you want when it comes to SaaS using python, ruby, php, node or java. Actually those have huge open source communities with tons of libs available to make work faster. This is not the case of C# and if it ever gets there, it probably won't be any better. So again, I don't see any reason to use it, and plenty of reasons not to and being a "hard-core MS hater" would probably come at the bottom of the list of reasons not to use C#, don't try to justify the lack of interest in MS by some kind of imaginary persecution. But to each his own , if you're happy with it go for it.
- whakojacko 16y agoYou can do similar in scala, either val allowed_usernames = all_users.filter(_.is_admin()).map(_.username) or val allowed_usernames = for(user <- all_users if user.is_admin()) { yield user.username } And still get all the of goodness of Trivial jvm/java compatability...
- moshezadka 16y agoNothing against scala, but you can program in Python and get all the goodness of JVM compat by simply running your programs in Jython...
- lusis 16y agoJython is behind current python implementations. If you're comfortable with being compatible up to python 2.5 then go right ahead. But if you need 2.6 or 2.7 compatibility, Jython is sadly not an option.
- jcromartie 16y agoOr Clojure... (or... or... or...) (map :username (filter admin? all-users))
- Scriptor 16y agoOr Clojure again (for [username all-users] :when (admin? username))
- m_myers 16y agoYes, terseness is definitely not among Java's virtues. Your way looks a lot more fun to write than mine -- it's amazing what a little well-placed syntactic sugar can do.
- RyanMcGreal 16y agoThis blew my mind when I first came across it: deck = ['%s%s' % (n, s) for s in 'HDCS' for n in '23456789XJQKA'] It creates a deck of cards. Also, you can easily shuffle the deck using the standard library. import random random.shuffle(deck)