Browse Source

some coffeescript examples

pull/1/head
Sami Vaarala 11 years ago
parent
commit
c0815055c5
  1. 4
      examples/coffee/Makefile
  2. 7
      examples/coffee/globals.coffee
  3. 2
      examples/coffee/hello.coffee
  4. 28
      examples/coffee/mandel.coffee

4
examples/coffee/Makefile

@ -0,0 +1,4 @@
dummy:
coffee -c globals.coffee
coffee -c hello.coffee
coffee -c mandel.coffee

7
examples/coffee/globals.coffee

@ -0,0 +1,7 @@
print '*** All globals'
print(name) for name in Object.getOwnPropertyNames(this)
print '*** Globals with a short name (<= 8 chars)'
print(name) for name in Object.getOwnPropertyNames(this) when name.length <= 8

2
examples/coffee/hello.coffee

@ -0,0 +1,2 @@
print 'Hello world!'
print 'build: ' + __duk__.build

28
examples/coffee/mandel.coffee

@ -0,0 +1,28 @@
mandel = (x0, y0, x1, y1, w, h, maxiter) ->
[dx, dy] = [(x1 - x0) / w, (y1 - y0) / h]
res = []
y = y0
for yc in [0..h-1]
x = x0
for xc in [0..w-1]
[xx, yy] = [x, y]
c = '*'
for i in [0..maxiter-1]
# (xx+i*yy)^2 + (x+i*y) = xx^2 + i*2*xx*yy - yy^2 + x + i*y
# = (xx^2 - yy^2 + x) + i*(2*xx*yy + y)
[xx2, yy2] = [xx*xx, yy*yy]
if xx2 + yy2 >= 4.0
c = '.'
break
[xx, yy] = [xx2 - yy2 + x, 2*xx*yy + y]
res.push(c)
x += dx
res.push('\n')
y += dy
print(res.join(''))
return
mandel(-2, 2, 2, -2, 200, 100, 1000)
Loading…
Cancel
Save