mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-17 19:11:22 -05:00
trying out new arrows for function literals -> is a function, => is a bound function
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
backwards: =>
|
backwards: ->
|
||||||
alert arguments.reverse()
|
alert arguments.reverse()
|
||||||
|
|
||||||
backwards "stairway", "to", "heaven"
|
backwards "stairway", "to", "heaven"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
$('table.list').each (table) =>
|
$('table.list').each (table) ->
|
||||||
$('tr.account', table).each (row) =>
|
$('tr.account', table).each (row) ->
|
||||||
row.show()
|
row.show()
|
||||||
row.highlight()
|
row.highlight()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
grade: (student) =>
|
grade: (student) ->
|
||||||
if student.excellent_work
|
if student.excellent_work
|
||||||
"A+"
|
"A+"
|
||||||
else if student.okay_stuff
|
else if student.okay_stuff
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
square: (x) => x * x
|
square: (x) -> x * x
|
||||||
cube: (x) => square(x) * x
|
cube: (x) -> square(x) * x
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
Account: (customer, cart) =>
|
Account: (customer, cart) ->
|
||||||
this.customer: customer
|
this.customer: customer
|
||||||
this.cart: cart
|
this.cart: cart
|
||||||
|
|
||||||
$('.shopping_cart').bind 'click', (event) ==>
|
$('.shopping_cart').bind 'click', (event) =>
|
||||||
this.customer.purchase this.cart
|
this.customer.purchase this.cart
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
weather_report: (location) =>
|
weather_report: (location) ->
|
||||||
# Make an Ajax request to fetch the weather...
|
# Make an Ajax request to fetch the weather...
|
||||||
[location, 72, "Mostly Sunny"]
|
[location, 72, "Mostly Sunny"]
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ opposite_day: true
|
|||||||
number: -42 if opposite_day
|
number: -42 if opposite_day
|
||||||
|
|
||||||
# Functions:
|
# Functions:
|
||||||
square: (x) => x * x
|
square: (x) -> x * x
|
||||||
|
|
||||||
# Arrays:
|
# Arrays:
|
||||||
list: [1, 2, 3, 4, 5]
|
list: [1, 2, 3, 4, 5]
|
||||||
@@ -15,11 +15,11 @@ list: [1, 2, 3, 4, 5]
|
|||||||
math: {
|
math: {
|
||||||
root: Math.sqrt
|
root: Math.sqrt
|
||||||
square: square
|
square: square
|
||||||
cube: (x) => x * square x
|
cube: (x) -> x * square x
|
||||||
}
|
}
|
||||||
|
|
||||||
# Splats:
|
# Splats:
|
||||||
race: (winner, runners...) =>
|
race: (winner, runners...) ->
|
||||||
print winner, runners
|
print winner, runners
|
||||||
|
|
||||||
# Existence:
|
# Existence:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
countdown: num for num in [10..1]
|
countdown: num for num in [10..1]
|
||||||
|
|
||||||
egg_delivery: =>
|
egg_delivery: ->
|
||||||
for i in [0...eggs.length] by 12
|
for i in [0...eggs.length] by 12
|
||||||
dozen_eggs: eggs[i...i+12]
|
dozen_eggs: eggs[i...i+12]
|
||||||
deliver new egg_carton(dozen)
|
deliver new egg_carton(dozen)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
num: 1
|
num: 1
|
||||||
change_numbers: =>
|
change_numbers: ->
|
||||||
new_num: -1
|
new_num: -1
|
||||||
num: 10
|
num: 10
|
||||||
new_num: change_numbers()
|
new_num: change_numbers()
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
gold: silver: the_field: "unknown"
|
gold: silver: the_field: "unknown"
|
||||||
|
|
||||||
medalists: (first, second, rest...) =>
|
medalists: (first, second, rest...) ->
|
||||||
gold: first
|
gold: first
|
||||||
silver: second
|
silver: second
|
||||||
the_field: rest
|
the_field: rest
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
Animal: =>
|
Animal: ->
|
||||||
Animal::move: (meters) =>
|
Animal::move: (meters) ->
|
||||||
alert this.name + " moved " + meters + "m."
|
alert this.name + " moved " + meters + "m."
|
||||||
|
|
||||||
Snake: (name) => this.name: name
|
Snake: (name) -> this.name: name
|
||||||
Snake extends Animal
|
Snake extends Animal
|
||||||
Snake::move: =>
|
Snake::move: ->
|
||||||
alert "Slithering..."
|
alert "Slithering..."
|
||||||
super 5
|
super 5
|
||||||
|
|
||||||
Horse: (name) => this.name: name
|
Horse: (name) -> this.name: name
|
||||||
Horse extends Animal
|
Horse extends Animal
|
||||||
Horse::move: =>
|
Horse::move: ->
|
||||||
alert "Galloping..."
|
alert "Galloping..."
|
||||||
super 45
|
super 45
|
||||||
|
|
||||||
|
|||||||
@@ -174,7 +174,7 @@ gem install coffee-script</pre>
|
|||||||
<td><code>-e, --eval</code></td>
|
<td><code>-e, --eval</code></td>
|
||||||
<td>
|
<td>
|
||||||
Compile and print a little snippet of CoffeeScript directly from the
|
Compile and print a little snippet of CoffeeScript directly from the
|
||||||
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: (x) => x * x"</tt>
|
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: (x) -> x * x"</tt>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -256,13 +256,13 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
|||||||
<p id="functions">
|
<p id="functions">
|
||||||
<b class="header">Functions and Invocation</b>
|
<b class="header">Functions and Invocation</b>
|
||||||
Functions are defined by a list of parameters, an arrow, and the
|
Functions are defined by a list of parameters, an arrow, and the
|
||||||
function body. The empty function looks like this: <tt>=></tt>. All
|
function body. The empty function looks like this: <tt>-></tt>. All
|
||||||
functions in CoffeeScript are named by default, for easier debugging.
|
functions in CoffeeScript are named by default, for easier debugging.
|
||||||
</p>
|
</p>
|
||||||
<%= code_for('functions', 'cube(5)') %>
|
<%= code_for('functions', 'cube(5)') %>
|
||||||
<p>
|
<p>
|
||||||
If you'd like to create an anonymous function, just wrap it in parentheses:
|
If you'd like to create an anonymous function, just wrap it in parentheses:
|
||||||
<tt>((x) => x * x)</tt>
|
<tt>((x) -> x * x)</tt>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p id="assignment">
|
<p id="assignment">
|
||||||
@@ -547,7 +547,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
|||||||
|
|
||||||
<p id="long_arrow">
|
<p id="long_arrow">
|
||||||
<b class="header">Function binding</b>
|
<b class="header">Function binding</b>
|
||||||
The long arrow <tt>==></tt> can be used to both define a function, and to bind
|
The long arrow <tt>=></tt> can be used to both define a function, and to bind
|
||||||
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
||||||
when using callback-based libraries like Prototype or jQuery, for creating
|
when using callback-based libraries like Prototype or jQuery, for creating
|
||||||
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
# The implementation of binary search that is tested.
|
# The implementation of binary search that is tested.
|
||||||
|
|
||||||
# Return the index of an element in a sorted list. (or -1, if not present)
|
# Return the index of an element in a sorted list. (or -1, if not present)
|
||||||
index: (list, target) =>
|
index: (list, target) ->
|
||||||
[low, high]: [0, list.length]
|
[low, high]: [0, list.length]
|
||||||
while low < high
|
while low < high
|
||||||
mid: (low + high) >> 1
|
mid: (low + high) >> 1
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
# Beautiful Code, Chapter 3.
|
# Beautiful Code, Chapter 3.
|
||||||
# Produces the expected runtime of Quicksort, for every integer from 1 to N.
|
# Produces the expected runtime of Quicksort, for every integer from 1 to N.
|
||||||
|
|
||||||
runtime: (N) =>
|
runtime: (N) ->
|
||||||
[sum, t]: [0, 0]
|
[sum, t]: [0, 0]
|
||||||
for n in [1..N]
|
for n in [1..N]
|
||||||
sum += 2 * t
|
sum += 2 * t
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
# '.', '^', '$', and '*'.
|
# '.', '^', '$', and '*'.
|
||||||
|
|
||||||
# Search for the regexp anywhere in the text.
|
# Search for the regexp anywhere in the text.
|
||||||
match: (regexp, text) =>
|
match: (regexp, text) ->
|
||||||
return match_here(regexp.slice(1), text) if regexp[0] is '^'
|
return match_here(regexp.slice(1), text) if regexp[0] is '^'
|
||||||
while text
|
while text
|
||||||
return true if match_here(regexp, text)
|
return true if match_here(regexp, text)
|
||||||
@@ -11,7 +11,7 @@ match: (regexp, text) =>
|
|||||||
false
|
false
|
||||||
|
|
||||||
# Search for the regexp at the beginning of the text.
|
# Search for the regexp at the beginning of the text.
|
||||||
match_here: (regexp, text) =>
|
match_here: (regexp, text) ->
|
||||||
[cur, next]: [regexp[0], regexp[1]]
|
[cur, next]: [regexp[0], regexp[1]]
|
||||||
if regexp.length is 0 then return true
|
if regexp.length is 0 then return true
|
||||||
if next is '*' then return match_star(cur, regexp.slice(2), text)
|
if next is '*' then return match_star(cur, regexp.slice(2), text)
|
||||||
@@ -20,7 +20,7 @@ match_here: (regexp, text) =>
|
|||||||
false
|
false
|
||||||
|
|
||||||
# Search for a kleene star match at the beginning of the text.
|
# Search for a kleene star match at the beginning of the text.
|
||||||
match_star: (c, regexp, text) =>
|
match_star: (c, regexp, text) ->
|
||||||
while true
|
while true
|
||||||
return true if match_here(regexp, text)
|
return true if match_here(regexp, text)
|
||||||
return false unless text and (text[0] is c or c is '.')
|
return false unless text and (text[0] is c or c is '.')
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
# Functions:
|
# Functions:
|
||||||
square: (x) => x * x
|
square: (x) -> x * x
|
||||||
|
|
||||||
sum: (x, y) => x + y
|
sum: (x, y) -> x + y
|
||||||
|
|
||||||
odd: (x) => x % 2 isnt 0
|
odd: (x) -> x % 2 isnt 0
|
||||||
|
|
||||||
even: (x) => x % 2 is 0
|
even: (x) -> x % 2 is 0
|
||||||
|
|
||||||
run_loop: =>
|
run_loop: ->
|
||||||
fire_events((e) => e.stopPropagation())
|
fire_events((e) -> e.stopPropagation())
|
||||||
listen()
|
listen()
|
||||||
wait()
|
wait()
|
||||||
|
|
||||||
@@ -22,14 +22,14 @@ spaced_out_multiline_object: {
|
|||||||
three: new Idea()
|
three: new Idea()
|
||||||
|
|
||||||
inner_obj: {
|
inner_obj: {
|
||||||
freedom: => _.freedom()
|
freedom: -> _.freedom()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Arrays:
|
# Arrays:
|
||||||
stooges: [{moe: 45}, {curly: 43}, {larry: 46}]
|
stooges: [{moe: 45}, {curly: 43}, {larry: 46}]
|
||||||
|
|
||||||
exponents: [(x) => x, (x) => x * x, (x) => x * x * x]
|
exponents: [(x) -> x, (x) -> x * x, (x) -> x * x * x]
|
||||||
|
|
||||||
empty: []
|
empty: []
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ decoration: medal_of_honor if war_hero
|
|||||||
go_to_sleep() unless coffee
|
go_to_sleep() unless coffee
|
||||||
|
|
||||||
# Returning early:
|
# Returning early:
|
||||||
race: =>
|
race: ->
|
||||||
run()
|
run()
|
||||||
walk()
|
walk()
|
||||||
crawl()
|
crawl()
|
||||||
@@ -103,7 +103,7 @@ while true
|
|||||||
|
|
||||||
# Lexical scoping.
|
# Lexical scoping.
|
||||||
v_1: 5
|
v_1: 5
|
||||||
change_a_and_set_b: =>
|
change_a_and_set_b: ->
|
||||||
v_1: 10
|
v_1: 10
|
||||||
v_2: 15
|
v_2: 15
|
||||||
v_2: 20
|
v_2: 20
|
||||||
@@ -128,7 +128,7 @@ activity: switch day
|
|||||||
else go_to_work()
|
else go_to_work()
|
||||||
|
|
||||||
# Semicolons can optionally be used instead of newlines.
|
# Semicolons can optionally be used instead of newlines.
|
||||||
wednesday: => eat_breakfast(); go_to_work(); eat_dinner()
|
wednesday: -> eat_breakfast(); go_to_work(); eat_dinner()
|
||||||
|
|
||||||
# Array slice literals.
|
# Array slice literals.
|
||||||
zero_to_nine: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
zero_to_nine: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
@@ -140,19 +140,19 @@ sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna
|
|||||||
aliquam erat volutpat. Ut wisi enim ad."
|
aliquam erat volutpat. Ut wisi enim ad."
|
||||||
|
|
||||||
# Inheritance and calling super.
|
# Inheritance and calling super.
|
||||||
Animal: =>
|
Animal: ->
|
||||||
Animal::move: (meters) =>
|
Animal::move: (meters) ->
|
||||||
alert(this.name + " moved " + meters + "m.")
|
alert(this.name + " moved " + meters + "m.")
|
||||||
|
|
||||||
Snake: (name) => this.name: name
|
Snake: (name) -> this.name: name
|
||||||
Snake extends Animal
|
Snake extends Animal
|
||||||
Snake::move: =>
|
Snake::move: ->
|
||||||
alert('Slithering...')
|
alert('Slithering...')
|
||||||
super(5)
|
super(5)
|
||||||
|
|
||||||
Horse: (name) => this.name: name
|
Horse: (name) -> this.name: name
|
||||||
Horse extends Animal
|
Horse extends Animal
|
||||||
Horse::move: =>
|
Horse::move: ->
|
||||||
alert('Galloping...')
|
alert('Galloping...')
|
||||||
super(45)
|
super(45)
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Uses a binary search algorithm to locate a value in the specified array.
|
# Uses a binary search algorithm to locate a value in the specified array.
|
||||||
binary_search: (items, value) =>
|
binary_search: (items, value) ->
|
||||||
|
|
||||||
start: 0
|
start: 0
|
||||||
stop: items.length - 1
|
stop: items.length - 1
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# A bubble sort implementation, sorting the given array in-place.
|
# A bubble sort implementation, sorting the given array in-place.
|
||||||
bubble_sort: (list) =>
|
bubble_sort: (list) ->
|
||||||
for i in [0...list.length]
|
for i in [0...list.length]
|
||||||
for j in [0...list.length - i]
|
for j in [0...list.length - i]
|
||||||
[list[j], list[j+1]]: [list[j+1], list[j]] if list[j] > list[j+1]
|
[list[j], list[j+1]]: [list[j+1], list[j]] if list[j] > list[j+1]
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
# "Classic" linked list implementation that doesn't keep track of its size.
|
# "Classic" linked list implementation that doesn't keep track of its size.
|
||||||
LinkedList: =>
|
LinkedList: ->
|
||||||
this._head: null # Pointer to the first item in the list.
|
this._head: null # Pointer to the first item in the list.
|
||||||
|
|
||||||
|
|
||||||
# Appends some data to the end of the list. This method traverses the existing
|
# Appends some data to the end of the list. This method traverses the existing
|
||||||
# list and places the value at the end in a new node.
|
# list and places the value at the end in a new node.
|
||||||
LinkedList::add: (data) =>
|
LinkedList::add: (data) ->
|
||||||
|
|
||||||
# Create a new node object to wrap the data.
|
# Create a new node object to wrap the data.
|
||||||
node: {data: data, next: null}
|
node: {data: data, next: null}
|
||||||
@@ -20,7 +20,7 @@ LinkedList::add: (data) =>
|
|||||||
|
|
||||||
|
|
||||||
# Retrieves the data at the given position in the list.
|
# Retrieves the data at the given position in the list.
|
||||||
LinkedList::item: (index) =>
|
LinkedList::item: (index) ->
|
||||||
|
|
||||||
# Check for out-of-bounds values.
|
# Check for out-of-bounds values.
|
||||||
return null if index < 0
|
return null if index < 0
|
||||||
@@ -36,7 +36,7 @@ LinkedList::item: (index) =>
|
|||||||
|
|
||||||
|
|
||||||
# Remove the item from the given location in the list.
|
# Remove the item from the given location in the list.
|
||||||
LinkedList::remove: (index) =>
|
LinkedList::remove: (index) ->
|
||||||
|
|
||||||
# Check for out-of-bounds values.
|
# Check for out-of-bounds values.
|
||||||
return null if index < 0
|
return null if index < 0
|
||||||
@@ -60,7 +60,7 @@ LinkedList::remove: (index) =>
|
|||||||
|
|
||||||
|
|
||||||
# Calculate the number of items in the list.
|
# Calculate the number of items in the list.
|
||||||
LinkedList::size: =>
|
LinkedList::size: ->
|
||||||
current: this._head
|
current: this._head
|
||||||
count: 0
|
count: 0
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ LinkedList::size: =>
|
|||||||
|
|
||||||
|
|
||||||
# Convert the list into an array.
|
# Convert the list into an array.
|
||||||
LinkedList::toArray: =>
|
LinkedList::toArray: ->
|
||||||
result: []
|
result: []
|
||||||
current: this._head
|
current: this._head
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ LinkedList::toArray: =>
|
|||||||
|
|
||||||
|
|
||||||
# The string representation of the linked list.
|
# The string representation of the linked list.
|
||||||
LinkedList::toString: => this.toArray().toString()
|
LinkedList::toString: -> this.toArray().toString()
|
||||||
|
|
||||||
|
|
||||||
# Tests.
|
# Tests.
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
# numbers, national insurance numbers, etc.
|
# numbers, national insurance numbers, etc.
|
||||||
# See: http://en.wikipedia.org/wiki/Luhn_algorithm
|
# See: http://en.wikipedia.org/wiki/Luhn_algorithm
|
||||||
|
|
||||||
is_valid_identifier: (identifier) =>
|
is_valid_identifier: (identifier) ->
|
||||||
|
|
||||||
sum: 0
|
sum: 0
|
||||||
alt: false
|
alt: false
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# Sorts an array in ascending natural order using merge sort.
|
# Sorts an array in ascending natural order using merge sort.
|
||||||
merge_sort: (list) =>
|
merge_sort: (list) ->
|
||||||
|
|
||||||
return list if list.length is 1
|
return list if list.length is 1
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# An in-place selection sort.
|
# An in-place selection sort.
|
||||||
selection_sort: (list) =>
|
selection_sort: (list) ->
|
||||||
len: list.length
|
len: list.length
|
||||||
|
|
||||||
# For each item in the list.
|
# For each item in the list.
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
# ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
|
# ['toast', 'cheese', 'wine'].each { |food| print food.capitalize }
|
||||||
|
|
||||||
['toast', 'wine', 'cheese'].each (food) => print(food.capitalize())
|
['toast', 'wine', 'cheese'].each (food) -> print(food.capitalize())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -14,10 +14,10 @@
|
|||||||
# end
|
# end
|
||||||
|
|
||||||
LotteryTicket: {
|
LotteryTicket: {
|
||||||
get_picks: => this.picks
|
get_picks: -> this.picks
|
||||||
set_picks: (nums) => this.picks: nums
|
set_picks: (nums) -> this.picks: nums
|
||||||
get_purchase: => this.purchase
|
get_purchase: -> this.purchase
|
||||||
set_purchase: (amount) => this.purchase: amount
|
set_purchase: (amount) -> this.purchase: amount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -40,11 +40,11 @@ LotteryTicket: {
|
|||||||
# end
|
# end
|
||||||
|
|
||||||
LotteryDraw: {
|
LotteryDraw: {
|
||||||
play: =>
|
play: ->
|
||||||
result: LotteryTicket.new_random()
|
result: LotteryTicket.new_random()
|
||||||
winners: {}
|
winners: {}
|
||||||
this.tickets.each (buyer, ticket_list) =>
|
this.tickets.each (buyer, ticket_list) ->
|
||||||
ticket_list.each (ticket) =>
|
ticket_list.each (ticket) ->
|
||||||
score: ticket.score(result)
|
score: ticket.score(result)
|
||||||
return if score is 0
|
return if score is 0
|
||||||
winners[buyer] ||= []
|
winners[buyer] ||= []
|
||||||
@@ -65,8 +65,8 @@ LotteryDraw: {
|
|||||||
# end
|
# end
|
||||||
|
|
||||||
WishScanner: {
|
WishScanner: {
|
||||||
scan_for_a_wish: =>
|
scan_for_a_wish: ->
|
||||||
wish: this.read().detect((thought) => thought.index('wish: ') is 0)
|
wish: this.read().detect((thought) -> thought.index('wish: ') is 0)
|
||||||
wish.replace('wish: ', '')
|
wish.replace('wish: ', '')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ WishScanner: {
|
|||||||
Creature : {
|
Creature : {
|
||||||
|
|
||||||
# This method applies a hit taken during a fight.
|
# This method applies a hit taken during a fight.
|
||||||
hit: (damage) =>
|
hit: (damage) ->
|
||||||
p_up: Math.rand(this.charisma)
|
p_up: Math.rand(this.charisma)
|
||||||
if p_up % 9 is 7
|
if p_up % 9 is 7
|
||||||
this.life += p_up / 4
|
this.life += p_up / 4
|
||||||
@@ -120,7 +120,7 @@ Creature : {
|
|||||||
if this.life <= 0 then puts("[" + this.name + " has died.]")
|
if this.life <= 0 then puts("[" + this.name + " has died.]")
|
||||||
|
|
||||||
# This method takes one turn in a fight.
|
# This method takes one turn in a fight.
|
||||||
fight: (enemy, weapon) =>
|
fight: (enemy, weapon) ->
|
||||||
if this.life <= 0 then return puts("[" + this.name + "is too dead to fight!]")
|
if this.life <= 0 then return puts("[" + this.name + "is too dead to fight!]")
|
||||||
|
|
||||||
# Attack the opponent.
|
# Attack the opponent.
|
||||||
@@ -156,12 +156,12 @@ Creature : {
|
|||||||
# Get evil idea and swap in code words
|
# Get evil idea and swap in code words
|
||||||
print("Enter your new idea: ")
|
print("Enter your new idea: ")
|
||||||
idea: gets()
|
idea: gets()
|
||||||
code_words.each((real, code) => idea.replace(real, code))
|
code_words.each((real, code) -> idea.replace(real, code))
|
||||||
|
|
||||||
# Save the jibberish to a new file
|
# Save the jibberish to a new file
|
||||||
print("File encoded. Please enter a name for this idea: ")
|
print("File encoded. Please enter a name for this idea: ")
|
||||||
idea_name: gets().strip()
|
idea_name: gets().strip()
|
||||||
File.open("idea-" + idea_name + '.txt', 'w', (file) => file.write(idea))
|
File.open("idea-" + idea_name + '.txt', 'w', (file) -> file.write(idea))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -177,7 +177,7 @@ File.open("idea-" + idea_name + '.txt', 'w', (file) => file.write(idea))
|
|||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
wipe_mutterings_from: (sentence) =>
|
wipe_mutterings_from: (sentence) ->
|
||||||
throw new Error("cannot wipe mutterings") unless sentence.indexOf
|
throw new Error("cannot wipe mutterings") unless sentence.indexOf
|
||||||
while sentence.indexOf('(') >= 0
|
while sentence.indexOf('(') >= 0
|
||||||
open: sentence.indexOf('(') - 1
|
open: sentence.indexOf('(') - 1
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ print("Odelay!") for i in [1..5]
|
|||||||
# add = (x, y): x + y.
|
# add = (x, y): x + y.
|
||||||
# add(2, 4) string print
|
# add(2, 4) string print
|
||||||
|
|
||||||
add: (x, y) => x + y
|
add: (x, y) -> x + y
|
||||||
print(add(2, 4))
|
print(add(2, 4))
|
||||||
|
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@ print({language: 'Potion', pointless: true}['language'])
|
|||||||
# minus = (x, y): x - y.
|
# minus = (x, y): x - y.
|
||||||
# minus (y=10, x=6)
|
# minus (y=10, x=6)
|
||||||
|
|
||||||
minus: (x, y) => x - y
|
minus: (x, y) -> x - y
|
||||||
minus(6, 10)
|
minus(6, 10)
|
||||||
|
|
||||||
|
|
||||||
@@ -53,8 +53,8 @@ for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'}
|
|||||||
# Person print = ():
|
# Person print = ():
|
||||||
# ('My name is ', /name, '.') join print.
|
# ('My name is ', /name, '.') join print.
|
||||||
|
|
||||||
Person: =>
|
Person: ->
|
||||||
Person::print: =>
|
Person::print: ->
|
||||||
print('My name is ' + this.name + '.')
|
print('My name is ' + this.name + '.')
|
||||||
|
|
||||||
|
|
||||||
@@ -71,9 +71,9 @@ print(p.name)
|
|||||||
#
|
#
|
||||||
# Policeman ('Constable') print
|
# Policeman ('Constable') print
|
||||||
|
|
||||||
Policeman: (rank) => this.rank: rank
|
Policeman: (rank) -> this.rank: rank
|
||||||
Policeman extends Person
|
Policeman extends Person
|
||||||
Policeman::print: =>
|
Policeman::print: ->
|
||||||
print('My name is ' + this.name + " and I'm a " + this.rank + '.')
|
print('My name is ' + this.name + " and I'm a " + this.rank + '.')
|
||||||
|
|
||||||
print(new Policeman('Constable'))
|
print(new Policeman('Constable'))
|
||||||
@@ -115,13 +115,13 @@ table: {
|
|||||||
# String length = (): 10.
|
# String length = (): 10.
|
||||||
|
|
||||||
# this foul business...
|
# this foul business...
|
||||||
String::length: => 10
|
String::length: -> 10
|
||||||
|
|
||||||
|
|
||||||
# block = :
|
# block = :
|
||||||
# 'potion' print.
|
# 'potion' print.
|
||||||
|
|
||||||
block: =>
|
block: ->
|
||||||
print('potion')
|
print('potion')
|
||||||
|
|
||||||
|
|
||||||
@@ -178,7 +178,7 @@ if (3).gender?
|
|||||||
# HomePage get = (url):
|
# HomePage get = (url):
|
||||||
# session = url query ? at ('session').
|
# session = url query ? at ('session').
|
||||||
|
|
||||||
HomePage::get: (url) =>
|
HomePage::get: (url) ->
|
||||||
session: url.query.session if url.query?
|
session: url.query.session if url.query?
|
||||||
|
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ HomePage::get: (url) =>
|
|||||||
# b /left = BTree ()
|
# b /left = BTree ()
|
||||||
# b /right = BTree ()
|
# b /right = BTree ()
|
||||||
|
|
||||||
BTree: =>
|
BTree: ->
|
||||||
b: new BTree()
|
b: new BTree()
|
||||||
b.left: new BTree()
|
b.left: new BTree()
|
||||||
b.right: new BTree()
|
b.right: new BTree()
|
||||||
@@ -199,7 +199,7 @@ b.right: new BTree()
|
|||||||
# if (b ? /left):
|
# if (b ? /left):
|
||||||
# 'left path found!' print.
|
# 'left path found!' print.
|
||||||
|
|
||||||
BTree: =>
|
BTree: ->
|
||||||
b: new BTree()
|
b: new BTree()
|
||||||
|
|
||||||
print('left path found!') if b.left?
|
print('left path found!') if b.left?
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
# If Underscore is called as a function, it returns a wrapped object that
|
# If Underscore is called as a function, it returns a wrapped object that
|
||||||
# can be used OO-style. This wrapper holds altered versions of all the
|
# can be used OO-style. This wrapper holds altered versions of all the
|
||||||
# underscore functions. Wrapped objects may be chained.
|
# underscore functions. Wrapped objects may be chained.
|
||||||
wrapper: (obj) =>
|
wrapper: (obj) ->
|
||||||
this._wrapped: obj
|
this._wrapped: obj
|
||||||
this
|
this
|
||||||
|
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
|
|
||||||
# Create a safe reference to the Underscore object forreference below.
|
# Create a safe reference to the Underscore object forreference below.
|
||||||
_: root._: (obj) => new wrapper(obj)
|
_: root._: (obj) -> new wrapper(obj)
|
||||||
|
|
||||||
|
|
||||||
# Export the Underscore object for CommonJS.
|
# Export the Underscore object for CommonJS.
|
||||||
@@ -54,7 +54,7 @@
|
|||||||
|
|
||||||
# The cornerstone, an each implementation.
|
# The cornerstone, an each implementation.
|
||||||
# Handles objects implementing forEach, arrays, and raw objects.
|
# Handles objects implementing forEach, arrays, and raw objects.
|
||||||
_.each: (obj, iterator, context) =>
|
_.each: (obj, iterator, context) ->
|
||||||
index: 0
|
index: 0
|
||||||
try
|
try
|
||||||
return obj.forEach(iterator, context) if obj.forEach
|
return obj.forEach(iterator, context) if obj.forEach
|
||||||
@@ -68,36 +68,36 @@
|
|||||||
|
|
||||||
# Return the results of applying the iterator to each element. Use JavaScript
|
# Return the results of applying the iterator to each element. Use JavaScript
|
||||||
# 1.6's version of map, if possible.
|
# 1.6's version of map, if possible.
|
||||||
_.map: (obj, iterator, context) =>
|
_.map: (obj, iterator, context) ->
|
||||||
return obj.map(iterator, context) if (obj and _.isFunction(obj.map))
|
return obj.map(iterator, context) if (obj and _.isFunction(obj.map))
|
||||||
results: []
|
results: []
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
results.push(iterator.call(context, value, index, list))
|
results.push(iterator.call(context, value, index, list))
|
||||||
results
|
results
|
||||||
|
|
||||||
|
|
||||||
# Reduce builds up a single result from a list of values. Also known as
|
# Reduce builds up a single result from a list of values. Also known as
|
||||||
# inject, or foldl. Uses JavaScript 1.8's version of reduce, if possible.
|
# inject, or foldl. Uses JavaScript 1.8's version of reduce, if possible.
|
||||||
_.reduce: (obj, memo, iterator, context) =>
|
_.reduce: (obj, memo, iterator, context) ->
|
||||||
return obj.reduce(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduce))
|
return obj.reduce(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduce))
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
memo: iterator.call(context, memo, value, index, list)
|
memo: iterator.call(context, memo, value, index, list)
|
||||||
memo
|
memo
|
||||||
|
|
||||||
|
|
||||||
# The right-associative version of reduce, also known as foldr. Uses
|
# The right-associative version of reduce, also known as foldr. Uses
|
||||||
# JavaScript 1.8's version of reduceRight, if available.
|
# JavaScript 1.8's version of reduceRight, if available.
|
||||||
_.reduceRight: (obj, memo, iterator, context) =>
|
_.reduceRight: (obj, memo, iterator, context) ->
|
||||||
return obj.reduceRight(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduceRight))
|
return obj.reduceRight(_.bind(iterator, context), memo) if (obj and _.isFunction(obj.reduceRight))
|
||||||
_.each _.clone(_.toArray(obj)).reverse(), (value, index) =>
|
_.each _.clone(_.toArray(obj)).reverse(), (value, index) ->
|
||||||
memo: iterator.call(context, memo, value, index, obj)
|
memo: iterator.call(context, memo, value, index, obj)
|
||||||
memo
|
memo
|
||||||
|
|
||||||
|
|
||||||
# Return the first value which passes a truth test.
|
# Return the first value which passes a truth test.
|
||||||
_.detect: (obj, iterator, context) =>
|
_.detect: (obj, iterator, context) ->
|
||||||
result: null
|
result: null
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
if iterator.call(context, value, index, list)
|
if iterator.call(context, value, index, list)
|
||||||
result: value
|
result: value
|
||||||
_.breakLoop()
|
_.breakLoop()
|
||||||
@@ -106,47 +106,47 @@
|
|||||||
|
|
||||||
# Return all the elements that pass a truth test. Use JavaScript 1.6's
|
# Return all the elements that pass a truth test. Use JavaScript 1.6's
|
||||||
# filter(), if it exists.
|
# filter(), if it exists.
|
||||||
_.select: (obj, iterator, context) =>
|
_.select: (obj, iterator, context) ->
|
||||||
if obj and _.isFunction(obj.filter) then return obj.filter(iterator, context)
|
if obj and _.isFunction(obj.filter) then return obj.filter(iterator, context)
|
||||||
results: []
|
results: []
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
results.push(value) if iterator.call(context, value, index, list)
|
results.push(value) if iterator.call(context, value, index, list)
|
||||||
results
|
results
|
||||||
|
|
||||||
|
|
||||||
# Return all the elements for which a truth test fails.
|
# Return all the elements for which a truth test fails.
|
||||||
_.reject: (obj, iterator, context) =>
|
_.reject: (obj, iterator, context) ->
|
||||||
results: []
|
results: []
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
results.push(value) if not iterator.call(context, value, index, list)
|
results.push(value) if not iterator.call(context, value, index, list)
|
||||||
results
|
results
|
||||||
|
|
||||||
|
|
||||||
# Determine whether all of the elements match a truth test. Delegate to
|
# Determine whether all of the elements match a truth test. Delegate to
|
||||||
# JavaScript 1.6's every(), if it is present.
|
# JavaScript 1.6's every(), if it is present.
|
||||||
_.all: (obj, iterator, context) =>
|
_.all: (obj, iterator, context) ->
|
||||||
iterator ||= _.identity
|
iterator ||= _.identity
|
||||||
return obj.every(iterator, context) if obj and _.isFunction(obj.every)
|
return obj.every(iterator, context) if obj and _.isFunction(obj.every)
|
||||||
result: true
|
result: true
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
_.breakLoop() unless (result: result and iterator.call(context, value, index, list))
|
_.breakLoop() unless (result: result and iterator.call(context, value, index, list))
|
||||||
result
|
result
|
||||||
|
|
||||||
|
|
||||||
# Determine if at least one element in the object matches a truth test. Use
|
# Determine if at least one element in the object matches a truth test. Use
|
||||||
# JavaScript 1.6's some(), if it exists.
|
# JavaScript 1.6's some(), if it exists.
|
||||||
_.any: (obj, iterator, context) =>
|
_.any: (obj, iterator, context) ->
|
||||||
iterator ||= _.identity
|
iterator ||= _.identity
|
||||||
return obj.some(iterator, context) if obj and _.isFunction(obj.some)
|
return obj.some(iterator, context) if obj and _.isFunction(obj.some)
|
||||||
result: false
|
result: false
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
_.breakLoop() if (result: iterator.call(context, value, index, list))
|
_.breakLoop() if (result: iterator.call(context, value, index, list))
|
||||||
result
|
result
|
||||||
|
|
||||||
|
|
||||||
# Determine if a given value is included in the array or object,
|
# Determine if a given value is included in the array or object,
|
||||||
# based on '==='.
|
# based on '==='.
|
||||||
_.include: (obj, target) =>
|
_.include: (obj, target) ->
|
||||||
return _.indexOf(obj, target) isnt -1 if _.isArray(obj)
|
return _.indexOf(obj, target) isnt -1 if _.isArray(obj)
|
||||||
for key, val of obj
|
for key, val of obj
|
||||||
return true if val is target
|
return true if val is target
|
||||||
@@ -154,41 +154,41 @@
|
|||||||
|
|
||||||
|
|
||||||
# Invoke a method with arguments on every item in a collection.
|
# Invoke a method with arguments on every item in a collection.
|
||||||
_.invoke: (obj, method) =>
|
_.invoke: (obj, method) ->
|
||||||
args: _.rest(arguments, 2)
|
args: _.rest(arguments, 2)
|
||||||
(if method then val[method] else val).apply(val, args) for val in obj
|
(if method then val[method] else val).apply(val, args) for val in obj
|
||||||
|
|
||||||
|
|
||||||
# Convenience version of a common use case of map: fetching a property.
|
# Convenience version of a common use case of map: fetching a property.
|
||||||
_.pluck: (obj, key) =>
|
_.pluck: (obj, key) ->
|
||||||
_.map(obj, ((val) => val[key]))
|
_.map(obj, ((val) -> val[key]))
|
||||||
|
|
||||||
|
|
||||||
# Return the maximum item or (item-based computation).
|
# Return the maximum item or (item-based computation).
|
||||||
_.max: (obj, iterator, context) =>
|
_.max: (obj, iterator, context) ->
|
||||||
return Math.max.apply(Math, obj) if not iterator and _.isArray(obj)
|
return Math.max.apply(Math, obj) if not iterator and _.isArray(obj)
|
||||||
result: {computed: -Infinity}
|
result: {computed: -Infinity}
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
computed: if iterator then iterator.call(context, value, index, list) else value
|
computed: if iterator then iterator.call(context, value, index, list) else value
|
||||||
computed >= result.computed and (result: {value: value, computed: computed})
|
computed >= result.computed and (result: {value: value, computed: computed})
|
||||||
result.value
|
result.value
|
||||||
|
|
||||||
|
|
||||||
# Return the minimum element (or element-based computation).
|
# Return the minimum element (or element-based computation).
|
||||||
_.min: (obj, iterator, context) =>
|
_.min: (obj, iterator, context) ->
|
||||||
return Math.min.apply(Math, obj) if not iterator and _.isArray(obj)
|
return Math.min.apply(Math, obj) if not iterator and _.isArray(obj)
|
||||||
result: {computed: Infinity}
|
result: {computed: Infinity}
|
||||||
_.each obj, (value, index, list) =>
|
_.each obj, (value, index, list) ->
|
||||||
computed: if iterator then iterator.call(context, value, index, list) else value
|
computed: if iterator then iterator.call(context, value, index, list) else value
|
||||||
computed < result.computed and (result: {value: value, computed: computed})
|
computed < result.computed and (result: {value: value, computed: computed})
|
||||||
result.value
|
result.value
|
||||||
|
|
||||||
|
|
||||||
# Sort the object's values by a criteria produced by an iterator.
|
# Sort the object's values by a criteria produced by an iterator.
|
||||||
_.sortBy: (obj, iterator, context) =>
|
_.sortBy: (obj, iterator, context) ->
|
||||||
_.pluck(((_.map obj, (value, index, list) =>
|
_.pluck(((_.map obj, (value, index, list) ->
|
||||||
{value: value, criteria: iterator.call(context, value, index, list)}
|
{value: value, criteria: iterator.call(context, value, index, list)}
|
||||||
).sort((left, right) =>
|
).sort((left, right) ->
|
||||||
a: left.criteria; b: right.criteria
|
a: left.criteria; b: right.criteria
|
||||||
if a < b then -1 else if a > b then 1 else 0
|
if a < b then -1 else if a > b then 1 else 0
|
||||||
)), 'value')
|
)), 'value')
|
||||||
@@ -196,7 +196,7 @@
|
|||||||
|
|
||||||
# Use a comparator function to figure out at what index an object should
|
# Use a comparator function to figure out at what index an object should
|
||||||
# be inserted so as to maintain order. Uses binary search.
|
# be inserted so as to maintain order. Uses binary search.
|
||||||
_.sortedIndex: (array, obj, iterator) =>
|
_.sortedIndex: (array, obj, iterator) ->
|
||||||
iterator ||= _.identity
|
iterator ||= _.identity
|
||||||
low: 0; high: array.length
|
low: 0; high: array.length
|
||||||
while low < high
|
while low < high
|
||||||
@@ -206,7 +206,7 @@
|
|||||||
|
|
||||||
|
|
||||||
# Convert anything iterable into a real, live array.
|
# Convert anything iterable into a real, live array.
|
||||||
_.toArray: (iterable) =>
|
_.toArray: (iterable) ->
|
||||||
return [] if (!iterable)
|
return [] if (!iterable)
|
||||||
return iterable.toArray() if (iterable.toArray)
|
return iterable.toArray() if (iterable.toArray)
|
||||||
return iterable if (_.isArray(iterable))
|
return iterable if (_.isArray(iterable))
|
||||||
@@ -215,7 +215,7 @@
|
|||||||
|
|
||||||
|
|
||||||
# Return the number of elements in an object.
|
# Return the number of elements in an object.
|
||||||
_.size: (obj) => _.toArray(obj).length
|
_.size: (obj) -> _.toArray(obj).length
|
||||||
|
|
||||||
|
|
||||||
# -------------------------- Array Functions: ------------------------------
|
# -------------------------- Array Functions: ------------------------------
|
||||||
@@ -223,7 +223,7 @@
|
|||||||
# Get the first element of an array. Passing "n" will return the first N
|
# Get the first element of an array. Passing "n" will return the first N
|
||||||
# values in the array. Aliased as "head". The "guard" check allows it to work
|
# values in the array. Aliased as "head". The "guard" check allows it to work
|
||||||
# with _.map.
|
# with _.map.
|
||||||
_.first: (array, n, guard) =>
|
_.first: (array, n, guard) ->
|
||||||
if n and not guard then slice.call(array, 0, n) else array[0]
|
if n and not guard then slice.call(array, 0, n) else array[0]
|
||||||
|
|
||||||
|
|
||||||
@@ -231,35 +231,35 @@
|
|||||||
# Especially useful on the arguments object. Passing an "index" will return
|
# Especially useful on the arguments object. Passing an "index" will return
|
||||||
# the rest of the values in the array from that index onward. The "guard"
|
# the rest of the values in the array from that index onward. The "guard"
|
||||||
# check allows it to work with _.map.
|
# check allows it to work with _.map.
|
||||||
_.rest: (array, index, guard) =>
|
_.rest: (array, index, guard) ->
|
||||||
slice.call(array, if _.isUndefined(index) or guard then 1 else index)
|
slice.call(array, if _.isUndefined(index) or guard then 1 else index)
|
||||||
|
|
||||||
|
|
||||||
# Get the last element of an array.
|
# Get the last element of an array.
|
||||||
_.last: (array) => array[array.length - 1]
|
_.last: (array) -> array[array.length - 1]
|
||||||
|
|
||||||
|
|
||||||
# Trim out all falsy values from an array.
|
# Trim out all falsy values from an array.
|
||||||
_.compact: (array) => array[i] for i in [0...array.length] when array[i]
|
_.compact: (array) -> array[i] for i in [0...array.length] when array[i]
|
||||||
|
|
||||||
|
|
||||||
# Return a completely flattened version of an array.
|
# Return a completely flattened version of an array.
|
||||||
_.flatten: (array) =>
|
_.flatten: (array) ->
|
||||||
_.reduce array, [], (memo, value) =>
|
_.reduce array, [], (memo, value) ->
|
||||||
return memo.concat(_.flatten(value)) if _.isArray(value)
|
return memo.concat(_.flatten(value)) if _.isArray(value)
|
||||||
memo.push(value)
|
memo.push(value)
|
||||||
memo
|
memo
|
||||||
|
|
||||||
|
|
||||||
# Return a version of the array that does not contain the specified value(s).
|
# Return a version of the array that does not contain the specified value(s).
|
||||||
_.without: (array) =>
|
_.without: (array) ->
|
||||||
values: _.rest(arguments)
|
values: _.rest(arguments)
|
||||||
val for val in _.toArray(array) when not _.include(values, val)
|
val for val in _.toArray(array) when not _.include(values, val)
|
||||||
|
|
||||||
|
|
||||||
# Produce a duplicate-free version of the array. If the array has already
|
# Produce a duplicate-free version of the array. If the array has already
|
||||||
# been sorted, you have the option of using a faster algorithm.
|
# been sorted, you have the option of using a faster algorithm.
|
||||||
_.uniq: (array, isSorted) =>
|
_.uniq: (array, isSorted) ->
|
||||||
memo: []
|
memo: []
|
||||||
for el, i in _.toArray(array)
|
for el, i in _.toArray(array)
|
||||||
memo.push(el) if i is 0 || (if isSorted is true then _.last(memo) isnt el else not _.include(memo, el))
|
memo.push(el) if i is 0 || (if isSorted is true then _.last(memo) isnt el else not _.include(memo, el))
|
||||||
@@ -268,16 +268,16 @@
|
|||||||
|
|
||||||
# Produce an array that contains every item shared between all the
|
# Produce an array that contains every item shared between all the
|
||||||
# passed-in arrays.
|
# passed-in arrays.
|
||||||
_.intersect: (array) =>
|
_.intersect: (array) ->
|
||||||
rest: _.rest(arguments)
|
rest: _.rest(arguments)
|
||||||
_.select _.uniq(array), (item) =>
|
_.select _.uniq(array), (item) ->
|
||||||
_.all rest, (other) =>
|
_.all rest, (other) ->
|
||||||
_.indexOf(other, item) >= 0
|
_.indexOf(other, item) >= 0
|
||||||
|
|
||||||
|
|
||||||
# Zip together multiple lists into a single array -- elements that share
|
# Zip together multiple lists into a single array -- elements that share
|
||||||
# an index go together.
|
# an index go together.
|
||||||
_.zip: =>
|
_.zip: ->
|
||||||
length: _.max(_.pluck(arguments, 'length'))
|
length: _.max(_.pluck(arguments, 'length'))
|
||||||
results: new Array(length)
|
results: new Array(length)
|
||||||
for i in [0...length]
|
for i in [0...length]
|
||||||
@@ -288,7 +288,7 @@
|
|||||||
# If the browser doesn't supply us with indexOf (I'm looking at you, MSIE),
|
# If the browser doesn't supply us with indexOf (I'm looking at you, MSIE),
|
||||||
# we need this function. Return the position of the first occurence of an
|
# we need this function. Return the position of the first occurence of an
|
||||||
# item in an array, or -1 if the item is not included in the array.
|
# item in an array, or -1 if the item is not included in the array.
|
||||||
_.indexOf: (array, item) =>
|
_.indexOf: (array, item) ->
|
||||||
return array.indexOf(item) if array.indexOf
|
return array.indexOf(item) if array.indexOf
|
||||||
i: 0; l: array.length
|
i: 0; l: array.length
|
||||||
while l - i
|
while l - i
|
||||||
@@ -298,7 +298,7 @@
|
|||||||
|
|
||||||
# Provide JavaScript 1.6's lastIndexOf, delegating to the native function,
|
# Provide JavaScript 1.6's lastIndexOf, delegating to the native function,
|
||||||
# if possible.
|
# if possible.
|
||||||
_.lastIndexOf: (array, item) =>
|
_.lastIndexOf: (array, item) ->
|
||||||
return array.lastIndexOf(item) if array.lastIndexOf
|
return array.lastIndexOf(item) if array.lastIndexOf
|
||||||
i: array.length
|
i: array.length
|
||||||
while i
|
while i
|
||||||
@@ -309,7 +309,7 @@
|
|||||||
# Generate an integer Array containing an arithmetic progression. A port of
|
# Generate an integer Array containing an arithmetic progression. A port of
|
||||||
# the native Python range() function. See:
|
# the native Python range() function. See:
|
||||||
# http://docs.python.org/library/functions.html#range
|
# http://docs.python.org/library/functions.html#range
|
||||||
_.range: (start, stop, step) =>
|
_.range: (start, stop, step) ->
|
||||||
a: arguments
|
a: arguments
|
||||||
solo: a.length <= 1
|
solo: a.length <= 1
|
||||||
i: start: if solo then 0 else a[0];
|
i: start: if solo then 0 else a[0];
|
||||||
@@ -330,44 +330,44 @@
|
|||||||
|
|
||||||
# Create a function bound to a given object (assigning 'this', and arguments,
|
# Create a function bound to a given object (assigning 'this', and arguments,
|
||||||
# optionally). Binding with arguments is also known as 'curry'.
|
# optionally). Binding with arguments is also known as 'curry'.
|
||||||
_.bind: (func, obj) =>
|
_.bind: (func, obj) ->
|
||||||
args: _.rest(arguments, 2)
|
args: _.rest(arguments, 2)
|
||||||
=> func.apply(obj or root, args.concat(arguments))
|
-> func.apply(obj or root, args.concat(arguments))
|
||||||
|
|
||||||
|
|
||||||
# Bind all of an object's methods to that object. Useful for ensuring that
|
# Bind all of an object's methods to that object. Useful for ensuring that
|
||||||
# all callbacks defined on an object belong to it.
|
# all callbacks defined on an object belong to it.
|
||||||
_.bindAll: (obj) =>
|
_.bindAll: (obj) ->
|
||||||
funcs: if arguments.length > 1 then _.rest(arguments) else _.functions(obj)
|
funcs: if arguments.length > 1 then _.rest(arguments) else _.functions(obj)
|
||||||
_.each(funcs, (f) => obj[f]: _.bind(obj[f], obj))
|
_.each(funcs, (f) -> obj[f]: _.bind(obj[f], obj))
|
||||||
obj
|
obj
|
||||||
|
|
||||||
|
|
||||||
# Delays a function for the given number of milliseconds, and then calls
|
# Delays a function for the given number of milliseconds, and then calls
|
||||||
# it with the arguments supplied.
|
# it with the arguments supplied.
|
||||||
_.delay: (func, wait) =>
|
_.delay: (func, wait) ->
|
||||||
args: _.rest(arguments, 2)
|
args: _.rest(arguments, 2)
|
||||||
setTimeout((=> func.apply(func, args)), wait)
|
setTimeout((-> func.apply(func, args)), wait)
|
||||||
|
|
||||||
|
|
||||||
# Defers a function, scheduling it to run after the current call stack has
|
# Defers a function, scheduling it to run after the current call stack has
|
||||||
# cleared.
|
# cleared.
|
||||||
_.defer: (func) =>
|
_.defer: (func) ->
|
||||||
_.delay.apply(_, [func, 1].concat(_.rest(arguments)))
|
_.delay.apply(_, [func, 1].concat(_.rest(arguments)))
|
||||||
|
|
||||||
|
|
||||||
# Returns the first function passed as an argument to the second,
|
# Returns the first function passed as an argument to the second,
|
||||||
# allowing you to adjust arguments, run code before and after, and
|
# allowing you to adjust arguments, run code before and after, and
|
||||||
# conditionally execute the original function.
|
# conditionally execute the original function.
|
||||||
_.wrap: (func, wrapper) =>
|
_.wrap: (func, wrapper) ->
|
||||||
=> wrapper.apply(wrapper, [func].concat(arguments))
|
-> wrapper.apply(wrapper, [func].concat(arguments))
|
||||||
|
|
||||||
|
|
||||||
# Returns a function that is the composition of a list of functions, each
|
# Returns a function that is the composition of a list of functions, each
|
||||||
# consuming the return value of the function that follows.
|
# consuming the return value of the function that follows.
|
||||||
_.compose: =>
|
_.compose: ->
|
||||||
funcs: arguments
|
funcs: arguments
|
||||||
=>
|
->
|
||||||
args: arguments
|
args: arguments
|
||||||
for i in [(funcs.length - 1)..0]
|
for i in [(funcs.length - 1)..0]
|
||||||
args: [funcs[i].apply(this, args)]
|
args: [funcs[i].apply(this, args)]
|
||||||
@@ -377,43 +377,43 @@
|
|||||||
# ------------------------- Object Functions: ----------------------------
|
# ------------------------- Object Functions: ----------------------------
|
||||||
|
|
||||||
# Retrieve the names of an object's properties.
|
# Retrieve the names of an object's properties.
|
||||||
_.keys: (obj) =>
|
_.keys: (obj) ->
|
||||||
return _.range(0, obj.length) if _.isArray(obj)
|
return _.range(0, obj.length) if _.isArray(obj)
|
||||||
key for key, val of obj
|
key for key, val of obj
|
||||||
|
|
||||||
|
|
||||||
# Retrieve the values of an object's properties.
|
# Retrieve the values of an object's properties.
|
||||||
_.values: (obj) =>
|
_.values: (obj) ->
|
||||||
_.map(obj, _.identity)
|
_.map(obj, _.identity)
|
||||||
|
|
||||||
|
|
||||||
# Return a sorted list of the function names available in Underscore.
|
# Return a sorted list of the function names available in Underscore.
|
||||||
_.functions: (obj) =>
|
_.functions: (obj) ->
|
||||||
_.select(_.keys(obj), (key) => _.isFunction(obj[key])).sort()
|
_.select(_.keys(obj), (key) -> _.isFunction(obj[key])).sort()
|
||||||
|
|
||||||
|
|
||||||
# Extend a given object with all of the properties in a source object.
|
# Extend a given object with all of the properties in a source object.
|
||||||
_.extend: (destination, source) =>
|
_.extend: (destination, source) ->
|
||||||
for key, val of source
|
for key, val of source
|
||||||
destination[key]: val
|
destination[key]: val
|
||||||
destination
|
destination
|
||||||
|
|
||||||
|
|
||||||
# Create a (shallow-cloned) duplicate of an object.
|
# Create a (shallow-cloned) duplicate of an object.
|
||||||
_.clone: (obj) =>
|
_.clone: (obj) ->
|
||||||
return obj.slice(0) if _.isArray(obj)
|
return obj.slice(0) if _.isArray(obj)
|
||||||
_.extend({}, obj)
|
_.extend({}, obj)
|
||||||
|
|
||||||
|
|
||||||
# Invokes interceptor with the obj, and then returns obj.
|
# Invokes interceptor with the obj, and then returns obj.
|
||||||
# The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
|
# The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
|
||||||
_.tap: (obj, interceptor) =>
|
_.tap: (obj, interceptor) ->
|
||||||
interceptor(obj)
|
interceptor(obj)
|
||||||
obj
|
obj
|
||||||
|
|
||||||
|
|
||||||
# Perform a deep comparison to check if two objects are equal.
|
# Perform a deep comparison to check if two objects are equal.
|
||||||
_.isEqual: (a, b) =>
|
_.isEqual: (a, b) ->
|
||||||
# Check object identity.
|
# Check object identity.
|
||||||
return true if a is b
|
return true if a is b
|
||||||
# Different types?
|
# Different types?
|
||||||
@@ -449,81 +449,81 @@
|
|||||||
|
|
||||||
|
|
||||||
# Is a given array or object empty?
|
# Is a given array or object empty?
|
||||||
_.isEmpty: (obj) => _.keys(obj).length is 0
|
_.isEmpty: (obj) -> _.keys(obj).length is 0
|
||||||
|
|
||||||
|
|
||||||
# Is a given value a DOM element?
|
# Is a given value a DOM element?
|
||||||
_.isElement: (obj) => obj and obj.nodeType is 1
|
_.isElement: (obj) -> obj and obj.nodeType is 1
|
||||||
|
|
||||||
|
|
||||||
# Is a given value an array?
|
# Is a given value an array?
|
||||||
_.isArray: (obj) => !!(obj and obj.concat and obj.unshift)
|
_.isArray: (obj) -> !!(obj and obj.concat and obj.unshift)
|
||||||
|
|
||||||
|
|
||||||
# Is a given variable an arguments object?
|
# Is a given variable an arguments object?
|
||||||
_.isArguments: (obj) => obj and _.isNumber(obj.length) and !_.isArray(obj) and !propertyIsEnumerable.call(obj, 'length')
|
_.isArguments: (obj) -> obj and _.isNumber(obj.length) and !_.isArray(obj) and !propertyIsEnumerable.call(obj, 'length')
|
||||||
|
|
||||||
|
|
||||||
# Is the given value a function?
|
# Is the given value a function?
|
||||||
_.isFunction: (obj) => !!(obj and obj.constructor and obj.call and obj.apply)
|
_.isFunction: (obj) -> !!(obj and obj.constructor and obj.call and obj.apply)
|
||||||
|
|
||||||
|
|
||||||
# Is the given value a string?
|
# Is the given value a string?
|
||||||
_.isString: (obj) => !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
|
_.isString: (obj) -> !!(obj is '' or (obj and obj.charCodeAt and obj.substr))
|
||||||
|
|
||||||
|
|
||||||
# Is a given value a number?
|
# Is a given value a number?
|
||||||
_.isNumber: (obj) => toString.call(obj) is '[object Number]'
|
_.isNumber: (obj) -> toString.call(obj) is '[object Number]'
|
||||||
|
|
||||||
|
|
||||||
# Is a given value a Date?
|
# Is a given value a Date?
|
||||||
_.isDate: (obj) => !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
|
_.isDate: (obj) -> !!(obj and obj.getTimezoneOffset and obj.setUTCFullYear)
|
||||||
|
|
||||||
|
|
||||||
# Is the given value a regular expression?
|
# Is the given value a regular expression?
|
||||||
_.isRegExp: (obj) => !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
|
_.isRegExp: (obj) -> !!(obj and obj.exec and (obj.ignoreCase or obj.ignoreCase is false))
|
||||||
|
|
||||||
|
|
||||||
# Is the given value NaN -- this one is interesting. NaN != NaN, and
|
# Is the given value NaN -- this one is interesting. NaN != NaN, and
|
||||||
# isNaN(undefined) == true, so we make sure it's a number first.
|
# isNaN(undefined) == true, so we make sure it's a number first.
|
||||||
_.isNaN: (obj) => _.isNumber(obj) and window.isNaN(obj)
|
_.isNaN: (obj) -> _.isNumber(obj) and window.isNaN(obj)
|
||||||
|
|
||||||
|
|
||||||
# Is a given value equal to null?
|
# Is a given value equal to null?
|
||||||
_.isNull: (obj) => obj is null
|
_.isNull: (obj) -> obj is null
|
||||||
|
|
||||||
|
|
||||||
# Is a given variable undefined?
|
# Is a given variable undefined?
|
||||||
_.isUndefined: (obj) => typeof obj is 'undefined'
|
_.isUndefined: (obj) -> typeof obj is 'undefined'
|
||||||
|
|
||||||
|
|
||||||
# -------------------------- Utility Functions: --------------------------
|
# -------------------------- Utility Functions: --------------------------
|
||||||
|
|
||||||
# Run Underscore.js in noConflict mode, returning the '_' variable to its
|
# Run Underscore.js in noConflict mode, returning the '_' variable to its
|
||||||
# previous owner. Returns a reference to the Underscore object.
|
# previous owner. Returns a reference to the Underscore object.
|
||||||
_.noConflict: =>
|
_.noConflict: ->
|
||||||
root._: previousUnderscore
|
root._: previousUnderscore
|
||||||
this
|
this
|
||||||
|
|
||||||
|
|
||||||
# Keep the identity function around for default iterators.
|
# Keep the identity function around for default iterators.
|
||||||
_.identity: (value) => value
|
_.identity: (value) -> value
|
||||||
|
|
||||||
|
|
||||||
# Break out of the middle of an iteration.
|
# Break out of the middle of an iteration.
|
||||||
_.breakLoop: => throw breaker
|
_.breakLoop: -> throw breaker
|
||||||
|
|
||||||
|
|
||||||
# Generate a unique integer id (unique within the entire client session).
|
# Generate a unique integer id (unique within the entire client session).
|
||||||
# Useful for temporary DOM ids.
|
# Useful for temporary DOM ids.
|
||||||
idCounter: 0
|
idCounter: 0
|
||||||
_.uniqueId: (prefix) =>
|
_.uniqueId: (prefix) ->
|
||||||
(prefix or '') + idCounter++
|
(prefix or '') + idCounter++
|
||||||
|
|
||||||
|
|
||||||
# JavaScript templating a-la ERB, pilfered from John Resig's
|
# JavaScript templating a-la ERB, pilfered from John Resig's
|
||||||
# "Secrets of the JavaScript Ninja", page 83.
|
# "Secrets of the JavaScript Ninja", page 83.
|
||||||
_.template: (str, data) =>
|
_.template: (str, data) ->
|
||||||
`var fn = new Function('obj',
|
`var fn = new Function('obj',
|
||||||
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
'var p=[],print=function(){p.push.apply(p,arguments);};' +
|
||||||
'with(obj){p.push(\'' +
|
'with(obj){p.push(\'' +
|
||||||
@@ -555,38 +555,38 @@
|
|||||||
# /*------------------------ Setup the OOP Wrapper: --------------------------*/
|
# /*------------------------ Setup the OOP Wrapper: --------------------------*/
|
||||||
|
|
||||||
# Helper function to continue chaining intermediate results.
|
# Helper function to continue chaining intermediate results.
|
||||||
result: (obj, chain) =>
|
result: (obj, chain) ->
|
||||||
if chain then _(obj).chain() else obj
|
if chain then _(obj).chain() else obj
|
||||||
|
|
||||||
|
|
||||||
# Add all of the Underscore functions to the wrapper object.
|
# Add all of the Underscore functions to the wrapper object.
|
||||||
_.each _.functions(_), (name) =>
|
_.each _.functions(_), (name) ->
|
||||||
method: _[name]
|
method: _[name]
|
||||||
wrapper.prototype[name]: =>
|
wrapper.prototype[name]: ->
|
||||||
unshift.call(arguments, this._wrapped)
|
unshift.call(arguments, this._wrapped)
|
||||||
result(method.apply(_, args), this._chain)
|
result(method.apply(_, args), this._chain)
|
||||||
|
|
||||||
|
|
||||||
# Add all mutator Array functions to the wrapper.
|
# Add all mutator Array functions to the wrapper.
|
||||||
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift']) (name) =>
|
_.each(['pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift']) (name) ->
|
||||||
method: Array.prototype[name]
|
method: Array.prototype[name]
|
||||||
wrapper.prototype[name]: =>
|
wrapper.prototype[name]: ->
|
||||||
method.apply(this._wrapped, arguments)
|
method.apply(this._wrapped, arguments)
|
||||||
result(this._wrapped, this._chain)
|
result(this._wrapped, this._chain)
|
||||||
|
|
||||||
|
|
||||||
# Add all accessor Array functions to the wrapper.
|
# Add all accessor Array functions to the wrapper.
|
||||||
_.each(['concat', 'join', 'slice']) (name) =>
|
_.each(['concat', 'join', 'slice']) (name) ->
|
||||||
method: Array.prototype[name]
|
method: Array.prototype[name]
|
||||||
wrapper.prototype[name]: =>
|
wrapper.prototype[name]: ->
|
||||||
result(method.apply(this._wrapped, arguments), this._chain)
|
result(method.apply(this._wrapped, arguments), this._chain)
|
||||||
|
|
||||||
|
|
||||||
# Start chaining a wrapped Underscore object.
|
# Start chaining a wrapped Underscore object.
|
||||||
wrapper::chain: =>
|
wrapper::chain: ->
|
||||||
this._chain: true
|
this._chain: true
|
||||||
this
|
this
|
||||||
|
|
||||||
|
|
||||||
# Extracts the result from a wrapped and chained object.
|
# Extracts the result from a wrapped and chained object.
|
||||||
wrapper::value: => this._wrapped
|
wrapper::value: -> this._wrapped
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ syn match coffeeNumber "-\=\<\d\+L\=\>\|0[xX][0-9a-fA-F]\+\>"
|
|||||||
syn region coffeeRegexpString start=+/[^/*]+me=e-1 skip=+\\\\\|\\/+ end=+/[gi]\{0,2\}\s*$+ end=+/[gi]\{0,2\}\s*[;.,)\]}]+me=e-1 contains=@htmlPreproc oneline
|
syn region coffeeRegexpString start=+/[^/*]+me=e-1 skip=+\\\\\|\\/+ end=+/[gi]\{0,2\}\s*$+ end=+/[gi]\{0,2\}\s*[;.,)\]}]+me=e-1 contains=@htmlPreproc oneline
|
||||||
|
|
||||||
syn match coffeePrototypeAccess "::"
|
syn match coffeePrototypeAccess "::"
|
||||||
syn match coffeeFunction "=>"
|
syn match coffeeFunction "->"
|
||||||
|
|
||||||
syn keyword coffeeExtends extends
|
syn keyword coffeeExtends extends
|
||||||
syn keyword coffeeConditional if else switch then
|
syn keyword coffeeConditional if else switch then
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ gem install coffee-script</pre>
|
|||||||
<td><code>-e, --eval</code></td>
|
<td><code>-e, --eval</code></td>
|
||||||
<td>
|
<td>
|
||||||
Compile and print a little snippet of CoffeeScript directly from the
|
Compile and print a little snippet of CoffeeScript directly from the
|
||||||
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: (x) => x * x"</tt>
|
command line (or from <b>stdin</b>). For example:<br /><tt>coffee -e "square: (x) -> x * x"</tt>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -353,7 +353,7 @@ coffee --print app/scripts/*.coffee > concatenation.js</pre>
|
|||||||
<p id="functions">
|
<p id="functions">
|
||||||
<b class="header">Functions and Invocation</b>
|
<b class="header">Functions and Invocation</b>
|
||||||
Functions are defined by a list of parameters, an arrow, and the
|
Functions are defined by a list of parameters, an arrow, and the
|
||||||
function body. The empty function looks like this: <tt>=></tt>. All
|
function body. The empty function looks like this: <tt>-></tt>. All
|
||||||
functions in CoffeeScript are named by default, for easier debugging.
|
functions in CoffeeScript are named by default, for easier debugging.
|
||||||
</p>
|
</p>
|
||||||
<div class='code'><pre class="idle"><span class="FunctionName">square</span><span class="Keyword">:</span> (x) <span class="Storage">=></span> x <span class="Keyword">*</span> x
|
<div class='code'><pre class="idle"><span class="FunctionName">square</span><span class="Keyword">:</span> (x) <span class="Storage">=></span> x <span class="Keyword">*</span> x
|
||||||
@@ -375,7 +375,7 @@ cube = function cube(x) {
|
|||||||
;alert(cube(5));'>run: cube(5)</button><br class='clear' /></div>
|
;alert(cube(5));'>run: cube(5)</button><br class='clear' /></div>
|
||||||
<p>
|
<p>
|
||||||
If you'd like to create an anonymous function, just wrap it in parentheses:
|
If you'd like to create an anonymous function, just wrap it in parentheses:
|
||||||
<tt>((x) => x * x)</tt>
|
<tt>((x) -> x * x)</tt>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
<p id="assignment">
|
<p id="assignment">
|
||||||
@@ -1304,7 +1304,7 @@ city = __c[1];
|
|||||||
|
|
||||||
<p id="long_arrow">
|
<p id="long_arrow">
|
||||||
<b class="header">Function binding</b>
|
<b class="header">Function binding</b>
|
||||||
The long arrow <tt>==></tt> can be used to both define a function, and to bind
|
The long arrow <tt>=></tt> can be used to both define a function, and to bind
|
||||||
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
it to the current value of <tt>this</tt>, right on the spot. This is helpful
|
||||||
when using callback-based libraries like Prototype or jQuery, for creating
|
when using callback-based libraries like Prototype or jQuery, for creating
|
||||||
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
iterator functions to pass to <tt>each</tt>, or event-handler functions
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ prechigh
|
|||||||
left EXTENDS
|
left EXTENDS
|
||||||
left '||=' '&&=' '?='
|
left '||=' '&&=' '?='
|
||||||
right ASSIGN RETURN
|
right ASSIGN RETURN
|
||||||
right '=>' '==>' UNLESS IF ELSE WHILE
|
right '->' '=>' UNLESS IF ELSE WHILE
|
||||||
preclow
|
preclow
|
||||||
|
|
||||||
rule
|
rule
|
||||||
@@ -207,8 +207,8 @@ rule
|
|||||||
|
|
||||||
# The symbols to signify functions, and bound functions.
|
# The symbols to signify functions, and bound functions.
|
||||||
FuncGlyph:
|
FuncGlyph:
|
||||||
'=>' { result = :func }
|
'->' { result = :func }
|
||||||
| '==>' { result = :boundfunc }
|
| '=>' { result = :boundfunc }
|
||||||
;
|
;
|
||||||
|
|
||||||
# The parameters to a function definition.
|
# The parameters to a function definition.
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ module CoffeeScript
|
|||||||
OPERATOR = /\A([+\*&|\/\-%=<>:!?]+)/
|
OPERATOR = /\A([+\*&|\/\-%=<>:!?]+)/
|
||||||
WHITESPACE = /\A([ \t]+)/
|
WHITESPACE = /\A([ \t]+)/
|
||||||
COMMENT = /\A(((\n?[ \t]*)?#.*$)+)/
|
COMMENT = /\A(((\n?[ \t]*)?#.*$)+)/
|
||||||
CODE = /\A(=?=>)/
|
CODE = /\A((-|=)>)/
|
||||||
REGEX = /\A(\/(.*?)([^\\]|\\\\)\/[imgy]{0,4})/
|
REGEX = /\A(\/(.*?)([^\\]|\\\\)\/[imgy]{0,4})/
|
||||||
MULTI_DENT = /\A((\n([ \t]*))+)(\.)?/
|
MULTI_DENT = /\A((\n([ \t]*))+)(\.)?/
|
||||||
LAST_DENT = /\n([ \t]*)/
|
LAST_DENT = /\n([ \t]*)/
|
||||||
|
|||||||
@@ -12,14 +12,14 @@ Readline: require('readline')
|
|||||||
coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee')
|
coffeePath: File.path(module.path).dirname().dirname().dirname().dirname().dirname().join('bin', 'coffee')
|
||||||
|
|
||||||
# Our general-purpose error handler.
|
# Our general-purpose error handler.
|
||||||
checkForErrors: (coffeeProcess) =>
|
checkForErrors: (coffeeProcess) ->
|
||||||
return true if coffeeProcess.wait() is 0
|
return true if coffeeProcess.wait() is 0
|
||||||
system.stderr.print(coffeeProcess.stderr.read())
|
system.stderr.print(coffeeProcess.stderr.read())
|
||||||
throw new Error("CoffeeScript compile error")
|
throw new Error("CoffeeScript compile error")
|
||||||
|
|
||||||
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
# Run a simple REPL, round-tripping to the CoffeeScript compiler for every
|
||||||
# command.
|
# command.
|
||||||
exports.run: (args) =>
|
exports.run: (args) ->
|
||||||
if args.length
|
if args.length
|
||||||
for path, i in args
|
for path, i in args
|
||||||
exports.evalCS(File.read(path))
|
exports.evalCS(File.read(path))
|
||||||
@@ -35,24 +35,24 @@ exports.run: (args) =>
|
|||||||
print(e)
|
print(e)
|
||||||
|
|
||||||
# Compile a given CoffeeScript file into JavaScript.
|
# Compile a given CoffeeScript file into JavaScript.
|
||||||
exports.compileFile: (path) =>
|
exports.compileFile: (path) ->
|
||||||
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
|
coffee: OS.popen([coffeePath, "--print", "--no-wrap", path])
|
||||||
checkForErrors(coffee)
|
checkForErrors(coffee)
|
||||||
coffee.stdout.read()
|
coffee.stdout.read()
|
||||||
|
|
||||||
# Compile a string of CoffeeScript into JavaScript.
|
# Compile a string of CoffeeScript into JavaScript.
|
||||||
exports.compile: (source, flags) =>
|
exports.compile: (source, flags) ->
|
||||||
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags or []))
|
coffee: OS.popen([coffeePath, "--eval", "--no-wrap"].concat(flags or []))
|
||||||
coffee.stdin.write(source).flush().close()
|
coffee.stdin.write(source).flush().close()
|
||||||
checkForErrors(coffee)
|
checkForErrors(coffee)
|
||||||
coffee.stdout.read()
|
coffee.stdout.read()
|
||||||
|
|
||||||
# Evaluating a string of CoffeeScript first compiles it externally.
|
# Evaluating a string of CoffeeScript first compiles it externally.
|
||||||
exports.evalCS: (source, flags) =>
|
exports.evalCS: (source, flags) ->
|
||||||
eval(exports.compile(source, flags))
|
eval(exports.compile(source, flags))
|
||||||
|
|
||||||
# Make a factory for the CoffeeScript environment.
|
# Make a factory for the CoffeeScript environment.
|
||||||
exports.makeNarwhalFactory: (path) =>
|
exports.makeNarwhalFactory: (path) ->
|
||||||
code: exports.compileFile(path)
|
code: exports.compileFile(path)
|
||||||
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
|
factoryText: "function(require,exports,module,system,print){" + code + "/**/\n}"
|
||||||
if system.engine is "rhino"
|
if system.engine is "rhino"
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ factories: {}
|
|||||||
loader: {
|
loader: {
|
||||||
|
|
||||||
# Reload the coffee-script environment from source.
|
# Reload the coffee-script environment from source.
|
||||||
reload: (topId, path) =>
|
reload: (topId, path) ->
|
||||||
coffeescript ||= require('coffee-script')
|
coffeescript ||= require('coffee-script')
|
||||||
factories[topId]: => coffeescript.makeNarwhalFactory(path)
|
factories[topId]: -> coffeescript.makeNarwhalFactory(path)
|
||||||
|
|
||||||
# Ensure that the coffee-script environment is loaded.
|
# Ensure that the coffee-script environment is loaded.
|
||||||
load: (topId, path) =>
|
load: (topId, path) ->
|
||||||
factories[topId] ||= this.reload(topId, path)
|
factories[topId] ||= this.reload(topId, path)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1141,8 +1141,8 @@ racc_token_table = {
|
|||||||
"&&=" => 87,
|
"&&=" => 87,
|
||||||
"?=" => 88,
|
"?=" => 88,
|
||||||
:ASSIGN => 89,
|
:ASSIGN => 89,
|
||||||
"=>" => 90,
|
"->" => 90,
|
||||||
"==>" => 91,
|
"=>" => 91,
|
||||||
"\n" => 92,
|
"\n" => 92,
|
||||||
";" => 93,
|
";" => 93,
|
||||||
"," => 94,
|
"," => 94,
|
||||||
@@ -1264,8 +1264,8 @@ Racc_token_to_s_table = [
|
|||||||
"\"&&=\"",
|
"\"&&=\"",
|
||||||
"\"?=\"",
|
"\"?=\"",
|
||||||
"ASSIGN",
|
"ASSIGN",
|
||||||
|
"\"->\"",
|
||||||
"\"=>\"",
|
"\"=>\"",
|
||||||
"\"==>\"",
|
|
||||||
"\"\\n\"",
|
"\"\\n\"",
|
||||||
"\";\"",
|
"\";\"",
|
||||||
"\",\"",
|
"\",\"",
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ module CoffeeScript
|
|||||||
IMPLICIT_END = [:IF, :UNLESS, :FOR, :WHILE, "\n", :PARAM_START, :OUTDENT]
|
IMPLICIT_END = [:IF, :UNLESS, :FOR, :WHILE, "\n", :PARAM_START, :OUTDENT]
|
||||||
IMPLICIT_CALL = [:IDENTIFIER, :NUMBER, :STRING, :JS, :REGEX, :NEW, :PARAM_START,
|
IMPLICIT_CALL = [:IDENTIFIER, :NUMBER, :STRING, :JS, :REGEX, :NEW, :PARAM_START,
|
||||||
:TRY, :DELETE, :INSTANCEOF, :TYPEOF, :SWITCH, :ARGUMENTS,
|
:TRY, :DELETE, :INSTANCEOF, :TYPEOF, :SWITCH, :ARGUMENTS,
|
||||||
:TRUE, :FALSE, :YES, :NO, :ON, :OFF, '!', '!!', :NOT, '=>', '==>']
|
:TRUE, :FALSE, :YES, :NO, :ON, :OFF, '!', '!!', :NOT, '->', '=>']
|
||||||
|
|
||||||
# The inverse mappings of token pairs we're trying to fix up.
|
# The inverse mappings of token pairs we're trying to fix up.
|
||||||
INVERSES = BALANCED_PAIRS.inject({}) do |memo, pair|
|
INVERSES = BALANCED_PAIRS.inject({}) do |memo, pair|
|
||||||
@@ -33,7 +33,7 @@ module CoffeeScript
|
|||||||
|
|
||||||
# Single-line flavors of block expressions that have unclosed endings.
|
# Single-line flavors of block expressions that have unclosed endings.
|
||||||
# The grammar can't disambiguate them, so we insert the implicit indentation.
|
# The grammar can't disambiguate them, so we insert the implicit indentation.
|
||||||
SINGLE_LINERS = [:ELSE, "=>", "==>", :TRY, :FINALLY, :THEN]
|
SINGLE_LINERS = [:ELSE, "->", "=>", :TRY, :FINALLY, :THEN]
|
||||||
SINGLE_CLOSERS = ["\n", :CATCH, :FINALLY, :ELSE, :OUTDENT, :LEADING_WHEN, :PARAM_START]
|
SINGLE_CLOSERS = ["\n", :CATCH, :FINALLY, :ELSE, :OUTDENT, :LEADING_WHEN, :PARAM_START]
|
||||||
|
|
||||||
# Rewrite the token stream in multiple passes, one logical filter at
|
# Rewrite the token stream in multiple passes, one logical filter at
|
||||||
@@ -193,7 +193,7 @@ module CoffeeScript
|
|||||||
end
|
end
|
||||||
|
|
||||||
# We'd like to support syntax like this:
|
# We'd like to support syntax like this:
|
||||||
# el.click((event) =>
|
# el.click((event) ->
|
||||||
# el.hide())
|
# el.hide())
|
||||||
# In order to accomplish this, move outdents that follow closing parens
|
# In order to accomplish this, move outdents that follow closing parens
|
||||||
# inwards, safely. The steps to accomplish this are:
|
# inwards, safely. The steps to accomplish this are:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
area: (x, y, x1, y1) =>
|
area: (x, y, x1, y1) ->
|
||||||
(x - x1) * (x - y1)
|
(x - x1) * (x - y1)
|
||||||
|
|
||||||
x: y: 10
|
x: y: 10
|
||||||
@@ -18,14 +18,14 @@ print(area(
|
|||||||
|
|
||||||
|
|
||||||
# Arguments are turned into arrays.
|
# Arguments are turned into arrays.
|
||||||
curried: =>
|
curried: ->
|
||||||
print area.apply(this, arguments.concat(20, 20)) is 100
|
print area.apply(this, arguments.concat(20, 20)) is 100
|
||||||
|
|
||||||
curried 10, 10
|
curried 10, 10
|
||||||
|
|
||||||
|
|
||||||
# Arguments is not a special keyword -- it can be assigned to:
|
# Arguments is not a special keyword -- it can be assigned to:
|
||||||
func: =>
|
func: ->
|
||||||
arguments: 25
|
arguments: 25
|
||||||
arguments
|
arguments
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ methods: ['one', 'two', 'three']
|
|||||||
|
|
||||||
for method in methods
|
for method in methods
|
||||||
name: method
|
name: method
|
||||||
obj[name]: =>
|
obj[name]: ->
|
||||||
"I'm " + name
|
"I'm " + name
|
||||||
|
|
||||||
print obj.one() is "I'm one"
|
print obj.one() is "I'm one"
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ print result is true and result2 is true
|
|||||||
|
|
||||||
# Assign to conditional.
|
# Assign to conditional.
|
||||||
|
|
||||||
get_x: => 10
|
get_x: -> 10
|
||||||
|
|
||||||
if x: get_x() then 100
|
if x: get_x() then 100
|
||||||
|
|
||||||
|
|||||||
2
test/fixtures/execution/test_blocks.coffee
vendored
2
test/fixtures/execution/test_blocks.coffee
vendored
@@ -1,4 +1,4 @@
|
|||||||
results: [1, 2, 3].map (x) =>
|
results: [1, 2, 3].map (x) ->
|
||||||
x * x
|
x * x
|
||||||
|
|
||||||
print results.join(' ') is '1 4 9'
|
print results.join(' ') is '1 4 9'
|
||||||
@@ -1,21 +1,21 @@
|
|||||||
Base: =>
|
Base: ->
|
||||||
Base::func: (string) =>
|
Base::func: (string) ->
|
||||||
'zero/' + string
|
'zero/' + string
|
||||||
|
|
||||||
FirstChild: =>
|
FirstChild: ->
|
||||||
FirstChild extends Base
|
FirstChild extends Base
|
||||||
FirstChild::func: (string) =>
|
FirstChild::func: (string) ->
|
||||||
super('one/') + string
|
super('one/') + string
|
||||||
|
|
||||||
SecondChild: =>
|
SecondChild: ->
|
||||||
SecondChild extends FirstChild
|
SecondChild extends FirstChild
|
||||||
SecondChild::func: (string) =>
|
SecondChild::func: (string) ->
|
||||||
super('two/') + string
|
super('two/') + string
|
||||||
|
|
||||||
ThirdChild: =>
|
ThirdChild: ->
|
||||||
this.array: [1, 2, 3]
|
this.array: [1, 2, 3]
|
||||||
ThirdChild extends SecondChild
|
ThirdChild extends SecondChild
|
||||||
ThirdChild::func: (string) =>
|
ThirdChild::func: (string) ->
|
||||||
super('three/') + string
|
super('three/') + string
|
||||||
|
|
||||||
result: (new ThirdChild()).func 'four'
|
result: (new ThirdChild()).func 'four'
|
||||||
@@ -23,13 +23,13 @@ result: (new ThirdChild()).func 'four'
|
|||||||
print result is 'zero/one/two/three/four'
|
print result is 'zero/one/two/three/four'
|
||||||
|
|
||||||
|
|
||||||
TopClass: (arg) =>
|
TopClass: (arg) ->
|
||||||
this.prop: 'top-' + arg
|
this.prop: 'top-' + arg
|
||||||
|
|
||||||
SuperClass: (arg) =>
|
SuperClass: (arg) ->
|
||||||
super 'super-' + arg
|
super 'super-' + arg
|
||||||
|
|
||||||
SubClass: =>
|
SubClass: ->
|
||||||
super 'sub'
|
super 'sub'
|
||||||
|
|
||||||
SuperClass extends TopClass
|
SuperClass extends TopClass
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
identity_wrap: (x) =>
|
identity_wrap: (x) ->
|
||||||
=> x
|
-> x
|
||||||
|
|
||||||
result: identity_wrap(identity_wrap(true))()()
|
result: identity_wrap(identity_wrap(true))()()
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
func: =>
|
func: ->
|
||||||
a: 3
|
a: 3
|
||||||
b: []
|
b: []
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ func: =>
|
|||||||
c: {
|
c: {
|
||||||
"text": b
|
"text": b
|
||||||
other: null
|
other: null
|
||||||
something_else: (x) => x + 5
|
something_else: (x) -> x + 5
|
||||||
}
|
}
|
||||||
|
|
||||||
c: 'error' unless 42 > 41
|
c: 'error' unless 42 > 41
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ print z is null and x is "EX"
|
|||||||
# Only evaluate once.
|
# Only evaluate once.
|
||||||
|
|
||||||
counter: 0
|
counter: 0
|
||||||
get_next_node: =>
|
get_next_node: ->
|
||||||
throw "up" if counter
|
throw "up" if counter
|
||||||
counter++
|
counter++
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ items: [1, 2, 3, "bacon", 4, 5]
|
|||||||
for item in items
|
for item in items
|
||||||
break if item is "bacon"
|
break if item is "bacon"
|
||||||
|
|
||||||
findit: (items) =>
|
findit: (items) ->
|
||||||
for item in items
|
for item in items
|
||||||
return item if item is "bacon"
|
return item if item is "bacon"
|
||||||
|
|
||||||
@@ -17,7 +17,7 @@ print findit(items) is "bacon"
|
|||||||
|
|
||||||
obj: {
|
obj: {
|
||||||
num: 5
|
num: 5
|
||||||
func: =>
|
func: ->
|
||||||
this.result: if false
|
this.result: if false
|
||||||
10
|
10
|
||||||
else
|
else
|
||||||
|
|||||||
26
test/fixtures/execution/test_functions.coffee
vendored
26
test/fixtures/execution/test_functions.coffee
vendored
@@ -1,6 +1,6 @@
|
|||||||
x: 1
|
x: 1
|
||||||
y: {}
|
y: {}
|
||||||
y.x: => 3
|
y.x: -> 3
|
||||||
|
|
||||||
print x is 1
|
print x is 1
|
||||||
print typeof(y.x) is 'function'
|
print typeof(y.x) is 'function'
|
||||||
@@ -9,17 +9,17 @@ print y.x.name is 'x'
|
|||||||
|
|
||||||
|
|
||||||
# The empty function should not cause a syntax error.
|
# The empty function should not cause a syntax error.
|
||||||
=>
|
->
|
||||||
|
|
||||||
|
|
||||||
obj: {
|
obj: {
|
||||||
name: "Fred"
|
name: "Fred"
|
||||||
|
|
||||||
bound: =>
|
bound: ->
|
||||||
(==> print(this.name is "Fred"))()
|
(=> print(this.name is "Fred"))()
|
||||||
|
|
||||||
unbound: =>
|
unbound: ->
|
||||||
(=> print(!this.name?))()
|
(-> print(!this.name?))()
|
||||||
}
|
}
|
||||||
|
|
||||||
obj.unbound()
|
obj.unbound()
|
||||||
@@ -29,18 +29,18 @@ obj.bound()
|
|||||||
# The named function should be cleared out before a call occurs:
|
# The named function should be cleared out before a call occurs:
|
||||||
|
|
||||||
# Python decorator style wrapper that memoizes any function
|
# Python decorator style wrapper that memoizes any function
|
||||||
memoize: (fn) =>
|
memoize: (fn) ->
|
||||||
cache: {}
|
cache: {}
|
||||||
self: this
|
self: this
|
||||||
(args...) =>
|
(args...) ->
|
||||||
key: args.toString()
|
key: args.toString()
|
||||||
return cache[key] if cache[key]
|
return cache[key] if cache[key]
|
||||||
cache[key] = fn.apply(self, args)
|
cache[key] = fn.apply(self, args)
|
||||||
|
|
||||||
Math: {
|
Math: {
|
||||||
Add: (a, b) => a + b
|
Add: (a, b) -> a + b
|
||||||
AnonymousAdd: ((a, b) => a + b)
|
AnonymousAdd: ((a, b) -> a + b)
|
||||||
FastAdd: memoize (a, b) => a + b
|
FastAdd: memoize (a, b) -> a + b
|
||||||
}
|
}
|
||||||
|
|
||||||
print Math.Add(5, 5) is 10
|
print Math.Add(5, 5) is 10
|
||||||
@@ -53,5 +53,5 @@ print 100 > 1 if 1 > 0
|
|||||||
print true unless false
|
print true unless false
|
||||||
print true for i in [1..3]
|
print true for i in [1..3]
|
||||||
|
|
||||||
print_func: (f) => print(f())
|
print_func: (f) -> print(f())
|
||||||
print_func => true
|
print_func -> true
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# comment
|
# comment
|
||||||
func: =>
|
func: ->
|
||||||
# comment
|
# comment
|
||||||
false
|
false
|
||||||
false # comment
|
false # comment
|
||||||
@@ -14,7 +14,7 @@ switch 'string'
|
|||||||
when null
|
when null
|
||||||
something_else()
|
something_else()
|
||||||
|
|
||||||
=>
|
->
|
||||||
code()
|
code()
|
||||||
# comment
|
# comment
|
||||||
|
|
||||||
|
|||||||
4
test/fixtures/execution/test_literals.coffee
vendored
4
test/fixtures/execution/test_literals.coffee
vendored
@@ -1,4 +1,4 @@
|
|||||||
a: [(x) => x, (x) => x * x]
|
a: [(x) -> x, (x) -> x * x]
|
||||||
|
|
||||||
print a.length is 2
|
print a.length is 2
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ neg: (3 -4)
|
|||||||
print neg is -1
|
print neg is -1
|
||||||
|
|
||||||
|
|
||||||
func: =>
|
func: ->
|
||||||
return if true
|
return if true
|
||||||
|
|
||||||
print func() is null
|
print func() is null
|
||||||
|
|||||||
@@ -13,6 +13,6 @@ print 50 > 10 > 5 is parseInt('5', 10)
|
|||||||
# more than once.
|
# more than once.
|
||||||
|
|
||||||
i: 0
|
i: 0
|
||||||
func: => i++
|
func: -> i++
|
||||||
|
|
||||||
print 1 > func() < 1
|
print 1 > func() < 1
|
||||||
|
|||||||
4
test/fixtures/execution/test_splats.coffee
vendored
4
test/fixtures/execution/test_splats.coffee
vendored
@@ -1,4 +1,4 @@
|
|||||||
func: (first, second, rest...) =>
|
func: (first, second, rest...) ->
|
||||||
rest.join ' '
|
rest.join ' '
|
||||||
|
|
||||||
result: func 1, 2, 3, 4, 5
|
result: func 1, 2, 3, 4, 5
|
||||||
@@ -8,7 +8,7 @@ print result is "3 4 5"
|
|||||||
|
|
||||||
gold: silver: bronze: the_field: null
|
gold: silver: bronze: the_field: null
|
||||||
|
|
||||||
medalists: (first, second, third, rest...) =>
|
medalists: (first, second, third, rest...) ->
|
||||||
gold: first
|
gold: first
|
||||||
silver: second
|
silver: second
|
||||||
bronze: third
|
bronze: third
|
||||||
|
|||||||
2
test/fixtures/execution/test_switch.coffee
vendored
2
test/fixtures/execution/test_switch.coffee
vendored
@@ -16,7 +16,7 @@ result: switch num
|
|||||||
|
|
||||||
print result
|
print result
|
||||||
|
|
||||||
func: (num) =>
|
func: (num) ->
|
||||||
switch num
|
switch num
|
||||||
when 2, 4, 6
|
when 2, 4, 6
|
||||||
true
|
true
|
||||||
|
|||||||
2
test/fixtures/generation/each.coffee
vendored
2
test/fixtures/generation/each.coffee
vendored
@@ -1,6 +1,6 @@
|
|||||||
# The cornerstone, an each implementation.
|
# The cornerstone, an each implementation.
|
||||||
# Handles objects implementing forEach, arrays, and raw objects.
|
# Handles objects implementing forEach, arrays, and raw objects.
|
||||||
_.each: (obj, iterator, context) =>
|
_.each: (obj, iterator, context) ->
|
||||||
index: 0
|
index: 0
|
||||||
try
|
try
|
||||||
if obj.forEach
|
if obj.forEach
|
||||||
|
|||||||
2
test/fixtures/generation/each.tokens
vendored
2
test/fixtures/generation/each.tokens
vendored
@@ -1 +1 @@
|
|||||||
[[:COMMENT, [" The cornerstone, an each implementation.", " Handles objects implementing forEach, arrays, and raw objects."]], ["\n", "\n"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "each"], [:ASSIGN, ":"], [:PARAM_START, "("], [:PARAM, "obj"], [",", ","], [:PARAM, "iterator"], [",", ","], [:PARAM, "context"], [:PARAM_END, ")"], ["=>", "=>"], [:INDENT, 2], [:IDENTIFIER, "index"], [:ASSIGN, ":"], [:NUMBER, "0"], ["\n", "\n"], [:TRY, "try"], [:INDENT, 2], [:IF, "if"], [:IDENTIFIER, "obj"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "forEach"], [:INDENT, 2], [:IDENTIFIER, "obj"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "forEach"], ["(", "("], [:IDENTIFIER, "iterator"], [",", ","], [:IDENTIFIER, "context"], [")", ")"], [:OUTDENT, 2], [:ELSE, "else"], [:IF, "if"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "isArray"], ["(", "("], [:IDENTIFIER, "obj"], [")", ")"], [:OR, "or"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "isArguments"], ["(", "("], [:IDENTIFIER, "obj"], [")", ")"], [:INDENT, 2], [:IDENTIFIER, "iterator"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "call"], ["(", "("], [:IDENTIFIER, "context"], [",", ","], [:IDENTIFIER, "item"], [",", ","], [:IDENTIFIER, "i"], [",", ","], [:IDENTIFIER, "obj"], [")", ")"], [:FOR, "for"], [:IDENTIFIER, "item"], [",", ","], [:IDENTIFIER, "i"], [:IN, "in"], [:IDENTIFIER, "obj"], [:OUTDENT, 2], [:ELSE, "else"], [:INDENT, 2], [:IDENTIFIER, "iterator"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "call"], ["(", "("], [:IDENTIFIER, "context"], [",", ","], [:IDENTIFIER, "obj"], ["[", "["], [:IDENTIFIER, "key"], ["]", "]"], [",", ","], [:IDENTIFIER, "key"], [",", ","], [:IDENTIFIER, "obj"], [")", ")"], [:FOR, "for"], [:IDENTIFIER, "key"], [:IN, "in"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "keys"], ["(", "("], [:IDENTIFIER, "obj"], [")", ")"], [:OUTDENT, 2], [:OUTDENT, 2], [:CATCH, "catch"], [:IDENTIFIER, "e"], [:INDENT, 2], [:THROW, "throw"], [:IDENTIFIER, "e"], [:IF, "if"], [:IDENTIFIER, "e"], [:ISNT, "isnt"], [:IDENTIFIER, "breaker"], [:OUTDENT, 2], ["\n", "\n"], [:IDENTIFIER, "obj"], [:OUTDENT, 2], ["\n", "\n"]]
|
[[:COMMENT, [" The cornerstone, an each implementation.", " Handles objects implementing forEach, arrays, and raw objects."]], ["\n", "\n"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "each"], [:ASSIGN, ":"], [:PARAM_START, "("], [:PARAM, "obj"], [",", ","], [:PARAM, "iterator"], [",", ","], [:PARAM, "context"], [:PARAM_END, ")"], ["->", "->"], [:INDENT, 2], [:IDENTIFIER, "index"], [:ASSIGN, ":"], [:NUMBER, "0"], ["\n", "\n"], [:TRY, "try"], [:INDENT, 2], [:IF, "if"], [:IDENTIFIER, "obj"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "forEach"], [:INDENT, 2], [:IDENTIFIER, "obj"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "forEach"], ["(", "("], [:IDENTIFIER, "iterator"], [",", ","], [:IDENTIFIER, "context"], [")", ")"], [:OUTDENT, 2], [:ELSE, "else"], [:IF, "if"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "isArray"], ["(", "("], [:IDENTIFIER, "obj"], [")", ")"], [:OR, "or"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "isArguments"], ["(", "("], [:IDENTIFIER, "obj"], [")", ")"], [:INDENT, 2], [:IDENTIFIER, "iterator"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "call"], ["(", "("], [:IDENTIFIER, "context"], [",", ","], [:IDENTIFIER, "item"], [",", ","], [:IDENTIFIER, "i"], [",", ","], [:IDENTIFIER, "obj"], [")", ")"], [:FOR, "for"], [:IDENTIFIER, "item"], [",", ","], [:IDENTIFIER, "i"], [:IN, "in"], [:IDENTIFIER, "obj"], [:OUTDENT, 2], [:ELSE, "else"], [:INDENT, 2], [:IDENTIFIER, "iterator"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "call"], ["(", "("], [:IDENTIFIER, "context"], [",", ","], [:IDENTIFIER, "obj"], ["[", "["], [:IDENTIFIER, "key"], ["]", "]"], [",", ","], [:IDENTIFIER, "key"], [",", ","], [:IDENTIFIER, "obj"], [")", ")"], [:FOR, "for"], [:IDENTIFIER, "key"], [:IN, "in"], [:IDENTIFIER, "_"], [:PROPERTY_ACCESS, "."], [:IDENTIFIER, "keys"], ["(", "("], [:IDENTIFIER, "obj"], [")", ")"], [:OUTDENT, 2], [:OUTDENT, 2], [:CATCH, "catch"], [:IDENTIFIER, "e"], [:INDENT, 2], [:THROW, "throw"], [:IDENTIFIER, "e"], [:IF, "if"], [:IDENTIFIER, "e"], [:ISNT, "isnt"], [:IDENTIFIER, "breaker"], [:OUTDENT, 2], ["\n", "\n"], [:IDENTIFIER, "obj"], [:OUTDENT, 2], ["\n", "\n"]]
|
||||||
@@ -10,7 +10,7 @@ catch error
|
|||||||
3
|
3
|
||||||
)
|
)
|
||||||
|
|
||||||
func: (x) =>
|
func: (x) ->
|
||||||
return throw x
|
return throw x
|
||||||
|
|
||||||
print(x * x for x in [1..100])
|
print(x * x for x in [1..100])
|
||||||
12
test/fixtures/generation/whitespace.coffee
vendored
12
test/fixtures/generation/whitespace.coffee
vendored
@@ -1,20 +1,20 @@
|
|||||||
# test
|
# test
|
||||||
f1: (x) =>
|
f1: (x) ->
|
||||||
x * x
|
x * x
|
||||||
f2: (y) =>
|
f2: (y) ->
|
||||||
y * x
|
y * x
|
||||||
f3: 3
|
f3: 3
|
||||||
|
|
||||||
# Parens can close on the proper level.
|
# Parens can close on the proper level.
|
||||||
elements.each((el) =>
|
elements.each((el) ->
|
||||||
el.click((event) =>
|
el.click((event) ->
|
||||||
el.reset()
|
el.reset()
|
||||||
el.show() if event.active
|
el.show() if event.active
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
# Or, parens can close blocks early.
|
# Or, parens can close blocks early.
|
||||||
elements.each((el) =>
|
elements.each((el) ->
|
||||||
el.click((event) =>
|
el.click((event) ->
|
||||||
el.reset()
|
el.reset()
|
||||||
el.show() if event.active))
|
el.show() if event.active))
|
||||||
@@ -25,10 +25,10 @@ class LexerTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_lexing_function_definition
|
def test_lexing_function_definition
|
||||||
code = "(x, y) => x * y"
|
code = "(x, y) -> x * y"
|
||||||
assert @lex.tokenize(code) == [[:PARAM_START, "("], [:PARAM, "x"],
|
assert @lex.tokenize(code) == [[:PARAM_START, "("], [:PARAM, "x"],
|
||||||
[",", ","], [:PARAM, "y"], [:PARAM_END, ")"],
|
[",", ","], [:PARAM, "y"], [:PARAM_END, ")"],
|
||||||
["=>", "=>"], [:INDENT, 2], [:IDENTIFIER, "x"], ["*", "*"],
|
["->", "->"], [:INDENT, 2], [:IDENTIFIER, "x"], ["*", "*"],
|
||||||
[:IDENTIFIER, "y"], [:OUTDENT, 2], ["\n", "\n"]]
|
[:IDENTIFIER, "y"], [:OUTDENT, 2], ["\n", "\n"]]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class ParserTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_parsing_an_function_definition
|
def test_parsing_an_function_definition
|
||||||
code = @par.parse("(x, y) => x * y").expressions.first
|
code = @par.parse("(x, y) -> x * y").expressions.first
|
||||||
assert code.params == ['x', 'y']
|
assert code.params == ['x', 'y']
|
||||||
body = code.body.expressions.first
|
body = code.body.expressions.first
|
||||||
assert body.is_a?(OpNode)
|
assert body.is_a?(OpNode)
|
||||||
|
|||||||
Reference in New Issue
Block a user