fix print!

This commit is contained in:
Scott Richmond 2024-12-13 13:47:03 -05:00
parent 73e60b8ced
commit 8535225167
2 changed files with 15 additions and 6 deletions

View File

@ -227,9 +227,10 @@ fn fold {
} }
} }
& TODO: optimize these with base :conj!
fn map { fn map {
"Maps a function over a list: returns a new list with elements that are the result of applying the function to each element in the original list. E.g., `map ([1, 2, 3], inc) &=> [2, 3, 4]`." "Maps a function over a list: returns a new list with elements that are the result of applying the function to each element in the original list. E.g., `map ([1, 2, 3], inc) &=> [2, 3, 4]`. With one argument, returns a function that is a mapper over lists; with two, it executes the mapping function right away."
(f as :fn) -> map (f, _)
(kw as :keyword) -> map (kw, _)
(f as :fn, xs) -> { (f as :fn, xs) -> {
fn mapper (prev, curr) -> append (prev, f (curr)) fn mapper (prev, curr) -> append (prev, f (curr))
fold (mapper, xs, []) fold (mapper, xs, [])
@ -242,6 +243,7 @@ fn map {
fn filter { fn filter {
"Takes a list and a predicate function, and returns a new list with only the items that produce truthy values when the function is called on them. E.g., `filter ([1, 2, 3, 4], odd?) &=> [1, 3]`." "Takes a list and a predicate function, and returns a new list with only the items that produce truthy values when the function is called on them. E.g., `filter ([1, 2, 3, 4], odd?) &=> [1, 3]`."
(p? as :fn) -> filter (p?, _)
(p? as :fn, xs) -> { (p? as :fn, xs) -> {
fn filterer (filtered, x) -> if p? (x) fn filterer (filtered, x) -> if p? (x)
then append (filtered, x) then append (filtered, x)
@ -256,7 +258,7 @@ fn keep {
} }
fn append { fn append {
"Adds an element to a list or set." "Adds an element to a list."
() -> [] () -> []
(xs as :list) -> xs (xs as :list) -> xs
(xs as :list, x) -> base :conj (xs, x) (xs as :list, x) -> base :conj (xs, x)
@ -273,7 +275,7 @@ fn append! {
fn concat { fn concat {
"Combines two lists, strings, or sets." "Combines two lists, strings, or sets."
(x as :string, y as :string) -> base :concat (x, y) (x as :string, y as :string) -> "{x}{y}"
(xs as :list, ys as :list) -> base :concat (xs, ys) (xs as :list, ys as :list) -> base :concat (xs, ys)
& (xs as :set, ys as :set) -> base :concat (xs, ys) & (xs as :set, ys as :set) -> base :concat (xs, ys)
(xs, ys, ...zs) -> fold (concat, zs, concat (xs, ys)) (xs, ys, ...zs) -> fold (concat, zs, concat (xs, ys))

View File

@ -259,8 +259,15 @@ pub fn or<'src>(x: &Value<'src>, y: &Value<'src>) -> Value<'src> {
// TODO: fix this: x is a list of all the args passed to Ludus's print! // TODO: fix this: x is a list of all the args passed to Ludus's print!
pub fn print<'src>(x: &Value<'src>) -> Value<'src> { pub fn print<'src>(x: &Value<'src>) -> Value<'src> {
// dbg!(x) let Value::List(args) = x else {
println!("{}", x); unreachable!("internal Ludus error")
};
let out = args
.iter()
.map(|val| format!("{val}"))
.collect::<Vec<_>>()
.join("");
println!("{out}");
Value::Keyword("ok") Value::Keyword("ok")
} }