Skip to content

Instantly share code, notes, and snippets.

@carlsmith
Last active February 5, 2019 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlsmith/c6aa9cc1093870fcf050d21d64aa362d to your computer and use it in GitHub Desktop.
Save carlsmith/c6aa9cc1093870fcf050d21d64aa362d to your computer and use it in GitHub Desktop.
A small collection of generic CoffeeScript helper functions.
# shorthand wrapper for `console.log`
put = (args...) -> console.log args...
# initialise undefined vars: `[a, b, c] = init 3`
init = (amount) -> undefined for n in [1..amount]
# decorator for defining constructors (see example.coffee)
factory = (mutator) -> (args...) ->
mutator (self = Object.create null), args...
return self
# decorator for defining blessings (see example.coffee)
bless = (mutator) -> (self, args...) ->
mutator self, args...
return self
Employee = factory (self, name, salary) ->
self.name = name
self.salary = salary
self.raise = (amount) -> self.salary += amount
addVoice = bless (self) ->
self.greet = ->
put "Hi, my name is #{self.name}."
put "I currently earn $#{self.salary}."
alice = Employee "Alice", 50000
addVoice alice
alice.raise 10000
alice.greet()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment