Created
November 29, 2012 07:40
-
-
Save drslump/4167417 to your computer and use it in GitHub Desktop.
PDI DEVCON1 - BooJs code examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Async | |
[async] def foo(): | |
print 'start' | |
try: | |
for i in range(3): | |
print i | |
for delay in (1000ms, 5s): | |
print "Waiting $delay..." | |
await delay = sleep(delay) | |
print "Waited for $(delay/1000)" | |
except: | |
print 'Error' | |
print 'end' | |
foo() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Boo.define('', ['exports', 'Boo', 'Async'], function (exports, Boo, NS0) { | |
function foo () { | |
var __e = null; | |
var delay = null; | |
var __final = null; | |
var __catch = null; | |
var __state = 0; | |
var $for$2 = null; | |
var i = 0; | |
return NS0.async(function () { | |
__catch = {}; | |
__final = []; | |
return Boo.generator(function (__value, __error) { | |
statemachine: while (true) { | |
try { | |
if (__error) { | |
throw __error; | |
} | |
switch (__state) { | |
case 0: | |
if (__value !== Boo.UNDEF || __error !== Boo.UNDEF) { | |
throw TypeError('Generator not started yet, unable to process sent value/error'); | |
} | |
Boo.print(['start']); | |
case 1: | |
__catch[7] = __catch[6] = __catch[5] = __catch[4] = __catch[3] = __catch[2] = __catch[1] = 8; | |
for (i = 0; i < 3; i++) { | |
Boo.print([i]); | |
} | |
$for$2 = Boo.generator([1000, 5000]); | |
case 2: | |
__catch[5] = __catch[4] = __catch[3] = __catch[2] = 6; | |
__final.push(function () { | |
$for$2.close(); | |
}); | |
case 3: | |
if (!($for$2 && (delay = $for$2.next(), true))) { | |
__state = 4; | |
continue statemachine; | |
} | |
Boo.print([['Waiting ', delay, '...'].join('')]); | |
__state = 5; | |
return NS0.sleep(delay); | |
case 4: | |
__final.pop()(); | |
__state = 7; | |
continue statemachine; | |
case 5: | |
delay = __value; | |
Boo.print(['Waited for ' + parseInt(delay / 1000)]); | |
__state = 3; | |
continue statemachine; | |
case 6: | |
case 7: | |
__state = 9; | |
continue statemachine; | |
case 8: | |
Boo.print(['Error']); | |
case 9: | |
Boo.print(['end']); | |
} | |
__state = -1; | |
throw Boo.STOP; | |
} catch (__e) { | |
if (__e = __e) { | |
if (true) { | |
if (__state in __catch) { | |
__error = null; | |
__state = __catch[__state]; | |
continue statemachine; | |
} | |
__state = -1; | |
while (__final.length) { | |
__final.pop()(); | |
} | |
throw __e; | |
} | |
} | |
} | |
break ; | |
} | |
}); | |
}); | |
} | |
foo(); | |
}); | |
//@ booAssembly = H4sIAAAAAAAAA+1SzU8TQRR/u4UGipCKH0GNOE3VCKGbWjaWJsZAoeEjfGkBE2ICu9uxLO7ubHa3QNUoB2NijAleTPwPvJqQQDx75eyVP4AjBw8anJ3ZfiBGosaDib923ux78+bN731MzG9ACAAa6Do4ANgGjn44Hut0tV360AabzTuxbWF8JzazpLvIdkjRUUykKZZFPKRi5JQspFtoaCqPTFLAUmtr5HIQYzoHMC6I8L5rc7oSdxfahBYhzElVgIJVYed/izWXqusit/sIMRll/9pe3Rg6aJypnyVJnzpxbCWOIkrjNtXpBaqP1OmSh9c8un/0SdIzloB4OAY1L0oONogWcOgPfMJHKGZ/gyLDwZUo6q+Phfg+j2q2ScTz+TEWBomTW8MTim6B6WrEwVgqGEY1Es9bhEaAGM2X8heA9Sb8BR1u8d9Gp3iNPh3pjpzuhnA3t2XzY1mBMeI1XpGlpNSb7L2e8S2NYPt2ehx/AvCY7vtUj+c9R7eKru9hBzMYn82DJ/IWxbMGUem+KfJZiw/Pjg7V8fBLMHwGoNlXgmEU2Gu8vSGo1ojpAlRGoydgGqZcT1FpQ5nKZ/Q7Am9hi8pPVPq4HUSlPyEK8yy6r+0hdr/jKtP2mfYGDHb6mWmv4SmNI7DbLylH3zPGZBeTEn3tPAzCBchAw/r3Ne4J+C63VyyccX2fT8JGUHxCxlxpXLGKMKUuY82rs0jDtIiK4YKkecSBgpJOq3Iqk0j1KnJCvpFOJfrkjJJIykpfn5rOyKm0BjcnSKFk4Fsw4JYtbUFZVXSPm+A+ITA67RBTdzE/BjaxvJWgOMUVPr2GrsKdkuXpJh4kpq14uqobulce8KinWvIw5Muuh00pcJJ8L93ATh47K7qGXagYeAJ5jdi4dpkhhECgBWlvDtPuXGxvbdp6eG/unLz7otNvyox49q6j2JPEyq1p2PZ0Ys0sOWTVFRp4LecepZ8bLV9Hth+8erfnSrkjg/4rQHzuxeQfRfmPfxTfAAWTwmEACAAA | |
//@ sourceMappingURL = async-await.js.map |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def on_ready(callback as callable): | |
callback(10) | |
def timeout(milliseconds as int, callback as callable): | |
`setTimeout(callback, milliseconds)` | |
on_ready() do: | |
print 'Ready!' | |
on_ready def(x): | |
print 'Ready!' | |
on_ready({ x | print 'Ready!' }) | |
on_ready: | |
print 'Ready!' | |
timeout 10s, { print 'Timeout!' } | |
timeout(5s) do: | |
print 'Timeout!' | |
timeout 1h + 30m: | |
print 'Timeout!' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Boo.define('', ['exports', 'Boo'], function (exports, Boo) { | |
function on_ready (callback) { | |
callback(10); | |
} | |
function timeout (milliseconds, callback) { | |
setTimeout(callback, milliseconds); | |
} | |
on_ready(function () { | |
Boo.print(['Ready!']); | |
}); | |
on_ready(function (x) { | |
Boo.print(['Ready!']); | |
}); | |
on_ready(function (x) { | |
Boo.print(['Ready!']); | |
}); | |
on_ready(function () { | |
Boo.print(['Ready!']); | |
}); | |
timeout(10000, function () { | |
Boo.print(['Timeout!']); | |
}); | |
timeout(5000, function () { | |
Boo.print(['Timeout!']); | |
}); | |
timeout(5400000, function () { | |
Boo.print(['Timeout!']); | |
}); | |
}); | |
//@ booAssemb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tru5gACgAA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def m(data): | |
match data: | |
case a=string(): | |
print '1', a | |
case array(a=string(), b=int(), *_): | |
print '2', a, b | |
case Hash(foo: a=string()): | |
print '3', a | |
case Hash(foo: c) and c is not null: | |
print '4', c | |
case a = Hash(): | |
print '5' | |
otherwise: | |
print 'other' | |
m('foo') | |
m(('foo',2,3)) | |
m((1,2,3)) | |
m(['foo',2,'baz']) | |
m({'foo': 'FOO', 'bar': 'BAR'}) | |
m(Hash(foo: 100)) | |
m({'bar': 'bar'}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Boo.define('', ['exports', 'Boo'], function (exports, Boo) { | |
function m (data) { | |
var c = null; | |
var $match$4 = null; | |
var $match$3 = null; | |
var b = 0; | |
var $match$2 = null; | |
var a = ''; | |
var $match$1 = null; | |
$match$1 = data; | |
if (Boo.isa($match$1, 'string') && (a = Boo.cast($match$1, 'string'), true)) { | |
Boo.print(['1', a]); | |
} else if (($match$2 = Boo.trycast($match$1, Array)) && 2 <= Boo.len($match$2) && Boo.isa($match$2[0], 'string') && (a = Boo.cast($match$2[0], 'string'), true) && Boo.isa($match$2[1], 'int') && (b = Boo.cast($match$2[1], 'int'), true)) { | |
Boo.print(['2', a, b]); | |
} else if (Boo.isa($match$1, Boo.Hash) && ($match$3 = Boo.cast($match$1, Boo.Hash), true) && Boo.isa($match$3['foo'], 'string') && (a = Boo.cast($match$3['foo'], 'string'), true)) { | |
Boo.print(['3', a]); | |
} else if (Boo.isa($match$1, Boo.Hash) && ($match$4 = Boo.cast($match$1, Boo.Hash), true) && (c = $match$4['foo'], true) && !Boo.isNull(c)) { | |
Boo.print(['4', c]); | |
} else if (Boo.isa($match$1, Boo.Hash) && (a = Boo.cast($match$1, Boo.Hash), true)) { | |
Boo.print(['5']); | |
} else { | |
Boo.print(['other']); | |
} | |
} | |
var $5 = null; | |
m('foo'); | |
m(['foo', 2, 3]); | |
m([1, 2, 3]); | |
m(['foo', 2, 'baz']); | |
m({ | |
'foo': 'FOO', | |
'bar': 'BAR' | |
}); | |
m(($5 = Boo.Hash(), $5['foo'] = 100, $5)); | |
m({ | |
'bar': 'bar' | |
}); | |
}); | |
//@ sourceMappingURL=pattern-matching.js.map |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Extension] def op_Addition(date as Date, ms as int) as Date: | |
return Date(date.getTime() + ms) | |
[Extension] def op_Subtraction(date as Date, ms as int) as Date: | |
return Date(date.getTime() - ms) | |
with date = Date(2000, 0, 1): | |
.setUTCFullYear(2000, 0, 1) | |
.setUTCHours(0, 0, 0, 0) | |
date_mod1 = date + 3h | |
date_mod2 = date - 3.5d | |
print 1h | |
print 1h + 30m + 30s | |
print date.toISOString() | |
print date_mod1.toISOString() | |
print date_mod2.toISOString() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Boo.define('', ['exports', 'Boo'], function (exports, Boo) { | |
function op_Addition (date, ms) { | |
return new Date(date.getTime() + ms); | |
} | |
function op_Subtraction (date, ms) { | |
return new Date(date.getTime() - ms); | |
} | |
var date_mod2 = null; | |
var date_mod1 = null; | |
var date = null; | |
date = new Date(2000, 0, 1); | |
date.setUTCFullYear(2000, 0, 1); | |
date.setUTCHours(0, 0, 0, 0); | |
date_mod1 = op_Addition(date, 10800000); | |
date_mod2 = op_Subtraction(date, 302400000); | |
Boo.print([3600000]); | |
Boo.print([5430000]); | |
Boo.print([date.toISOString()]); | |
Boo.print([date_mod1.toISOString()]); | |
Boo.print([date_mod2.toISOString()]); | |
}); | |
//@ booAssembly = H4sIAAAAAAAAA+1UT08TURCfbaGW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U+Z8XSu52InuyG/Ib1LtpgAAKAAA= | |
//@ sourceMappingURL=timespan.js.map |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": 3, | |
"file": "timespan.js", | |
"sources": ["timespan.boo"], | |
"names": ["op_Addition","date","ms","Date","getTime","op_Subtraction","setUTCFullYear","setUTCHours","date_mod1","date_mod2","Boo","print","toISOString"], | |
"mappings": "BBBB,4DBBgB;UBBBB,cBBYC,KBBcC,MBCtC;UBBW,IBBJC,MBBoB,BBBH,BBBP,BBBLF,KBBKG,WBBYF;GBEjB;;UBBBG,iBBBeJ,KBBcC,MBCzC;UBBW,IBBJC,MBBoB,BBBH,BBBP,BBBLF,KBBKG,WBBYF;GBQjC;;iBBBB,MBDB;iBBBB,MBJK;YBBB,MBBL,BBBB,BBBKD;QBBW,IBBJE,MBBK,KBBM,GBBG,IBCP,BBBB,BBBd,BBDBF;OBCBK,eBBe,KBBM,GBBG,IBCb,BBBB,BBBX,BBFBL;OBEBM,YBBY,EBBG,GBBG,GBBG,IBEhB,BBBB,BBBVC;aBBiB,BBBBR,aBBLC,KBBK,WBCP,BBBB,BBBVQ;aBBiB,BBBBJ,gBBBLJ,KBBK,YBEjB,BBBB,BBBB,BBBBS;MBBBC,MBBB,CBBB,UBCB,BBBB,BBBB,BBBBD;MBBBC,MBBB,CBBe,UBCf,BBBB,BBBB,BBBBD;MBBBC,MBBB,CBBsB,BBBX,BBBLV,KBBKW,gBBCX,BBBB,BBBB,BBBBF;MBBBC,MBBB,CBB2B,BBBX,BBBVH,UBBUI,gBBChB,BBBB,BBBB,BBBBF;MBBBC,MBBB,CBB2B,BBBX,BBBVF,UBBUG" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment