Keep working on prelude.

This commit is contained in:
Scott Richmond 2023-12-06 20:29:21 -05:00
parent 8ce97081d0
commit 53c4433d2a

View File

@ -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 & what we need for some very basic list manipulation
fn rest { fn rest {
"Returns all but the first element of a list or tuple, as a list." "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)) (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? { fn list? {
"Returns true if the value is a list." "Returns true if the value is a list."
(l as :list) -> true (l as :list) -> true
@ -283,7 +302,7 @@ fn update! {
} }
&&& numbers, basically: arithmetic and not much else, yet &&& numbers, basically: arithmetic and not much else, yet
& TODO: add nan?,
fn number? { fn number? {
"Returns true if a value is a number." "Returns true if a value is a number."
(x as :number) -> true (x as :number) -> true
@ -434,6 +453,27 @@ fn pos? {
(_) -> false (_) -> 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 &&& nil: working with nothing
fn nil? { fn nil? {
@ -448,6 +488,12 @@ fn some? {
(_) -> true (_) -> 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 &&& true & false: boolean logic
fn bool? { fn bool? {
@ -457,6 +503,12 @@ fn bool? {
(_) -> false (_) -> 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 { fn bool {
"Returns false if a value is nil or false, otherwise returns true." "Returns false if a value is nil or false, otherwise returns true."
(nil) -> false (nil) -> false
@ -489,7 +541,7 @@ fn or {
} }
&&& associative collections: dicts, structs, namespaces &&& associative collections: dicts, structs, namespaces
& TODO?: get_in, update_in & TODO?: get_in, update_in, merge
fn assoc { fn assoc {
"Takes a dict, key, and value, and returns a new dict with the key set to value." "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) ((x, y)) -> dist (x, y)
} }
&&& Number functions &&& more number functions
& TODO: add max, min
fn random { 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." "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 () () -> base :random ()
@ -1039,6 +1092,9 @@ fn penwidth {
} }
ns prelude { ns prelude {
type
eq?
fn?
first first
second second
rest rest
@ -1059,12 +1115,16 @@ ns prelude {
console console
show show
prn! prn!
type
report! report!
panic!
doc!
concat concat
deref deref
make! make!
update! update!
string
string?
join
add add
sub sub
mult mult
@ -1077,14 +1137,18 @@ ns prelude {
zero? zero?
neg? neg?
pos? pos?
eq? even?
odd?
gt? gt?
gte? gte?
lt? lt?
lte? lte?
keyword?
nil? nil?
some? some?
some
bool? bool?
false?
bool bool
not not
and and
@ -1101,8 +1165,6 @@ ns prelude {
values values
diff diff
each! each!
panic!
doc!
sin sin
cos cos
tan tan
@ -1130,6 +1192,7 @@ ns prelude {
err? err?
unwrap! unwrap!
unwrap_or unwrap_or
assert!
colors colors
forward!, fd! forward!, fd!
back!, bk! back!, bk!