2013-09-20 08:31:28 +00:00
---
language: pogoscript
contributors:
- ["Tim Macfarlane", "http://github.com/refractalize"]
filename: learnPogo.pogo
---
Pogoscript is a little language that emphasises readability, DSLs and provides excellent asynchronous primitives for writing connected JavaScript applications for the browser or server.
2013-09-20 10:43:05 +00:00
``` javascript
2013-09-20 10:14:56 +00:00
// defining a variable
2013-09-20 08:31:28 +00:00
water temperature = 24
2013-09-20 10:14:56 +00:00
// re-assigning a variable after its definition
2013-09-20 08:31:28 +00:00
water temperature := 26
2013-09-20 10:14:56 +00:00
// functions allow their parameters to be placed anywhere
temperature at (a) altitude = 32 - a / 100
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// longer functions are just indented
temperature at (a) altitude :=
if (a < 0 )
2013-09-20 08:31:28 +00:00
water temperature
else
2013-09-20 10:14:56 +00:00
32 - a / 100
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// calling a function
2013-09-20 08:31:28 +00:00
current temperature = temperature at 3200 altitude
2013-09-20 10:14:56 +00:00
// this function constructs a new object with methods
2013-09-20 08:31:28 +00:00
position (x, y) = {
x = x
y = y
distance from position (p) =
dx = self.x - p.x
dy = self.y - p.y
Math.sqrt (dx * dx + dy * dy)
}
2013-09-20 10:14:56 +00:00
// `self` is similar to `this` in JavaScript with the
// exception that `self` isn't redefined in each new
// function definition
// `self` just does what you expect
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// calling methods
2013-09-20 08:31:28 +00:00
position (7, 2).distance from position (position (5, 1))
2013-09-20 10:14:56 +00:00
// as in JavaScript, objects are hashes too
2013-09-20 08:31:28 +00:00
position.'x' == position.x == position.('x')
2013-09-20 10:14:56 +00:00
// arrays
2013-09-20 08:31:28 +00:00
positions = [
position (1, 1)
position (1, 2)
position (1, 3)
]
2013-09-20 10:14:56 +00:00
// indexing an array
2013-09-20 08:31:28 +00:00
positions.0.y
n = 2
positions.(n).y
2013-09-20 10:14:56 +00:00
// strings
2013-09-20 08:31:28 +00:00
poem = 'Tail turned to red sunset on a juniper crown a lone magpie cawks.
Mad at Oryoki in the shrine-room -- Thistles blossomed late afternoon.
Put on my shirt and took it off in the sun walking the path to lunch.
A dandelion seed floats above the marsh grass with the mosquitos.
At 4 A.M. the two middleaged men sleeping together holding hands.
In the half-light of dawn a few birds warble under the Pleiades.
Sky reddens behind fir trees, larks twitter, sparrows cheep cheep cheep
cheep cheep.'
2013-09-20 10:14:56 +00:00
// that's Allen Ginsburg
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// interpolation
2013-09-20 08:31:28 +00:00
outlook = 'amazing!'
console.log "the weather tomorrow is going to be #(outlook)"
2013-09-20 10:14:56 +00:00
// regular expressions
r/(\d+)m/i
r/(\d+) degrees/mg
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// operators
2013-09-20 08:31:28 +00:00
true @and true
false @or true
@not false
2 < 4
2 >= 2
2 > 1
// plus all the javascript ones
2013-09-20 10:14:56 +00:00
// to define your own
2013-09-20 08:31:28 +00:00
(p1) plus (p2) =
position (p1.x + p2.x, p1.y + p2.y)
2013-09-20 10:14:56 +00:00
// `plus` can be called as an operator
2013-09-20 08:31:28 +00:00
position (1, 1) @plus position (0, 2)
2013-09-20 10:14:56 +00:00
// or as a function
2013-09-20 08:31:28 +00:00
(position (1, 1)) plus (position (0, 2))
2013-09-20 10:14:56 +00:00
// explicit return
2013-09-20 08:31:28 +00:00
(x) times (y) = return (x * y)
2013-09-20 10:14:56 +00:00
// new
2013-09-20 08:31:28 +00:00
now = @new Date ()
2013-09-20 10:14:56 +00:00
// functions can take named optional arguments
2013-09-20 08:31:28 +00:00
spark (position, color: 'black', velocity: {x = 0, y = 0}) = {
color = color
position = position
velocity = velocity
}
red = spark (position 1 1, color: 'red')
fast black = spark (position 1 1, velocity: {x = 10, y = 0})
2013-09-20 10:14:56 +00:00
// functions can unsplat arguments too
2013-09-20 08:31:28 +00:00
log (messages, ...) =
console.log (messages, ...)
2013-09-20 10:14:56 +00:00
// blocks are functions passed to other functions.
// This block takes two parameters, `spark` and `c` ,
// the body of the block is the indented code after the
// function call
2013-09-20 08:31:28 +00:00
render each @(spark) into canvas context @(c)
ctx.begin path ()
ctx.stroke style = spark.color
ctx.arc (
spark.position.x + canvas.width / 2
spark.position.y
3
0
Math.PI * 2
)
ctx.stroke ()
2013-09-20 10:14:56 +00:00
// asynchronous calls
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// JavaScript both in the browser and on the server (with Node.js)
// makes heavy use of asynchronous IO with callbacks. Async IO is
// amazing for performance and making concurrency simple but it
// quickly gets complicated.
// Pogoscript has a few things to make async IO much much easier
// Node.js includes the `fs` module for accessing the file system.
// Let's list the contents of a directory
2013-09-20 08:31:28 +00:00
fs = require 'fs'
directory listing = fs.readdir! '.'
2013-09-20 10:14:56 +00:00
// `fs.readdir()` is an asynchronous function, so we can call it
// using the `!` operator. The `!` operator allows you to call
// async functions with the same syntax and largely the same
// semantics as normal synchronous functions. Pogoscript rewrites
// it so that all subsequent code is placed in the callback function
// to `fs.readdir()` .
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// to catch asynchronous errors while calling asynchronous functions
2013-09-20 08:31:28 +00:00
try
another directory listing = fs.readdir! 'a-missing-dir'
catch (ex)
console.log (ex)
2013-09-20 10:14:56 +00:00
// in fact, if you don't use `try catch` , it will raise the error up the
// stack to the outer-most `try catch` or to the event loop, as you'd expect
// with non-async exceptions
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// all the other control structures work with asynchronous calls too
// here's `if else`
config =
if (fs.stat! 'config.json'.is file ())
JSON.parse (fs.read file! 'config.json' 'utf-8')
else
{
color: 'red'
}
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// to run two asynchronous calls concurrently, use the `?` operator.
// The `?` operator returns a *future* which can be executed to
// wait for and obtain the result, again using the `!` operator
2013-09-20 08:31:28 +00:00
2013-09-20 10:14:56 +00:00
// we don't wait for either of these calls to finish
2013-09-20 08:31:28 +00:00
a = fs.stat? 'a.txt'
b = fs.stat? 'b.txt'
2013-09-20 10:14:56 +00:00
// now we wait for the calls to finish and print the results
2013-09-20 08:31:28 +00:00
console.log "size of a.txt is #(a!.size)"
console.log "size of b.txt is #(b!.size)"
2013-09-20 10:14:56 +00:00
// futures in Pogoscript are analogous to Promises
2013-09-20 08:31:28 +00:00
```
That's it.
Download [Node.js ](http://nodejs.org/ ) and `npm install pogo` .
There is plenty of documentation on [http://pogoscript.org/ ](http://pogoscript.org/ ), inlcuding a [cheat sheet ](http://pogoscript.org/cheatsheet.html ), a [guide ](http://pogoscript.org/guide/ ), and how [Pogoscript translates to Javascript ](http://featurist.github.io/pogo-examples/ ). Get in touch on the [google group ](http://groups.google.com/group/pogoscript ) if you have questions!