calog/examples/scripts/languages/berry.be

42 lines
1.1 KiB
Text

# A guided tour of Berry (Python-like) running on calog.
# Demonstrates: a def function, a list built and iterated with a for loop, a map
# iterated over its keys, then the jsonStringify and cryptoUuid natives.
# Run: bin/calog examples/scripts/languages/berry.be
# A function.
def square(n)
return n * n
end
# Build a list of squares with a numeric range for loop.
var squares = []
for i : 1..6
squares.push(square(i))
end
# Iterate the list, accumulating a sum.
var total = 0
for v : squares
total = total + v
end
# list.concat joins the elements into a readable string.
calogPrint("squares 1..6 =", squares.concat(", "))
calogPrint("sum of squares =", total)
# A map (Berry's associative type), iterated over its keys.
var record = {
"language": "Berry",
"count": squares.size(),
"total": total,
"id": cryptoUuid()
}
for k : record.keys()
calogPrint(" ", k, "=", record[k])
end
# Serialize the map to JSON with the native.
calogPrint("json:", jsonStringify(record))
# Tear down and exit cleanly (required -- calog does not auto-exit).
calogExit(0)