| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
A scripting language for spreadsheet formulas, CLI tools, ETL pipelines.
Aka can be used as transformers:
Input data is untyped.
when x is
true "Good job!"
false "Try again!"Manipulating JSON:
---
description: Convert item prices from € to % of total price
tags: [json]
---
json_result = $parse_json($input)
items_result = when json_result is
Null -> Error("Provide an Array and not Null")
Object(hash_map) -> hash_map.items # `.` on hash maps returns Result
Array(items) -> Ok(items)
_ -> Error("Provide an Array and not a primitive value")
items_total_price = Result:map(items_result,
items -> items
& List:map(item -> item.price & Result:withDefault(0))
& List:sum
)
# Final expression must be a `Result`
# - `Error`: E.g. printed to stderr, shown as cell error, …
# - `Ok`: E.g. printed to stdout (`Json` is automatically stringified),
# shown as cell content, …
items_result
& Result:map(items ->
items & List:map(item ->
item.price
& Result:map(price -> price / items_total)
)
)Instead of threading the errors through the whole program, this can also be simplified to:
---
description: Convert item prices from € to % of total price
tags: [json]
---
json_result = $parse_json($input)
items = when json_result is
Array(items) -> items
Null -> $stop(Error("Provide an Array and not Null"))
Object(hash_map) -> $stop_error(hash_map.items) # `.x` returns Result
_ -> stop(Error("Provide an Array and not a primitive value"))
items_total_price = items
& List:map(item -> item.price & Result:withDefault(0))
& List:sum
Ok(items & List:map(item ->
item.price & Result:map(price ->
price / items_total_price
)
))Deal-breakers are marked with a ✋.
Numbat (Rust)
Roc (Rust)
| Back | FazBrowse Home | New Git URL |