← Back to Demos & Examples

02_pipelines_and_json.vk

Raw .vk
// Example 02: Module aliases, pipes, UFCS dot-chains, JSON, match, and reduce
// Track A (ORO VM) : veko run examples/02_pipelines_and_json.vk
// Track A (.vko)   : veko compile --vko examples/02_pipelines_and_json.vk -o dist/02.vko ; veko run dist/02.vko
// Track B (Wasm 3) : veko run --wasm examples/02_pipelines_and_json.vk
// Track C (Native) : veko compile --native examples/02_pipelines_and_json.vk -o dist/02.exe ; ./dist/02.exe
use [j = json, s = strings]

payload = '[{"name": " alpha ", "score": 90, "active": true}, {"name": " beta ", "score": 40, "active": false}, {"name": " gamma ", "score": 85, "active": true}]'

names = payload
	\ j/decode
	\ filter fn [item] [item.active]
	\ map fn [item] [item.name.trim.upper]

println (s/join names ' | ')

scores = payload
	\ j/decode
	\ filter fn [item] [item.active]
	\ map fn [item] [item.score]

total = reduce scores 0 fn [acc x] [acc + x]
println total

// Tagged variant matching with match
ev = { kind = .ready, code = 200 }
status-msg = match ev.kind [
	.starting ['booting']
	.ready    ['serving']
	_         ['unknown']
]
println status-msg