Last active
August 29, 2015 13:58
-
-
Save mowat27/9930523 to your computer and use it in GitHub Desktop.
CoffeeScript book examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| refine = (x, y...) -> | |
| console.log "required: #{x}" | |
| console.log "optional: #{y.join(', ')}" if y.length > 0 | |
| walk = (head, tail...) -> | |
| console.log head | |
| walk.apply(@, tail) if tail.length | |
| # Exercises | |
| start = (num) -> | |
| console.log "\n-----------------------------" | |
| console.log "Exercise #{num}" | |
| console.log "-----------------------------\n" | |
| # 1. | |
| start 1 | |
| console.log "original" | |
| clearArray = (arr) -> | |
| arr.splice 0, arr.length | |
| console.log clearArray [1,2,3] | |
| console.log "cleared array" | |
| clearArray = (arr) -> | |
| arr.splice 0, arr.length | |
| arr | |
| console.log clearArray [1,2,3] | |
| console.log "nothing" | |
| clearArray = -> | |
| console.log clearArray [1,2,3] | |
| # 2. | |
| start 2 | |
| run = (fn, args...) -> fn.apply(@, args) | |
| run console.log, 1,2,3 | |
| # 3. | |
| start 3 | |
| console.log "What does the question mean?" | |
| # 4. | |
| start 4 | |
| console.log ''' | |
| Whitespace becomes significant in CoffeeScript argument lists because | |
| the interpreter needs to be able to tell the difference between the start | |
| of an argument list and the arguments in it. | |
| For example | |
| f(a,b) passes a,b to f | |
| f a, b also passes a,b to f | |
| But | |
| f (a, b) will attempt to pass (a,b) to f - which causes a syntax error | |
| Interestingly, f (a) works as expected | |
| ''' | |
| # 5. | |
| start 5 | |
| console.log ''' | |
| foo.bar.baz() context = foo.bar | |
| @hoo() context = (current context) | |
| @hoo.rah() context = (current content).hoo | |
| ''' | |
| # 6. | |
| start 6 | |
| xInContext = -> | |
| console.log @x | |
| what = {x: 'quantum entanglement'} | |
| xInContext.apply(what) | |
| # 7. | |
| start 7 | |
| console.log ''' | |
| Yes. | |
| There are 2 vars named x. In the outer scope x is true and then that is bound to the | |
| locally scoped x inside the function | |
| ''' | |
| x = true | |
| showAnswer = (x = x) -> | |
| console.log if x then 'It works' else 'Nope' | |
| showAnswer() | |
| console.log ''' | |
| Wrong! | |
| The argumement list is running in a totally separate context. Even using @x doesnt work! | |
| ''' | |
| @x = true | |
| showAnswer = (x = @x) -> | |
| console.log if x then 'It works' else 'Nope' | |
| showAnswer() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 5x5 example | |
| stdin = process.openStdin() | |
| stdin.setEncoding 'utf8' | |
| GRID_SIZE = 5 | |
| inputCallback = null | |
| stdin.on 'data', (input) -> inputCallback input | |
| inRange = (x,y) -> | |
| 0 <= x <= GRID_SIZE and 0 <= y <= GRID_SIZE | |
| isInteger = (num) -> num is Math.round(num) | |
| strToCoordiantes = (input) -> | |
| halves = input.split(',') | |
| x = parseFloat(halves[0]) | |
| y = parseFloat(halves[1]) | |
| if halves.length is 2 | |
| if !isInteger(x) or !isInteger(y) | |
| console.log "Both coordinates must be integers" | |
| else if not inRange(x,y) | |
| console.log "Coordinates out of range - 1 to #{GRID_SIZE}" | |
| else | |
| {x,y} | |
| else | |
| console.log "Inputs must be of the form `x, y`." | |
| promptForTile1 = -> | |
| console.log "Please enter coordinates for the first tile." | |
| inputCallback = (input) -> | |
| promptForTile2() if strToCoordiantes input | |
| promptForTile2 = -> | |
| console.log "Please enter coordisnates for the second tile." | |
| inputCallback = (input) -> | |
| if strToCoordiantes(input) | |
| console.log "Swapping tiles...done" | |
| promptForTile1() | |
| console.log "Welcome to 5x5" | |
| promptForTile1() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment