From da6ea274541ceeeb32243d9ee45e2eb6cfa3191e Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Thu, 29 Jul 2010 00:03:42 -0400 Subject: [PATCH] most of the examples converted to symbology. --- examples/beautiful_code/binary_search.coffee | 16 ++-- .../beautiful_code/quicksort_runtime.coffee | 6 +- .../regular_expression_matcher.coffee | 12 +-- examples/blocks.coffee | 25 +++--- examples/code.coffee | 84 +++++++++--------- .../computer_science/binary_search.coffee | 22 ++--- examples/computer_science/bubble_sort.coffee | 8 +- examples/computer_science/linked_list.coffee | 64 +++++++------- .../computer_science/luhn_algorithm.coffee | 18 ++-- examples/computer_science/merge_sort.coffee | 14 +-- .../computer_science/selection_sort.coffee | 14 +-- examples/poignant.coffee | 85 +++++++++---------- examples/potion.coffee | 77 ++++++++--------- 13 files changed, 216 insertions(+), 229 deletions(-) diff --git a/examples/beautiful_code/binary_search.coffee b/examples/beautiful_code/binary_search.coffee index 8a1456a0..6ad42c8b 100644 --- a/examples/beautiful_code/binary_search.coffee +++ b/examples/beautiful_code/binary_search.coffee @@ -2,15 +2,15 @@ # The implementation of binary search that is tested. # Return the index of an element in a sorted list. (or -1, if not present) -index: (list, target) -> - [low, high]: [0, list.length] +index = (list, target) -> + [low, high] = [0, list.length] while low < high - mid: (low + high) >> 1 - val: list[mid] + mid = (low + high) >> 1 + val = list[mid] return mid if val is target - if val < target then low: mid + 1 else high: mid + if val < target then low = mid + 1 else high = mid return -1 -puts 2 is index([10, 20, 30, 40, 50], 30) -puts 4 is index([-97, 35, 67, 88, 1200], 1200) -puts 0 is index([0, 45, 70], 0) \ No newline at end of file +puts 2 is index [10, 20, 30, 40, 50], 30 +puts 4 is index [-97, 35, 67, 88, 1200], 1200 +puts 0 is index [0, 45, 70], 0 \ No newline at end of file diff --git a/examples/beautiful_code/quicksort_runtime.coffee b/examples/beautiful_code/quicksort_runtime.coffee index 6e9677f2..27794f44 100644 --- a/examples/beautiful_code/quicksort_runtime.coffee +++ b/examples/beautiful_code/quicksort_runtime.coffee @@ -1,11 +1,11 @@ # Beautiful Code, Chapter 3. # Produces the expected runtime of Quicksort, for every integer from 1 to N. -runtime: (N) -> - [sum, t]: [0, 0] +runtime = (N) -> + [sum, t] = [0, 0] for n in [1..N] sum += 2 * t - t: n - 1 + sum / n + t = n - 1 + sum / n t puts runtime(3) is 2.6666666666666665 diff --git a/examples/beautiful_code/regular_expression_matcher.coffee b/examples/beautiful_code/regular_expression_matcher.coffee index 524b3396..99ed0a11 100644 --- a/examples/beautiful_code/regular_expression_matcher.coffee +++ b/examples/beautiful_code/regular_expression_matcher.coffee @@ -3,16 +3,16 @@ # '.', '^', '$', and '*'. # 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 '^' while text return true if match_here(regexp, text) - text: text.slice(1) + text = text.slice(1) false # Search for the regexp at the beginning of the text. -match_here: (regexp, text) -> - [cur, next]: [regexp[0], regexp[1]] +match_here = (regexp, text) -> + [cur, next] = [regexp[0], regexp[1]] if regexp.length is 0 then return true if next is '*' then return match_star(cur, regexp.slice(2), text) if cur is '$' and not next then return text.length is 0 @@ -20,11 +20,11 @@ match_here: (regexp, text) -> false # Search for a kleene star match at the beginning of the text. -match_star: (c, regexp, text) -> +match_star = (c, regexp, text) -> loop return true if match_here(regexp, text) return false unless text and (text[0] is c or c is '.') - text: text.slice(1) + text = text.slice(1) puts match("ex", "some text") puts match("s..t", "spit") diff --git a/examples/blocks.coffee b/examples/blocks.coffee index 6cd04dcf..f9e67ffd 100644 --- a/examples/blocks.coffee +++ b/examples/blocks.coffee @@ -6,8 +6,8 @@ get '/hello', -> # Append. -append: (location, data) -> - path: new Pathname location +append = (location, data) -> + path = new Pathname location throw new Error("Location does not exist") unless path.exists() File.open path, 'a', (file) -> @@ -17,23 +17,20 @@ append: (location, data) -> # Rubinius' File.open implementation. -File.open: (path, mode, block) -> - io: new File path, mode +File.open = (path, mode, block) -> + io = new File path, mode return io unless block try block io finally - try - io.close() unless io.closed() - catch error - # nothing, just swallow them. + io.close() unless io.closed() # Write. -write: (location, data) -> - path: new Pathname location +write = (location, data) -> + path = new Pathname location raise "Location does not exist" unless path.exists() File.open path, 'w', (file) -> @@ -43,15 +40,15 @@ write: (location, data) -> # Rails' respond_to. -index: -> - people: Person.find 'all' +index = -> + people = Person.find 'all' respond_to (format) -> format.html() - format.xml -> render { xml: people.xml() } + format.xml -> render xml: people.xml() # Synchronization. -synchronize: (block) -> +synchronize = (block) -> lock() try block() finally unlock() diff --git a/examples/code.coffee b/examples/code.coffee index e87fd267..4dd69576 100644 --- a/examples/code.coffee +++ b/examples/code.coffee @@ -1,39 +1,37 @@ # 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()) listen() wait() # Objects: -dense_object_literal: {one: 1, two: 2, three: 3} +dense_object_literal = {one: 1, two: 2, three: 3} -spaced_out_multiline_object: { +spaced_out_multiline_object = pi: 3.14159 list: [1, 2, 3, 4] regex: /match[ing](every|thing|\/)/gi three: new Idea - inner_obj: { + inner_obj: freedom: -> _.freedom() - } -} # 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 = [] -multiline: [ +multiline = [ 'line one' 'line two' ] @@ -47,14 +45,14 @@ else if submarine.sinking else run_away() -eldest: if 25 > 21 then liz else marge +eldest = if 25 > 21 then liz else marge -decoration: medal_of_honor if war_hero +decoration = medal_of_honor if war_hero go_to_sleep() unless coffee # Returning early: -race: -> +race = -> run() walk() crawl() @@ -62,13 +60,13 @@ race: -> race() # Conditional assignment: -good ||= evil -wine &&= cheese +good = or evil +wine = and cheese # Nested property access and calls. ((moon.turn(360))).shapes[3].move({x: 45, y: 30}).position['top'].offset('x') -a: b: c: 5 +a = b = c = 5 # Embedded JavaScript. callback( @@ -102,19 +100,19 @@ loop !!true # Lexical scoping. -v_1: 5 -change_a_and_set_b: -> - v_1: 10 - v_2: 15 -v_2: 20 +v_1 = 5 +change_a_and_set_b = -> + v_1 = 10 + v_2 = 15 +v_2 = 20 # Array comprehensions. -supper: food.capitalize() for food in ['toast', 'cheese', 'wine'] +supper = food.capitalize() for food in ['toast', 'cheese', 'wine'] -drink(bottle) for bottle, i in ['soda', 'wine', 'lemonade'] when even(i) +drink bottle for bottle, i in ['soda', 'wine', 'lemonade'] when even i # Switch statements ("else" serves as a default). -activity: switch day +activity = switch day when "Tuesday" then eat_breakfast() when "Sunday" then go_to_church() when "Saturday" then go_to_the_park() @@ -128,50 +126,46 @@ activity: switch day else go_to_work() # 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. -zero_to_nine: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] -three_to_six: zero_to_nine[3..6] +zero_to_nine = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +three_to_six = zero_to_nine[3..6] # Multiline strings with inner quotes. -story: "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit, +story = "Lorem ipsum dolor \"sit\" amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad." # Inheritance and calling super. class Animal + constructor: (@name) -> + move: (meters) -> alert this.name + " moved " + meters + "m." class Snake extends Animal - constructor: (name) -> - @name: name - move: -> alert 'Slithering...' super 5 class Horse extends Animal - constructor: (name) -> - @name: name - move: -> alert 'Galloping...' super 45 -sam: new Snake "Sammy the Snake" -tom: new Horse "Tommy the Horse" +sam = new Snake "Sammy the Snake" +tom = new Horse "Tommy the Horse" sam.move() tom.move() # Numbers. -a_googol: 1e100 -hex: 0xff0000 -negative: -1.0 -infinity: Infinity -nan: NaN +a_googol = 1e100 +hex = 0xff0000 +negative = -1.0 +infinity = Infinity +nan = NaN # Deleting. delete secret.identity \ No newline at end of file diff --git a/examples/computer_science/binary_search.coffee b/examples/computer_science/binary_search.coffee index 2c5e020e..69fc678a 100644 --- a/examples/computer_science/binary_search.coffee +++ b/examples/computer_science/binary_search.coffee @@ -1,25 +1,25 @@ # Uses a binary search algorithm to locate a value in the specified array. -binary_search: (items, value) -> +binary_search = (items, value) -> - start: 0 - stop: items.length - 1 - pivot: Math.floor((start + stop) / 2) + start = 0 + stop = items.length - 1 + pivot = Math.floor (start + stop) / 2 while items[pivot] isnt value and start < stop # Adjust the search area. - stop: pivot - 1 if value < items[pivot] - start: pivot + 1 if value > items[pivot] + stop = pivot - 1 if value < items[pivot] + start = pivot + 1 if value > items[pivot] # Recalculate the pivot. - pivot: Math.floor((stop + start) / 2) + pivot = Math.floor (stop + start) / 2 # Make sure we've found the correct value. if items[pivot] is value then pivot else -1 # Test the function. -puts(2 is binary_search([10, 20, 30, 40, 50], 30)) -puts(4 is binary_search([-97, 35, 67, 88, 1200], 1200)) -puts(0 is binary_search([0, 45, 70], 0)) -puts(-1 is binary_search([0, 45, 70], 10)) \ No newline at end of file +puts 2 is binary_search [10, 20, 30, 40, 50], 30 +puts 4 is binary_search [-97, 35, 67, 88, 1200], 1200 +puts 0 is binary_search [0, 45, 70], 0 +puts(-1 is binary_search [0, 45, 70], 10) \ No newline at end of file diff --git a/examples/computer_science/bubble_sort.coffee b/examples/computer_science/bubble_sort.coffee index f27a04df..4757c8ef 100644 --- a/examples/computer_science/bubble_sort.coffee +++ b/examples/computer_science/bubble_sort.coffee @@ -1,11 +1,11 @@ # A bubble sort implementation, sorting the given array in-place. -bubble_sort: (list) -> +bubble_sort = (list) -> for i in [0...list.length] 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] list # Test the function. -puts(bubble_sort([3, 2, 1]).join(' ') is '1 2 3') -puts(bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file +puts bubble_sort([3, 2, 1]).join(' ') is '1 2 3' +puts bubble_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file diff --git a/examples/computer_science/linked_list.coffee b/examples/computer_science/linked_list.coffee index c64bccb1..51032af0 100644 --- a/examples/computer_science/linked_list.coffee +++ b/examples/computer_science/linked_list.coffee @@ -2,7 +2,7 @@ class LinkedList constructor: -> - 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 @@ -10,13 +10,13 @@ class LinkedList add: (data) -> # Create a new node object to wrap the data. - node: {data: data, next: null} + node = data: data, next: null - current: this._head ||= node + current = this._head = or node if this._head isnt node - current: current.next while current.next - current.next: node + (current = current.next) while current.next + current.next = node this @@ -27,11 +27,11 @@ class LinkedList # Check for out-of-bounds values. return null if index < 0 - current: this._head or null - i: -1 + current = this._head or null + i = -1 # Advance through the list. - current: current.next while current and index > (i += 1) + (current = current.next) while current and index > (i += 1) # Return null if we've reached the end. current and current.data @@ -43,19 +43,19 @@ class LinkedList # Check for out-of-bounds values. return null if index < 0 - current: this._head or null - i: -1 + current = this._head or null + i = -1 # Special case: removing the first item. if index is 0 - this._head: current.next + this._head = current.next else # Find the right location. - [previous, current]: [current, current.next] while index > (i += 1) + ([previous, current] = [current, current.next]) while index > (i += 1) # Skip over the item to remove. - previous.next: current.next + previous.next = current.next # Return the value. current and current.data @@ -63,24 +63,24 @@ class LinkedList # Calculate the number of items in the list. size: -> - current: this._head - count: 0 + current = this._head + count = 0 while current count += 1 - current: current.next + current = current.next count # Convert the list into an array. toArray: -> - result: [] - current: this._head + result = [] + current = this._head while current - result.push(current.data) - current: current.next + result.push current.data + current = current.next result @@ -90,19 +90,19 @@ class LinkedList # Tests. -list: new LinkedList +list = new LinkedList list.add("Hi") -puts(list.size() is 1) -puts(list.item(0) is "Hi") -puts(list.item(1) is null) +puts list.size() is 1 +puts list.item(0) is "Hi" +puts list.item(1) is null -list: new LinkedList +list = new LinkedList list.add("zero").add("one").add("two") -puts(list.size() is 3) -puts(list.item(2) is "two") -puts(list.remove(1) is "one") -puts(list.item(0) is "zero") -puts(list.item(1) is "two") -puts(list.size() is 2) -puts(list.item(-10) is null) +puts list.size() is 3 +puts list.item(2) is "two" +puts list.remove(1) is "one" +puts list.item(0) is "zero" +puts list.item(1) is "two" +puts list.size() is 2 +puts list.item(-10) is null diff --git a/examples/computer_science/luhn_algorithm.coffee b/examples/computer_science/luhn_algorithm.coffee index ceb4f3dd..db1c5443 100644 --- a/examples/computer_science/luhn_algorithm.coffee +++ b/examples/computer_science/luhn_algorithm.coffee @@ -2,15 +2,15 @@ # numbers, national insurance numbers, etc. # See: http://en.wikipedia.org/wiki/Luhn_algorithm -is_valid_identifier: (identifier) -> +is_valid_identifier = (identifier) -> - sum: 0 - alt: false + sum = 0 + alt = false for i in [(identifier.length - 1)..0] # Get the next digit. - num: parseInt(identifier.charAt(i), 10) + num = parseInt identifier.charAt(i), 10 # If it's not a valid number, abort. return false if isNaN(num) @@ -18,10 +18,10 @@ is_valid_identifier: (identifier) -> # If it's an alternate number... if alt num *= 2 - num: (num % 10) + 1 if num > 9 + num = (num % 10) + 1 if num > 9 # Flip the alternate bit. - alt: !alt + alt = !alt # Add to the rest of the sum. sum += num @@ -31,6 +31,6 @@ is_valid_identifier: (identifier) -> # Tests. -puts(is_valid_identifier("49927398716") is true) -puts(is_valid_identifier("4408041234567893") is true) -puts(is_valid_identifier("4408041234567890") is false) +puts is_valid_identifier("49927398716") is true +puts is_valid_identifier("4408041234567893") is true +puts is_valid_identifier("4408041234567890") is false diff --git a/examples/computer_science/merge_sort.coffee b/examples/computer_science/merge_sort.coffee index 77eabf97..4fb69072 100644 --- a/examples/computer_science/merge_sort.coffee +++ b/examples/computer_science/merge_sort.coffee @@ -1,12 +1,12 @@ # Sorts an array in ascending natural order using merge sort. -merge_sort: (list) -> +merge_sort = (list) -> return list if list.length is 1 - result: [] - pivot: Math.floor(list.length / 2) - left: merge_sort(list.slice(0, pivot)) - right: merge_sort(list.slice(pivot)) + result = [] + pivot = Math.floor list.length / 2 + left = merge_sort list.slice 0, pivot + right = merge_sort list.slice pivot while left.length and right.length result.push(if left[0] < right[0] then left.shift() else right.shift()) @@ -15,5 +15,5 @@ merge_sort: (list) -> # Test the function. -puts(merge_sort([3, 2, 1]).join(' ') is '1 2 3') -puts(merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file +puts merge_sort([3, 2, 1]).join(' ') is '1 2 3' +puts merge_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file diff --git a/examples/computer_science/selection_sort.coffee b/examples/computer_science/selection_sort.coffee index 5c54ba38..1e87d561 100644 --- a/examples/computer_science/selection_sort.coffee +++ b/examples/computer_science/selection_sort.coffee @@ -1,23 +1,23 @@ # An in-place selection sort. -selection_sort: (list) -> - len: list.length +selection_sort = (list) -> + len = list.length # For each item in the list. for i in [0...len] # Set the minimum to this position. - min: i + min = i # Check the rest of the array to see if anything is smaller. - (min: j if list[j] < list[min]) for j in [(i+1)...len] + (min = j if list[j] < list[min]) for j in [(i+1)...len] # Swap if a smaller item has been found. - [list[i], list[min]]: [list[min], list[i]] if i isnt min + [list[i], list[min]] = [list[min], list[i]] if i isnt min # The list is now sorted. list # Test the function. -puts(selection_sort([3, 2, 1]).join(' ') is '1 2 3') -puts(selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9') \ No newline at end of file +puts selection_sort([3, 2, 1]).join(' ') is '1 2 3' +puts selection_sort([9, 2, 7, 0, 1]).join(' ') is '0 1 2 7 9' \ No newline at end of file diff --git a/examples/poignant.coffee b/examples/poignant.coffee index 0754284a..074a79d2 100644 --- a/examples/poignant.coffee +++ b/examples/poignant.coffee @@ -2,7 +2,7 @@ # ['toast', 'cheese', 'wine'].each { |food| print food.capitalize } -['toast', 'wine', 'cheese'].each (food) -> print(food.capitalize()) +['toast', 'wine', 'cheese'].each (food) -> print food.capitalize() @@ -13,12 +13,11 @@ # def purchased=(var); @purchased = var; end # end -LotteryTicket: { - get_picks: -> this.picks - set_picks: (nums) -> this.picks: nums - get_purchase: -> this.purchase - set_purchase: (amount) -> this.purchase: amount -} +LotteryTicket = + get_picks: -> @picks + set_picks: (@picks) -> + get_purchased: -> @purchase + set_purchased: (@purchased) -> @@ -39,19 +38,18 @@ LotteryTicket: { # end # end -LotteryDraw: { +LotteryDraw = play: -> - result: LotteryTicket.new_random() - winners: {} + result = LotteryTicket.new_random() + winners = {} this.tickets.each (buyer, ticket_list) -> ticket_list.each (ticket) -> - score: ticket.score(result) + score = ticket.score result return if score is 0 - winners[buyer] ||= [] - winners[buyer].push([ticket, score]) - this.tickets: {} + winners[buyer] = or [] + winners[buyer].push [ticket, score] + this.tickets = {} winners -} @@ -64,11 +62,10 @@ LotteryDraw: { # end # end -WishScanner: { +WishScanner = scan_for_a_wish: -> - wish: this.read().detect((thought) -> thought.index('wish: ') is 0) - wish.replace('wish: ', '') -} + wish = this.read().detect (thought) -> thought.index('wish: ') is 0 + wish.replace 'wish: ', '' @@ -108,34 +105,32 @@ WishScanner: { # # end -Creature : { +Creature = # This method applies a hit taken during a fight. hit: (damage) -> - p_up: Math.rand(this.charisma) + p_up = Math.rand this.charisma if p_up % 9 is 7 this.life += p_up / 4 - puts("[" + this.name + " magick powers up " + p_up + "!]") + puts "[" + this.name + " magick powers up " + p_up + "!]" this.life -= damage - 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. 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. - your_hit: Math.rand(this.strength + weapon) - puts("[You hit with " + your_hit + "points of damage!]") - enemy.hit(your_hit) + your_hit = Math.rand this.strength + weapon + puts "[You hit with " + your_hit + "points of damage!]" + enemy.hit your_hit # Retaliation. - puts(enemy) + puts enemy if enemy.life > 0 - enemy_hit: Math.rand(enemy.strength + enemy.weapon) - puts("[Your enemy hit with " + enemy_hit + "points of damage!]") - this.hit(enemy_hit) - -} + enemy_hit = Math.rand enemy.strength + enemy.weapon + puts "[Your enemy hit with " + enemy_hit + "points of damage!]" + this.hit enemy_hit @@ -154,14 +149,14 @@ Creature : { # end # Get evil idea and swap in code words -print("Enter your new idea: ") -idea: gets() -code_words.each((real, code) -> idea.replace(real, code)) +print "Enter your new idea: " +idea = gets() +code_words.each (real, code) -> idea.replace(real, code) # Save the jibberish to a new file -print("File encoded. Please enter a name for this idea: ") -idea_name: gets().strip() -File.open("idea-" + idea_name + '.txt', 'w', (file) -> file.write(idea)) +print "File encoded. Please enter a name for this idea: " +idea_name = gets().strip() +File.open "idea-" + idea_name + '.txt', 'w', (file) -> file.write idea @@ -177,10 +172,10 @@ File.open("idea-" + idea_name + '.txt', 'w', (file) -> file.write(idea)) # end # end -wipe_mutterings_from: (sentence) -> - throw new Error("cannot wipe mutterings") unless sentence.indexOf +wipe_mutterings_from = (sentence) -> + throw new Error "cannot wipe mutterings" unless sentence.indexOf while sentence.indexOf('(') >= 0 - open: sentence.indexOf('(') - 1 - close: sentence.indexOf(')') + 1 - sentence: sentence[0..open] + sentence[close..sentence.length] - sentence \ No newline at end of file + open = sentence.indexOf('(') - 1 + close = sentence.indexOf(')') + 1 + sentence = sentence[0..open] + sentence[close..sentence.length] + sentence diff --git a/examples/potion.coffee b/examples/potion.coffee index df8ae990..66c126a3 100644 --- a/examples/potion.coffee +++ b/examples/potion.coffee @@ -2,14 +2,14 @@ # 5 times: "Odelay!" print. -print("Odelay!") for i in [1..5] +print "Odelay!" for [1..5] # add = (x, y): x + y. # add(2, 4) string print -add: (x, y) -> x + y -print(add(2, 4)) +add = (x, y) -> x + y +print add 2, 4 # loop: 'quaff' print. @@ -19,25 +19,25 @@ loop print 'quaff' # ('cheese', 'bread', 'mayo') at (1) print -print(['cheese', 'bread', 'mayo'][1]) +print ['cheese', 'bread', 'mayo'][1] # (language='Potion', pointless=true) at (key='language') print -print({language: 'Potion', pointless: true}['language']) +print {language: 'Potion', pointless: true}['language'] # minus = (x, y): x - y. # minus (y=10, x=6) -minus: (x, y) -> x - y -minus(6, 10) +minus = (x, y) -> x - y +minus 6, 10 # foods = ('cheese', 'bread', 'mayo') # foods (2) -foods: ['cheese', 'bread', 'mayo'] +foods = ['cheese', 'bread', 'mayo'] foods[2] @@ -45,7 +45,7 @@ foods[2] # (key, ' is a ', val) join print. for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'} - print(key + ' is a ' + val) + print key + ' is a ' + val # Person = class: /name, /age, /sex. @@ -54,13 +54,13 @@ for key, val of {dog: 'canine', cat: 'feline', fox: 'vulpine'} class Person print: -> - print 'My name is ' + this.name + '.' + print 'My name is ' + @name + '.' # p = Person () # p /name string print -p: new Person +p = new Person print p.name @@ -71,10 +71,10 @@ print p.name # Policeman ('Constable') print class Policeman extends Person - constructor: (rank) -> - @rank: rank + constructor: (@rank) -> + print: -> - print 'My name is ' + this.name + " and I'm a " + this.rank + '.' + print 'My name is ' + @name + " and I'm a " + @rank + '.' print new Policeman 'Constable' @@ -83,11 +83,13 @@ print new Policeman 'Constable' # [para 'Welcome.', button 'OK']] # app first name -app = { - window: {width: 200, height: 200} +app = + window: + width: 200 + height: 200 para: 'Welcome.' button: 'OK' -} + app.window @@ -96,33 +98,32 @@ app.window # # x = 1, y = 2 -x: 1 -y: 2 +x = 1 +y = 2 -x: 1; y: 2 +x = 1; y = 2 # table = (language='Potion' # pointless=true) -table: { +table = language: 'Potion' pointless: yes -} # # this foul business... # String length = (): 10. # this foul business... -String::length: -> 10 +String::length = -> 10 # block = : # 'potion' print. -block: -> - print('potion') +block = -> + print 'potion' # if (age > 100): 'ancient'. @@ -156,30 +157,30 @@ switch author # 'quaff' print # count--. -count: 8 +count = 8 while count > 0 - print('quaff') + print 'quaff' count-- # 1 to 5 (a): # a string print. -print(a) for a in [1..5] +print a for a in [1..5] # if (3 ?gender): # "Huh? Numbers are sexed? That's amazing." print. if (3).gender? - print("Huh? Numbers are sexed? That's amazing.") + print "Huh? Numbers are sexed? That's amazing." # HomePage get = (url): # session = url query ? at ('session'). -HomePage::get: (url) -> - session: url.query.session if url.query? +HomePage::get = (url) -> + session = url.query.session if url.query? # BTree = class: /left, /right. @@ -187,10 +188,10 @@ HomePage::get: (url) -> # b /left = BTree () # b /right = BTree () -BTree: -> -b: new BTree -b.left: new BTree -b.right: new BTree +BTree = -> +b = new BTree +b.left = new BTree +b.right = new BTree # BTree = class: /left, /right. @@ -199,7 +200,7 @@ b.right: new BTree # if (b ? /left): # 'left path found!' print. -BTree: -> -b: new BTree +BTree = -> +b = new BTree -print('left path found!') if b.left? +print 'left path found!' if b.left?