How can addition be more easier than this?.... right. Silly me for thinking 1 + 1 is simpler.
print eval('+'.join(map(str, [1, 1])))
How can addition be more easier than this?Yeah, that one's a joke, and not a very good one. Python can do addition just fine, with operators or functions.
print eval('+'.join(map(str, [1, 1])))
print "And now for something completely {0}".format('different')set tabstop=2self inside class definitions (inside of instance methods, self is implied in Ruby).set expandtab!)tabstop=2I think PEP8 has something to say about that.
from __future__ import print_function but I probably won't switch over to Python 3 until it has a killer app.print "I don't like %s" % foodis a mere 3 levels in:
class Diner(object):
def dislike(food):
print "I don't like %s" % food
Whereas yours is, what, 5 levels in?
class Diner(object)
def dislike(*args, **kwargs):
def who_indents(*args, **kwargs):
def like_this_seriously(food):
print "I don't like %s" % food
return like_this_seriously
return who_indents
I use tabs in Python because the style guide says to and because my IDE is set up thusly. But every time I encounter someone who uses 2 spaces or 3 spaces for their indentation, I pine for tabs.import datetime
today = datetime.datetime.now()
last_week = today - datetime.timedelta(days=7)
next_year = today + datetime.timedelta(weeks=52)
print today
print last_week
print next_year
After some of the juggling I had to do with date stuff in Java, this made me weep with joy.Smaller is better.I agree with this.
foods = ['broccoli', 'spinach', 'chocolate']
[eat(food) for food in foods if food != 'chocolate']
CoffeeScript:
foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'
PostScript:
/foods [(broccoli) (spinach) (chocolate)] def
foods { dup (chocolate) ne {eat} {pop} ifelse } forall
JavaScript:
var i, n, food, foods;
foods = ['broccoli', 'spinach', 'chocolate'];
for (i = 0, n = foods.length; i < n; i++) {
food = foods[i];
if (food !== 'chocolate')
eat(food);
}
and just for laughs, PostScript compiled from the JavaScript:
{
[ (broccoli) (spinach) (chocolate) ] js:array
/foods js:=
foods (length) js:. /n js:=
0 /i js:=
{
i dup 1 add /i js:=
pop
}
{
foods i js:index /food js:=
food (chocolate) js:ne js:test
{
[ food ] eat js:call
pop
} if
}
{
i n js:lt js:test
} js:for
} [ /i /n /food /foods ] js:exec
The js2ps compiler does exactly what the JavaScript does, but completely misses what it means, because it doesn't recognize the JavaScript idiom. Beyond that, there are several other low order optimizations missing. Compilers are hard.
JavaScript:
var i, n, food, foods;
foods = ['broccoli', 'spinach', 'chocolate'];
for (i = 0, n = foods.length; i < n; i++) {
food = foods[i];
if (food !== 'chocolate')
eat(food);
}
var foods = ['broccoli', 'spinach', 'chocolate'];
for (i in foods) {
if (foods[i] != 'chocolate')
eat(food);
}
eat(foods[i]);But otherwise fine. :P
foods = ['broccoli', 'spinach', 'chocolate'];
foods.filter(function (item) { return item != 'chocolate'; }).forEach(eat);
foods = ['broccoli', 'spinach', 'chocolate'];
foods.filter(function (item) item != 'chocolate').forEach(eat);
foods = ['broccoli', 'spinach', 'chocolate'];
[eat(food) for each (food in foods) if (food != 'chocolate')];
if (typeof elvis !== "undefined" && elvis !== null) alert("I knew it!");alert "I knew it!" if elvis?if elvis?
alert "I knew it!"race = function (winner){
var runners = arguments.length > 1 ? [].slice.call(arguments, 1) : [];
// do stuff
}race = (winner, runners...) ->
# do stuffrace = function (winner){
var runners = arguments.length > 1 ? [].slice.call(arguments, 1) : [];
// do stuff
} as a likely JavaScript idiom for dealing with multiple trailing arguments, you know a JS dev could have written this:function race(winner,runners) {
//do stuff
} and invoked it using race(winner,[runner1, runner2, ..., runnerN]) to the same effect as your CoffeeScript example (at a cost of one whole additional character)?
class Stuff:
def __init__(self, what):
self.stuff = what
a = Stuff("apple");
b = Stuff("banana");
c = Stuff("carrot");
listHead = a
a.next = b
b.next = c
c.next = None
t = listHead
while t:
print t.stuff
t = t.next
log(9, "hey there, this is pretty serious").log = (log_level, stuff_to_log...)->
if log_level > GLOBAL_LOG_LEVEL
console.log stuff_to_log...function log(log_level){
stuff_to_log = Array.prototype.slice.call(arguments, 1);
if (log_level > GLOBAL_LOG_LEVEL)
console.log.apply(console, stuff_to_log);
}« Older David Remnick: How far can the resistance to Vladi... | 3D street art terracotta Lego ... Newer »
This thread has been archived and is closed to new comments
This is a case where software holy wars make the problem much worse than it has to be.
posted by LogicalDash at 6:13 AM on December 12, 2011