UNPKG

cor-lang

Version:
217 lines (177 loc) 6.15 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="../assets/style.css"> <link rel="shortcut icon" href="../assets/favicon.ico" /> <script type="text/javascript" src="../../dist/cor.js"></script> <script type="text/javascript" src="../assets/script.js"></script> <script data-entry="./playground.cor"></script> <title>Cor - Playground</title> </head> <body> <span id="top"></span> <nav class="navbar" role="navigation"> <div class="nav content"> <ul class="left"> <li class="inline"><a href="../../index.html">Cor</a></li> <li class="inline"><a href="../get_started.html">Get started</a></li> <li class="inline"><a href="../documentation.html">Documentation</a></li> <li class="inline"><a href="index.html">Playground</a></li> <li class="inline"><a href="../../test/index.html">Run tests</a></li> </ul> <ul class="right"> <li class="inline"><a class="icon-github" href="https://github.com/yosbelms/cor" target="_blank"></a></li> </ul> </div> </nav> <div class="body pg-content"> <p><h1 style="display: inline;">Playground </h1><small>it is a living Cor example, you can find the source code <a href="https://github.com/yosbelms/cor/blob/master/docs/playground">here</a></small></p> <br /> Start with <select id="sample-select"> <option name="play-fib">Fibonacci</option> <option name="play-numbers">Delayed numbers</option> <option name="play-mouser">Mouser</option> <option name="play-pingpong">Ping-Pong</option> <option name="play-model">Model</option> </select> <div class="pg-toolbar"> <a class="pg-btn-run" href="javascript:void 0" id="btn-run">Run</a> <a class="pg-btn-link" href="javascript:void 0" id="btn-link">Link</a> </div> <ul class="pg-editor"> <li class="inline"> <textarea class="pg-source" id="source" spellcheck="false"></textarea> </li> <li class="inline"> <textarea class="pg-output" id="output" spellcheck="false"></textarea> </li> </ul> <script id="play-fib" type="text/cor">// fib() returns a function which generates // succesive fibonacci numbers func fib(n) { a = 0 b = 1 return func()(t = a + b, a = b, b = t)[0] } func init() { gen = fib() res = (,) for i in 2:15 { echo($'{i}, {gen()}') } } </script> <script id="play-mouser" type="text/cor">// Channels in action // just run the example and move // the mouse over the output panel outEl = document.getElementById('output') func init() { offsetCh = chan(200) outEl.onmousemove = func(event) go { offsetCh <- (event.clientX, event.clientY) } go { echo('Move the mouse over me !!!') for i in 0:500 { offset = <- offsetCh echo($'X:{offset[0]} - Y:{offset[1]}') timeout(10) clearOutput() } } } </script> <script id="play-pingpong" type="text/cor">// Ping-pong example ported from Go // http://talks.golang.org/2013/advconc.slide#6 // Using unbuffered channels to synchronize coroutines func player(name, table) go { for { ball = <- table if table.closed { echo($'{name}: Table is gone...') return } ball.hits++ echo($'{name} {ball.hits}') timeout(500) catch table <- ball { return } } } func init() go { table = chan() // send to the channel inside a new coroutine // to avoid blocking the current context go {table <- (hits: 0)} player('Ping', table) player('Pong', table) timeout(5000) table.close() } </script> <script id="play-numbers" type="text/cor">// Prints numbers from 0 to 9 to the output panel // with an interval of 400 millisecond between each print func init() go { for i in 0:10 { echo(i) timeout(400) } } </script> <script id="play-model" type="text/cor">// Show inheritance and literal object contructions // id generator idSeed = 0 func genId() { idSeed++ return $'id-{idSeed}' } // Model class class Model { id = genId() func toJSON() (id: me.id) } // inherit from model class Todo : Model { desc func toJSON() { obj = super() obj.desc = me.desc obj.type = 'Todo' return obj } } // initialize module func init() { echo(JSON.stringify(( &Todo('Buy a battery'), &Todo('Clean the fan'), &Todo('Make a push'), ), 0, 4)) } </script> <div class="rule"></div> <div class="rule"></div> <div id="footer" class="feature footer-links"> <ul> <li><a href="https://github.com/yosbelms/cor">GitHub Repository</a></li> <li>&middot;</li> <li><a href="https://github.com/yosbelms/cor/issues">Issues</a></li> </ul> </div> </div> <div class="feature footer"> <p>Designed, written, and built with love by <a href="https://github.com/yosbelms" target="_blank">Yosbel Marín</a>. Code licensed under <a href="https://github.com/yosbelms/cor/blob/master/LICENSE" target="_blank">BSD</a>, documentation under <a href="http://creativecommons.org/licenses/by/3.0/">CC BY 3.0</a>.</p> </div> <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-73271106-1', 'auto'); ga('send', 'pageview'); </script> </body> </html>