jashkenas--coffeescript/code.jaa

100 lines
1.8 KiB
Plaintext
Raw Normal View History

2009-12-13 23:37:29 +00:00
# TODO: switch/case statements
# Flow: For loops, etc.
# ++ and -- (prefix and postfix)
# Fix array comprehensions -- they're mad busted.
2009-12-15 05:30:26 +00:00
# Start looking up all free assignments in scope -- to var or not to var.
2009-12-15 14:11:27 +00:00
# Literal javascript interpolation.
2009-12-15 05:30:40 +00:00
# Think of a name for this crazy thing.
2009-12-13 22:07:16 +00:00
# Functions:
2009-12-13 23:37:29 +00:00
square: x => x * x.
2009-12-13 22:07:16 +00:00
2009-12-13 23:37:29 +00:00
sum: x, y => x + y.
odd: x => x % 2 is 0.
even: x => x % 2 aint 0.
run_loop: =>
2009-12-14 15:00:31 +00:00
fire_events( e => e.stopPropagation(). )
2009-12-13 23:37:29 +00:00
listen()
wait().
# Objects:
object_literal: {one: 1, two: 2, three: 3}
multiline_object: {
pi: 3.14159
list: [1, 2, 3, 4]
2009-12-14 15:00:31 +00:00
three: new Idea()
2009-12-13 23:37:29 +00:00
inner_obj: {
freedom: => _.freedom().
}
}
# Arrays:
stooges : [{moe: 45}, {curly: 43}, {larry: 46}]
exponents : [x => x., x => x * x., x => x * x * x.]
# Conditionals and ternaries.
if submarine.shields_up
full_speed_ahead()
weapons.fire_torpedos()
else
run_away().
eldest: if 25 > 21 then liz else marge.
2009-12-15 13:53:21 +00:00
decoration: medal_of_honor if war_hero
go_to_sleep() unless coffee
2009-12-13 23:37:29 +00:00
# Returning early:
race: =>
run()
walk()
crawl()
if tired then return sleep().
race().
2009-12-13 22:07:16 +00:00
2009-12-14 01:29:44 +00:00
# Conditional operators:
good ||= evil
wine &&= cheese
2009-12-14 05:06:08 +00:00
# Nested property access and calls.
((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position
2009-12-15 14:11:27 +00:00
# Embedded JavaScript.
callback(
`function(e) { e.stop(); }`
)
# Try/Catch/Finally/Throw.
2009-12-14 15:00:31 +00:00
try
all_hell_breaks_loose()
dogs_and_cats_living_together()
throw "up"
2009-12-14 15:00:31 +00:00
catch error
print( error )
finally
clean_up().
2009-12-13 22:07:16 +00:00
try all_hell_breaks_loose() catch error print(error) finally clean_up().
# While loops.
while demand > supply
sell()
restock().
while supply > demand then buy().
# Unary operators.
!!true
# For loops.
2009-12-15 13:53:21 +00:00
# foods: ['toast', 'wine', 'cheese']
# print(item.capitalize()) for item in foods.
#
# drink(item) for item in foods if item is 'wine'.