Skip to content

Instantly share code, notes, and snippets.

@wesvetter
Forked from carlsmith/common.coffee
Created February 5, 2019 17:27
Show Gist options
  • Save wesvetter/2cd2501e704d96c047cb9198048b8fa4 to your computer and use it in GitHub Desktop.
Save wesvetter/2cd2501e704d96c047cb9198048b8fa4 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