examples
every program here runs as-is. open one in the playground, change a line, run it again.
01hello.lum
try it →your first program — variables, strings and string math
// Welcome to Lumen ✦ let name = "world" print("Hello, " + name + "!") print("Lumen " * 3)
02pipes.lum
try it →filter, map and sum a list in one readable chain
// |> passes the value on the left as the first argument [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |> filter(fn(n) { n % 2 == 0 }) |> map(fn(n) { n * n }) |> sum |> print
03fibonacci.lum
try it →recursion, where a function returns its last line
fn fib(n) { if n < 2 { n } else { fib(n - 1) + fib(n - 2) } } for i in range(15) { print("fib(" + i + ") =", fib(i)) }
04fizzbuzz.lum
try it →if/else as a value, right inside a loop
for i in range(1, 21) { let out = if i % 15 == 0 { "FizzBuzz" } else if i % 3 == 0 { "Fizz" } else if i % 5 == 0 { "Buzz" } else { i } print(out) }
05closures.lum
try it →a function that remembers its own counter
fn counter() { let count = 0 fn() { count += 1; count } } let tick = counter() tick(); tick() print("ticked", tick(), "times")
06maps.lum
try it →named values, updates and walking the keys
let hero = { name: "Ada", level: 7, skills: ["logic", "poetry"] } hero.level += 1 print(hero.name, "is level", hero.level) for key in hero { print(" -", key, "=>", hero[key]) }
07primes.lum
try it →while loops, early return and a pipe to the finish
fn is_prime(n) { if n < 2 { return false } let i = 2 while i * i <= n { if n % i == 0 { return false } i += 1 } true } range(2, 60) |> filter(is_prime) |> join(", ") |> print
08json.lum
try it →read structured data, transform it, send JSON back
// Read structured data, transform it, send JSON back let data = json_parse("{\"scores\":[12,3,25,8],\"name\":\"Ada\"}") let top = data.scores |> sort |> reverse |> slice(0, 2) print(data.name, "top scores:", top) { name: data.name, top: top } |> json_stringify |> print
09collections.lum
try it →sort objects, find the first match, ask any and every
let tasks = [ { name: "parser", done: true, priority: 2 }, { name: "compiler", done: false, priority: 1 }, { name: "tests", done: true, priority: 3 } ] tasks |> sort(fn(a, b) { a.priority - b.priority }) |> map(fn(task) { task.name }) |> join(", ") |> print print("next:", find(tasks, fn(task) { not task.done }).name) print("all done:", every(tasks, fn(task) { task.done })) print("some done:", any(tasks, fn(task) { task.done })) let versions = { lumen: 1, tools: 2 } print(has(versions, "lumen"), values(versions), entries(versions))
10strings.lum
try it →trim, replace, search and count words
// Text tools: shape, search and count words let shout = " hello lumen " |> trim |> upper print(shout) print(replace("a-b-c", "-", " + ")) print(starts_with("lumen", "lu"), ends_with("lumen", "en")) print(includes("glow", "low"), includes([1, 2, 3], 2)) let words = split("the glow of the code", " ") print("the appears", count(words, fn(w) { w == "the" }), "times")
11lists.lum
try it →dedupe, flatten and pair up lists
// Reshape lists: dedupe, flatten, pair up print(unique([1, 2, 2, 3, 1])) print(flatten([[1, 2], [3], [4, 5]])) print(zip(["a", "b", "c"], [1, 2, 3])) let scores = [5, 120, 42, 300] print(scores |> map(fn(s) { clamp(s, 0, 100) })) print(pow(2, 10), clamp(7, 0, 5))
12continue.lum
try it →skip an iteration without leaving the loop
// Skip an iteration without leaving the loop let total = 0 for n in range(1, 7) { if n % 2 == 0 { continue } total += n } print("odd total:", total) let n = 0 while n < 5 { n += 1 if n < 3 { continue } print(n) }
new to the language? the beginner data lesson walks through JSON, sorting, searching and maps step by step.