Some more stdlib fun and example code and cosmetic changes. (0.38.15)
This commit is contained in:
parent
cb5e61c091
commit
9016b207b8
12 changed files with 330 additions and 4 deletions
36
lib/utils/match.fun
Normal file
36
lib/utils/match.fun
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-02-19
|
||||
*/
|
||||
|
||||
// A simple dynamic match helper.
|
||||
// Usage:
|
||||
// match(x, [
|
||||
// {"when": fun(v) v > 0, "then": fun(v) print("pos")},
|
||||
// {"is": 0, "then": fun(v) print("zero")},
|
||||
// {"else": fun(v) print("neg or other")}
|
||||
// ])
|
||||
|
||||
fun match(value, cases)
|
||||
// Iterate cases in order; support keys: is, when, else
|
||||
for c in cases
|
||||
if (has_key(c, "is"))
|
||||
if (value == c["is"])
|
||||
th = c["then"]
|
||||
return th(value)
|
||||
else if (has_key(c, "when"))
|
||||
pred = c["when"]
|
||||
if (pred(value))
|
||||
th = c["then"]
|
||||
return th(value)
|
||||
else if (has_key(c, "else"))
|
||||
el = c["else"]
|
||||
return el(value)
|
||||
// No match; return nil
|
||||
return nil
|
||||
71
lib/utils/option.fun
Normal file
71
lib/utils/option.fun
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-02-19
|
||||
*/
|
||||
|
||||
// Option helpers for ergonomic error-absent values.
|
||||
// Represented as dictionaries with a _tag and optional value field.
|
||||
|
||||
// Constructors
|
||||
fun some(x)
|
||||
return {"_tag": "Some", "value": x}
|
||||
|
||||
fun none()
|
||||
return {"_tag": "None"}
|
||||
|
||||
// Predicates
|
||||
fun is_some(opt)
|
||||
return opt["_tag"] == "Some"
|
||||
|
||||
fun is_none(opt)
|
||||
return opt["_tag"] == "None"
|
||||
|
||||
// Extractors
|
||||
fun unwrap(opt)
|
||||
if is_some(opt)
|
||||
return opt["value"]
|
||||
// Fall back to raising a runtime error
|
||||
error("called unwrap() on None")
|
||||
return nil
|
||||
|
||||
fun unwrap_or(opt, default)
|
||||
if is_some(opt)
|
||||
return opt["value"]
|
||||
return default
|
||||
|
||||
// Functional helpers
|
||||
// map(opt, f) -> Some(f(value)) or None
|
||||
fun option_map(opt, f)
|
||||
if is_some(opt)
|
||||
return some(f(opt["value"]))
|
||||
return none()
|
||||
|
||||
// and_then(opt, f) where f: a -> Option[b]
|
||||
fun and_then(opt, f)
|
||||
if is_some(opt)
|
||||
return f(opt["value"])
|
||||
return none()
|
||||
|
||||
// or_else(opt, f) where f: () -> Option[a]
|
||||
fun or_else(opt, f)
|
||||
if is_some(opt)
|
||||
return opt
|
||||
return f()
|
||||
|
||||
// to_result(opt, err)
|
||||
fun to_result(opt, err)
|
||||
if is_some(opt)
|
||||
return ok(opt["value"])
|
||||
return err(err)
|
||||
|
||||
// Convenience: try_get(dict, key) -> Option[value]
|
||||
fun try_get(dict, key)
|
||||
if has_key(dict, key)
|
||||
return some(dict[key])
|
||||
return none()
|
||||
74
lib/utils/result.fun
Normal file
74
lib/utils/result.fun
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-02-19
|
||||
*/
|
||||
|
||||
// Result helpers: Ok(value) or Err(error)
|
||||
|
||||
// Constructors
|
||||
fun ok(x)
|
||||
return {"_tag": "Ok", "value": x}
|
||||
|
||||
fun err(e)
|
||||
return {"_tag": "Err", "error": e}
|
||||
|
||||
// Predicates
|
||||
fun is_ok(res)
|
||||
return res["_tag"] == "Ok"
|
||||
|
||||
fun is_err(res)
|
||||
return res["_tag"] == "Err"
|
||||
|
||||
// Extractors
|
||||
fun unwrap(res)
|
||||
if is_ok(res)
|
||||
return res["value"]
|
||||
error("called unwrap() on Err: " + to_string(res["error"]))
|
||||
return nil
|
||||
|
||||
fun unwrap_or(res, default)
|
||||
if is_ok(res)
|
||||
return res["value"]
|
||||
return default
|
||||
|
||||
fun unwrap_err(res)
|
||||
if is_err(res)
|
||||
return res["error"]
|
||||
error("called unwrap_err() on Ok")
|
||||
return nil
|
||||
|
||||
// map(res, f) applies f to Ok value; Err passes through
|
||||
fun result_map(res, f)
|
||||
if is_ok(res)
|
||||
return ok(f(res["value"]))
|
||||
return res
|
||||
|
||||
// map_err(res, f) applies f to error; Ok passes through
|
||||
fun map_err(res, f)
|
||||
if is_err(res)
|
||||
return err(f(res["error"]))
|
||||
return res
|
||||
|
||||
// and_then(res, f) where f: a -> Result[b]
|
||||
fun and_then(res, f)
|
||||
if is_ok(res)
|
||||
return f(res["value"]) // caller should return Result
|
||||
return res
|
||||
|
||||
// or_else(res, f) where f: () -> Result[a]
|
||||
fun or_else(res, f)
|
||||
if is_ok(res)
|
||||
return res
|
||||
return f()
|
||||
|
||||
// to_option(res) -> Some(value) or None
|
||||
fun to_option(res)
|
||||
if is_ok(res)
|
||||
return some(res["value"]) // requires option.fun loaded
|
||||
return none()
|
||||
Loading…
Add table
Add a link
Reference in a new issue