move nil? to fix binding; use append! for turtle states

This commit is contained in:
Scott Richmond 2024-06-24 10:54:54 -04:00
parent a30cbaebc6
commit 9752a87f27
2 changed files with 50 additions and 33 deletions

View File

@ -55,6 +55,26 @@ fn assoc? {
(_) -> false (_) -> false
} }
&&& nil: working with nothing
fn nil? {
"Returns true if a value is nil."
(nil) -> true
(_) -> false
}
fn some? {
"Returns true if a value is not nil."
(nil) -> false
(_) -> 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
}
& ...and if two things are the same & ...and if two things are the same
fn eq? { fn eq? {
"Returns true if all arguments have the same value." "Returns true if all arguments have the same value."
@ -244,6 +264,13 @@ fn append {
(xs as :set, x) -> base :conj (xs, x) (xs as :set, x) -> base :conj (xs, x)
} }
fn append! {
"Adds an element to a list, modifying it."
() -> []
(xs as :list) -> xs
(xs as :list, x) -> base :conj! (xs, x)
}
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) -> base :concat (x, y)
@ -684,27 +711,6 @@ fn keyword? {
& 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. & 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? {
"Returns true if a value is nil."
(nil) -> true
(_) -> false
}
fn some? {
"Returns true if a value is not nil."
(nil) -> false
(_) -> 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
}
& TODO: make `and` and `or` special forms which lazily evaluate arguments & TODO: make `and` and `or` special forms which lazily evaluate arguments
fn and { fn and {
"Returns true if all values passed in are truthy. Note that this does not short-circuit: all arguments are evaulated before they are passed in." "Returns true if all values passed in are truthy. Note that this does not short-circuit: all arguments are evaulated before they are passed in."
@ -1094,13 +1100,13 @@ box p5_calls = []
& it must be the first call to p5. & it must be the first call to p5.
box bgcolor = colors :black box bgcolor = colors :black
fn add_call! (call) -> update! (p5_calls, append (_, call)) fn add_call! (call) -> update! (p5_calls, append! (_, call))
fn add_command! (command) -> { fn add_command! (command) -> {
update! (turtle_commands, append (_, command)) update! (turtle_commands, append! (_, command))
let prev = do turtle_states > unbox > last let prev = do turtle_states > unbox > last
let curr = apply_command (prev, command) let curr = apply_command (prev, command)
update! (turtle_states, append (_, curr)) update! (turtle_states, append! (_, curr))
let call = state/call () let call = state/call ()
if call then { add_call! (call); :ok } else :ok if call then { add_call! (call); :ok } else :ok
} }
@ -1323,7 +1329,7 @@ fn turtle_state {
fn load_turtle_state! { fn load_turtle_state! {
"Sets the turtle state to a previously saved state. Returns the state." "Sets the turtle state to a previously saved state. Returns the state."
(state) -> { (state) -> {
update! (turtle_states, append (_, state)) update! (turtle_states, append! (_, state))
let call = state/call () let call = state/call ()
if call then { add_call! (call); :ok } else :ok if call then { add_call! (call); :ok } else :ok
} }

View File

@ -47,22 +47,33 @@
(set post (i/interpret prelude/post/ast ctx)) (set post (i/interpret prelude/post/ast ctx))
([err] (e/runtime-error err))) ([err] (e/runtime-error err)))
(set (out :draw) (post :draw)) (set (out :draw) (post :draw))
(-> out j/encode string)) out
# (-> out j/encode string)
)
(comment (comment
# (do # (do
(def start (os/clock))
(def source ` (def source `
add (1, 2) repeat 100 {
repeat 100 {
fd! (1)
rt! (inv (100))
}
rt! (inv (100))
}
`) `)
(def out (-> source (def out (-> source
ludus ludus
j/decode # j/decode
)) ))
(def end (os/clock))
(setdyn :out stdout) (setdyn :out stdout)
# (pp out) (pp out)
(def console (out "console")) # (def console (out "console"))
(print console) # (print console)
(def result (out "result")) # (def result (out "result"))
(print result) # (print result)
(print (- end start))
) )