On Github MattMS / WD42_CoffeeScript_talk
Still worth a look
...and a little CoffeeScript :)
ES(6|7|201[56]) look (mostly) good.
I like CoffeeScript syntax, you may not.
Same idea as Sass, Less, Jade, etc.
But totally worth learning.
Vim
Regular expressions
First step to liking CoffeeScript.
hopefully_a_name = input('Who be ye? ') print('Yo', hopefully_a_name)
def but_is_it_awesome(questionable_item): if questionable_item in ['CoffeeScript', 'Python']: return 'yeah' elif questionable_item != 'Java': return 'maybe' else: return 'nah' print(but_is_it_awesome('Lua'))
class MeatSack: def __init__(self, name): self.name = name def talk(self): return 'Me {}'.format(self.name) meaty = MeatSack('T-Bag') print(meaty.talk())
10 in [2, 6, 12, 16, 60] 'nah' in 'banana' 'base' in {'base': 0xc, 'value': 'a'}
Syntax is well-considered.
You were going to indent it anyway, right?
(nearly coffee-time)
- date: 2015-09-30 place: Typewriter Factory task: Talk about CoffeeScript. - date: 2015-10-01 place: Antarctic Division task: Last day shenanigans. - date: 2015-10-02 place: The Winston task: Eat ribs and drink beer.
Safe for our puny human brains.
Only write what you need.
Better for configuration than transport.
Use safe_load.
Yay?
CoffeeScript is JavaScript.
Most documentation/libraries are in JavaScript.
You will probably debug the JavaScript with browser tools.
greet = (name)-> 'Hello ' + name
Free returns!
console.log(greet('World'))
or
console.log greet 'World'
(Sorry Lisp folk)
Think of the command line.
git add main.coffee
Well, think in reverse (if you must).
'World' > greet > console.log
wha = do_something with_this + this_too and_me / wait_what
which is apparently
wha = do_something(with_this + this_too(and_me / wait_what))
So instead...
chance_of_distracting_shopkeep = attractiveness * magic_skillz shopkeep_attention = 1 / shopkeep_hours_on_shift pizza_slices_acquired = if chance_of_distracting_shopkeep > shopkeep_attention all_the_slices else money_in_pocket / cost_of_pizza_slice
Everything is an expression (returns something).
One liners are fun to write, but to read?
path = require 'path' parts = path.parse path.join site, folder, file
gives
var path = require('path') var parts = path.parse(path.join(site, folder, file))
Alternatively
path = require 'path' parts = path.parse path.join [ site folder file ]...
I love this stuff.
Separated your code with fancy comment lines?
Run out of space with indented comments?
Made up your own comment styles?
# My cool server http = require 'http' server = http.createServer (req, res)-> res.writeHead 200, 'Content-Type': 'application/json' res.end JSON.stringify ok: true ## Start server Start the server on [port 1337](http://localhost:1337). server.listen 1337
Break up sections with headings.
All comments get the same width.
Looks cool on GitHub.
### So much space to fill with (almost) whatever you like. Literate CoffeeScript is nicer though. You only get 2 Markdown heading levels with this. ###
reaction = 'Woah!' all_the_stuff = """ #{reaction} I <em>strongly</em> dislike <abbr title="Hypertext Markup Language">HTML</abbr>, but how awesome is this! """
OPERATOR = /// ^ ( ?: [-=]> # function | [-+*/%<>&|^!?=]= # compound assign / compare | >>>=? # zero-fill right shift | ([-+:])\1 # doubles | ([&|<>])\2=? # logic / shift | \?\. # soak access | \.{2,3} # range or splat ///
or
OPERATOR = /^(?:[-=]>|[-+*\/%<>&|^!?=]=|>>>=?|([-+:])\1|([&|<>])\2=?|\?\.|\.{2,3})/
$ -> console.log 'And so it begins.'
gives
$(function () { console.log('And so it begins.') })
$ '#much_win_button' .on 'click', -> evil_corp_bank.receive user_bank.transfer_all() .text 'You won a goat!'
id = setTimeout -> console.log 'Done!' , 6000
$ '#lil_boxy_thing' .css height: '72px' width: '72px'
Always === and !==
on == true yes is on off != true no inst on
if person? person.say_hi()
gives
if (typeof person !== "undefined" && person !== null) { person.say_hi() }
[ title: 'Fight Club' year: 1999 , title: 'The Fifth Element' year: 1997 , title: 'The Matrix' year: 1999 ]
Remember YAML.
"Indirect descendant of CoffeeScript"
Too many symbols for me, but no hating!
Markdown for comments.
Reduces noisy characters.
Encourages me to write simpler code.
Fits my Jade/Stylus/Python style.
coffeescript.org is totally worth looking over.
@MattMS