pharmokan

coffeescript useful snippets 1

Apr 12th, 2020
665
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Eat lunch.
  2. eat = (food) -> "#{food} eaten."
  3. eat food for food in ['toast', 'cheese', 'wine']
  4.  
  5. # Fine five course dining.
  6. courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']
  7. menu = (i, dish) -> "Menu Item #{i}: #{dish}"
  8. menu i + 1, dish for dish, i in courses
  9.  
  10. # Health conscious meal.
  11. foods = ['broccoli', 'spinach', 'chocolate']
  12. console.log (eat food for food in foods when food isnt 'chocolate'
  13. #------
  14. [1..10].filter (x) ->  x%2
  15. #------
  16. console.log i for i in [10..0]
  17. #------
  18. for i in [10..0]
  19.     console.log i
  20. #------
  21. for i in [10..0] then console.log i
  22. #------
  23. data = [ 1, 2, 3, "a", "b", "c", 2, 3, 4, "b", "c", "d" ]
  24. set = []
  25. set.push i for i in data when not (i in set)
  26. console.log data
  27. console.log set
  28. #------
  29. keys = ['a','b','c']
  30.   values = [1,2,3]
  31.   map = {}
  32.   map[key] = values[i] for key, i in keys
  33. #---------
  34. sameOcc = []
  35. nums1 = [3,1,2,2,1]
  36. nums2 = [2,2,3]
  37. sameOcc.push i for i in nums1 when i in nums1 and i in nums2
  38.  
  39. console.log sameOcc
  40. #[3,2,2]
  41. #---------
  42. sumOfSquares = ( list ) ->
  43.     list.reduce (( sum, x ) -> sum + x ), 0
  44. sumOfSquares [1,2,3,4]
  45. #10
  46. #---------
  47. class Foo
  48.     @staticMethod: -> 'Bar'
  49.  
  50.     instanceMethod: -> 'Baz'
  51.  
  52. foo = new Foo
  53.  
  54. foo.instanceMethod() #=> 'Baz'
  55. Foo.staticMethod() #=> 'Bar'
  56. #---------
  57. launch() if ignition is on
  58. #---------
  59. volume = 10 if band isnt SpinalTap
  60. #---------
  61. letTheWildRumpusBegin() unless answer is no
  62. #---------
  63. if car.speed < limit then accelerate()
  64. #---------
  65. winner = yes if pick in [47, 92, 13]
  66. #---------
  67. print inspect "My name is #{@name}"
  68. #---------
  69. eldest = if 24 > 21 then "Liz" else "Ike"
  70. #---------
  71. for a from b _EQUIVALENT_ for (a of b)
  72. #---------
  73. a ? b   returns a if a is in scope and a != null; otherwise, b
  74. #---------
  75. sleep = (ms) ->
  76.   new Promise (resolve) ->
  77.     window.setTimeout resolve, ms
  78.  
  79. say = (text) ->
  80.   window.speechSynthesis.cancel()
  81.   window.speechSynthesis.speak new SpeechSynthesisUtterance text
  82.  
  83. countdown = (seconds) ->
  84.   for i in [seconds..1]
  85.     say i
  86.     await sleep 1000 # wait one second
  87.   say "Blastoff!"
  88.  
  89. countdown 3
  90. #---------
  91. ages = for child, age of yearsOld
  92.   "#{child} is #{age}"
  93.  
  94. # => [ 'max is 10', 'ida is 9', 'tim is 11' ]
  95. #---------
  96. #---------
  97. #---------
  98. #---------
  99. #---------
  100. #---------
  101. #---------
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×