From 53c4433d2aeb9eb53d68a2cd0a9e16d0149bea9a Mon Sep 17 00:00:00 2001 From: Scott Richmond Date: Wed, 6 Dec 2023 20:29:21 -0500 Subject: [PATCH] Keep working on prelude. --- src/ludus/prelude.ld | 77 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/src/ludus/prelude.ld b/src/ludus/prelude.ld index 47001ba..bfb96d1 100644 --- a/src/ludus/prelude.ld +++ b/src/ludus/prelude.ld @@ -19,6 +19,15 @@ fn eq? { } } +& TODO: add not_eq + +&&& functions: getting things done +fn fn? { + "Returns true if an argument is a function." + (f as :fn) -> true + (_) -> false +} + & what we need for some very basic list manipulation fn rest { "Returns all but the first element of a list or tuple, as a list." @@ -94,6 +103,16 @@ fn count { (xs as :struct) -> dec (base :count (xs)) } +fn empty? { + "Returns true if something is empty. Otherwise returns false (including for things that can't logically be empty, like numbers)." + ([]) -> true + (#{}) -> true + (${}) -> true + (()) -> true + ("") -> true + (_) -> false +} + fn list? { "Returns true if the value is a list." (l as :list) -> true @@ -283,7 +302,7 @@ fn update! { } &&& numbers, basically: arithmetic and not much else, yet - +& TODO: add nan?, fn number? { "Returns true if a value is a number." (x as :number) -> true @@ -434,6 +453,27 @@ fn pos? { (_) -> false } +fn even? { + "Returns true if a value is an even number, otherwise returns false." + (x as :number) if eq (0, mod (x, 2)) -> true + (_) -> false +} + +fn odd? { + "Returns true if a value is an odd number, otherwise returns false." + (x as :number) if eq (1, mod (x, 2)) -> true + (_) -> false +} + +&&& keywords: funny names +fn keyword? { + "Returns true if a value is a keyword, otherwise returns false." + (kw as :keyword) -> true + (_) -> false +} + +& TODO: determine if Ludus should have a `keyword` function that takes a string and returns a keyword. Too many panics, it has weird memory consequences, etc. + &&& nil: working with nothing fn nil? { @@ -448,6 +488,12 @@ fn some? { (_) -> true } +fn some { + "Takes a possibly nil value and a default value. Returns the value if it's not nil, returns the default if it's nil." + (nil, default) -> default + (value, _) -> value +} + &&& true & false: boolean logic fn bool? { @@ -457,6 +503,12 @@ fn bool? { (_) -> false } +fn false? { + "Returns true if a value is false, otherwise returns false. Useful to distinguish between false and nil." + (false) -> true + (_) -> false +} + fn bool { "Returns false if a value is nil or false, otherwise returns true." (nil) -> false @@ -489,7 +541,7 @@ fn or { } &&& associative collections: dicts, structs, namespaces -& TODO?: get_in, update_in +& TODO?: get_in, update_in, merge fn assoc { "Takes a dict, key, and value, and returns a new dict with the key set to value." () -> #{} @@ -699,7 +751,8 @@ fn dist { ((x, y)) -> dist (x, y) } -&&& Number functions +&&& more number functions +& TODO: add max, min fn random { "Returns a random number. With zero arguments, returns a random number between 0 (inclusive) and 1 (exclusive). With one argument, returns a random number between 0 and n. With two arguments, returns a random number between m and n." () -> base :random () @@ -1039,6 +1092,9 @@ fn penwidth { } ns prelude { + type + eq? + fn? first second rest @@ -1059,12 +1115,16 @@ ns prelude { console show prn! - type report! + panic! + doc! concat deref make! update! + string + string? + join add sub mult @@ -1077,14 +1137,18 @@ ns prelude { zero? neg? pos? - eq? + even? + odd? gt? gte? lt? lte? + keyword? nil? some? + some bool? + false? bool not and @@ -1101,8 +1165,6 @@ ns prelude { values diff each! - panic! - doc! sin cos tan @@ -1130,6 +1192,7 @@ ns prelude { err? unwrap! unwrap_or + assert! colors forward!, fd! back!, bk!