diff --git a/prelude.ld b/prelude.ld index b15e3d5..8230825 100644 --- a/prelude.ld +++ b/prelude.ld @@ -55,6 +55,26 @@ fn assoc? { (_) -> 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 fn eq? { "Returns true if all arguments have the same value." @@ -244,6 +264,13 @@ fn append { (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 { "Combines two lists, strings, or sets." (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. -&&& 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 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." @@ -1094,13 +1100,13 @@ box p5_calls = [] & it must be the first call to p5. 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) -> { - update! (turtle_commands, append (_, command)) + update! (turtle_commands, append! (_, command)) let prev = do turtle_states > unbox > last let curr = apply_command (prev, command) - update! (turtle_states, append (_, curr)) + update! (turtle_states, append! (_, curr)) let call = state/call () if call then { add_call! (call); :ok } else :ok } @@ -1323,7 +1329,7 @@ fn turtle_state { fn load_turtle_state! { "Sets the turtle state to a previously saved state. Returns the state." (state) -> { - update! (turtle_states, append (_, state)) + update! (turtle_states, append! (_, state)) let call = state/call () if call then { add_call! (call); :ok } else :ok } diff --git a/src/ludus.janet b/src/ludus.janet index b438c35..c81a54f 100644 --- a/src/ludus.janet +++ b/src/ludus.janet @@ -47,22 +47,33 @@ (set post (i/interpret prelude/post/ast ctx)) ([err] (e/runtime-error err))) (set (out :draw) (post :draw)) - (-> out j/encode string)) + out + # (-> out j/encode string) + ) (comment # (do + (def start (os/clock)) (def source ` - add (1, 2) +repeat 100 { + repeat 100 { + fd! (1) + rt! (inv (100)) + } + rt! (inv (100)) +} `) (def out (-> source ludus - j/decode + # j/decode )) + (def end (os/clock)) (setdyn :out stdout) - # (pp out) - (def console (out "console")) - (print console) - (def result (out "result")) - (print result) + (pp out) + # (def console (out "console")) + # (print console) + # (def result (out "result")) + # (print result) + (print (- end start)) )