37 lines
1.4 KiB
Text
37 lines
1.4 KiB
Text
// A guided tour of Wren running on calog. Wren reaches natives only through
|
|
// Calog.call(name, [args]), so print, crypto, and exit all go through it.
|
|
// Demonstrates: a class with a method, a List transformed with map, and the
|
|
// cryptoUuid / cryptoBase64Encode / calogPrint natives. Synchronous only.
|
|
// Run: bin/calog examples/scripts/languages/wren.wren
|
|
|
|
// A small class with a constructor and a method.
|
|
class Square {
|
|
construct new(n) {
|
|
_n = n
|
|
}
|
|
|
|
value { _n * _n }
|
|
}
|
|
|
|
// Build a List, then transform it with map (a Sequence) and materialize it.
|
|
var numbers = [1, 2, 3, 4, 5, 6]
|
|
var squares = numbers.map {|n| Square.new(n).value }.toList
|
|
|
|
// Reduce to a sum to show more of the List API.
|
|
var total = squares.reduce(0) {|acc, v| acc + v }
|
|
|
|
// Natives are reached via Calog.call(name, [args]).
|
|
Calog.call("calogPrint", ["squares 1..6 =", squares.join(", ")])
|
|
Calog.call("calogPrint", ["sum of squares =", total])
|
|
|
|
// A generated UUID, then base64-encoded through two more natives.
|
|
var id = Calog.call("cryptoUuid", [])
|
|
var encoded = Calog.call("cryptoBase64Encode", [id])
|
|
Calog.call("calogPrint", ["uuid =", id])
|
|
Calog.call("calogPrint", ["base64 =", encoded])
|
|
|
|
// String interpolation works too; pass the finished string to the native.
|
|
Calog.call("calogPrint", ["Wren squared %(numbers.count) numbers, total %(total)."])
|
|
|
|
// Tear down and exit cleanly (required -- calog does not auto-exit).
|
|
Calog.call("calogExit", [0])
|