1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/code.cs

122 lines
2 KiB
C#
Raw Normal View History

2009-12-16 20:04:01 -05:00
# TODO: Add range indexing: array[5..7] => array.slice(5, 7)
2009-12-13 17:07:16 -05:00
# Functions:
square: x => x * x.
sum: x, y => x + y.
odd: x => x % 2 is 0.
even: x => x % 2 aint 0.
run_loop: =>
fire_events( e => e.stopPropagation(). )
listen()
wait().
# Objects:
dense_object_literal: {one: 1, two: 2, three: 3}
spaced_out_multiline_object: {
2009-12-13 17:07:16 -05:00
2009-12-13 18:37:29 -05:00
pi: 3.14159
2009-12-13 18:37:29 -05:00
list: [1, 2, 3, 4]
2009-12-14 10:00:31 -05:00
three: new Idea()
2009-12-13 18:37:29 -05:00
inner_obj: {
freedom: => _.freedom().
}
2009-12-13 18:37:29 -05:00
}
# Arrays:
stooges : [{moe: 45}, {curly: 43}, {larry: 46}]
exponents : [x => x., x => x * x., x => x * x * x.]
empty: []
2009-12-13 18:37:29 -05:00
# Conditionals and ternaries.
if submarine.shields_up
full_speed_ahead()
2009-12-15 20:49:24 -05:00
fire_torpedos()
2009-12-13 18:37:29 -05:00
else
run_away().
eldest: if 25 > 21 then liz else marge.
2009-12-15 08:53:21 -05:00
decoration: medal_of_honor if war_hero
go_to_sleep() unless coffee
2009-12-13 18:37:29 -05:00
# Returning early:
race: =>
run()
walk()
crawl()
if tired then return sleep().
race().
2009-12-13 17:07:16 -05:00
2009-12-13 20:29:44 -05:00
# Conditional operators:
good ||= evil
wine &&= cheese
2009-12-14 00:06:08 -05:00
# Nested property access and calls.
((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position
2009-12-15 10:35:17 -05:00
a: b: c: 5
2009-12-15 09:11:27 -05:00
# Embedded JavaScript.
callback(
`function(e) { e.stop(); }`
)
# Try/Catch/Finally/Throw.
2009-12-14 10:00:31 -05:00
try
all_hell_breaks_loose()
dogs_and_cats_living_together()
throw "up"
2009-12-14 10:00:31 -05:00
catch error
print( error )
finally
clean_up().
2009-12-13 17:07:16 -05: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
# Lexical scoping.
a: 5
change_a_and_set_b: =>
a: 10
b: 15.
b: 20
# Array comprehensions.
supper: food.capitalize() for food in ['toast', 'cheese', 'wine'].
drink(bottle) for bottle, i in ['soda', 'wine', 'lemonade'] if even(i).
2009-12-15 22:30:27 -05:00
# Switch statements.
switch day
case "Tuesday" then eat_breakfast()
case "Sunday" then go_to_church()
2009-12-15 22:38:17 -05:00
case "Saturday" then go_to_the_park()
2009-12-15 22:30:27 -05:00
case "Wednesday"
eat_breakfast()
go_to_work()
eat_dinner()
default go_to_work().
2009-12-16 20:19:52 -05:00
# Semicolons can optionally be used instead of newlines.
wednesday: => eat_breakfast(); go_to_work(); eat_dinner(); .