Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 27th, 2012  |  syntax: None  |  size: 1.19 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #
  2. # Calculate the fibonacci numbers
  3. #
  4. # Warning: This script can go really slow if you try to calculate the number with n > 20
  5. #
  6. Math.fib = (n) ->
  7.     s = 0
  8.     return s if n == 0
  9.     if n == 1
  10.       s += 1
  11.     else
  12.       Math.fib(n - 1) + Math.fib(n - 2)
  13.  
  14.  
  15. #
  16. # Generate a RFC 4122 GUID
  17. #
  18. # See section 4.4 (Algorithms for Creating a UUID from Truly Random or
  19. # Pseudo-Random Numbers) for generating a GUID, since we don't have
  20. # hardware access within JavaScript.
  21. #
  22. # More info:
  23. #
  24. #   - http://www.rfc-archive.org/getrfc.php?rfc=4122
  25. #   - http://www.broofa.com/2008/09/javascript-uuid-function/
  26. #   - http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
  27. #
  28. Math.uuid = ->
  29.  
  30.     chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  31.     uuid = new Array(36)
  32.     random = 0
  33.  
  34.     for digit in [0..36]
  35.       switch digit
  36.         when 8, 13, 18, 23
  37.           uuid[digit] = '-'
  38.         when 14
  39.           uuid[digit] = '4'
  40.         else
  41.           random = 0x2000000 + (Math.random() * 0x1000000) | 0 if (random <= 0x02)
  42.           r = random & 0xf
  43.           random = random >> 4
  44.           uuid[digit] = chars[if digit == 19 then (r & 0x3) | 0x8 else r]
  45.  
  46.     uuid.join('')