A peer-to-peer stack for code collaboration
Streamline vector and list functions (#277)
Thomas Scholtes30876d2d748a
diff --git a/docs/source/guide/Basics.lrad b/docs/source/guide/Basics.lradindex ecd61f0..832a8fe 100644--- a/docs/source/guide/Basics.lrad+++ b/docs/source/guide/Basics.lrad@@ -56,10 +56,10 @@ Concatenate vectors using `<>`:(<> [1 2 3] [4 5 6]) ;; ==> [1 2 3 4 5 6]```-Add an element to the left using `add-left`:+Add an element to the left using `cons`:```radicle-(add-left 0 [1 2 3]) ;; ==> [0 1 2 3]+(cons 0 [1 2 3]) ;; ==> [0 1 2 3]```Add an element to the right using `add-right`:diff --git a/docs/source/reference.rst b/docs/source/reference.rstindex 8b24949..b697925 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -108,21 +108,22 @@ Given a reference ``r`` and a value ``v``, updates the value stored inThe most basic built-in pattern-matching dispatch function.-``head``+``cons``~~~~~~~~+Adds an element to the front of a sequence.++``first``+~~~~~~~~~+Retrieves the first element of a sequence if it exists. Otherwise throwsan exception.-``cons``+``rest``~~~~~~~~-Adds an element to the front of a list.--``add-left``-~~~~~~~~~~~~--Adds an element to the left side of a vector.+Given a non-empty sequence, returns the sequence of all the elements but+the first. If the sequence is empty, throws an exception.``add-right``~~~~~~~~~~~~~@@ -192,12 +193,6 @@ is empty, in which case an exception is thrown.Given a sequence ``xs`` and a function ``f``, returns a sequence withthe same elements ``x`` of ``xs`` but sorted according to ``(f x)``.-``tail``-~~~~~~~~--Given a non-empty sequence, returns the sequence of all the elements but-the first. If the sequence is empty, throws an exception.-``take``~~~~~~~~@@ -576,6 +571,16 @@ left (i.e. from left to right) in the maybe monad.Returns true if ``x`` is an element of the sequence ``xs``+``head``+~~~~~~~~++Backwards compatible alias for ``first``.++``tail``+~~~~~~~~++Backwards compatible alias for ``rest``.+``prelude/patterns``--------------------@@ -613,12 +618,12 @@ bound to the matched value.``(/cons x-pat xs-pat)``~~~~~~~~~~~~~~~~~~~~~~~~-A pattern for lists with a head and a tail.+A pattern for sequences with a head and a tail.``(/nil v)``~~~~~~~~~~~~-Empty-list pattern.+Empty-sequence pattern. Matches ``[]`` and ``(list)````(/just pat)``~~~~~~~~~~~~~~~diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.radindex f1af842..0eda35d 100644--- a/rad/monadic/issue-remote.rad+++ b/rad/monadic/issue-remote.rad@@ -157,8 +157,8 @@(def process-command"Function that maps post-eval commands to existing functions"(fn [expr]- (def command (head expr))- (def args (tail expr))+ (def command (first expr))+ (def args (rest expr))(def do-this (lookup command (read-ref commands)))(apply do-this args)))@@ -171,7 +171,7 @@state, while also calling the callback on the result."(fn [state f arg cb](def exec (base-eval (list f (list 'quote arg)) state))- (cb (head exec))+ (cb (first exec))exec))(def updatable-evaldiff --git a/rad/monadic/issues.rad b/rad/monadic/issues.radindex 962dcdd..6486aad 100644--- a/rad/monadic/issues.rad+++ b/rad/monadic/issues.rad@@ -144,6 +144,6 @@[ (view (.. (@ 0) (@ :title)) is) ==> "title0" ][ (view (.. (@ 1) (@ :title)) is) ==> "title1" ][ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment1" ]- [ (short (head (values is))) ==> "0: title0" ]- [ (short (head (list-last 1))) ==> "1: title1" ]+ [ (short (first (values is))) ==> "0: title0" ]+ [ (short (first (list-last 1))) ==> "1: title1" ])diff --git a/rad/prelude/basic.rad b/rad/prelude/basic.radindex c454755..0c3191c 100644--- a/rad/prelude/basic.rad+++ b/rad/prelude/basic.rad@@ -1,9 +1,17 @@{:module 'prelude/basic:doc "Basic function used for checking equality, determining the type of a value, etc."- :exports '[or some empty-seq? length maybe->>= maybe-foldlM elem?]}+ :exports '[or some empty-seq? length maybe->>= maybe-foldlM elem? head tail]};; or+(def head+ "Backwards compatible alias for `first`."+ first)++(def tail+ "Backwards compatible alias for `rest`."+ rest)+(def or"Returns `x` if `x` is not `#f`, otherwise returns `y`"(fn [x y]diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.radindex 4249892..c537b46 100644--- a/rad/prelude/chain.rad+++ b/rad/prelude/chain.rad@@ -154,8 +154,8 @@(fn [expr env](if (list? expr)(cond- (eq? (head expr) :enter-chain)- (enter-remote-chain! (head (eval__ (nth 1 expr) env)) env)+ (eq? (first expr) :enter-chain)+ (enter-remote-chain! (first (eval__ (nth 1 expr) env)) env):else (eval__ expr env))(eval__ expr env))))@@ -165,7 +165,7 @@state, while also calling the callback on the result."(fn [state f arg cb](def exec (base-eval (list f (list 'quote arg)) state))- (cb (head exec))+ (cb (first exec))exec))(def updatable-evaldiff --git a/rad/prelude/dict.rad b/rad/prelude/dict.radindex c3c430b..bddf46a 100644--- a/rad/prelude/dict.rad+++ b/rad/prelude/dict.rad@@ -5,7 +5,7 @@(def dict-from-seq"Creates a dictionary from a list of key-value pairs."(fn [xs]- (foldr (fn [kv acc] (insert (head kv) (head (tail kv)) acc)) (dict) xs)))+ (foldr (fn [kv acc] (insert (first kv) (first (rest kv)) acc)) (dict) xs)))(:test "dict-from-seq"[ (dict-from-seq (list (list :one 1) (list :two 2)))@@ -16,7 +16,7 @@(def keys"Given a dict `d`, returns a vector of its keys."(fn [d]- (map head (seq d))))+ (map first (seq d))))(:test "keys"[ (keys {:one 1 :two 2}) ==> [:one :two] ]diff --git a/rad/prelude/io.rad b/rad/prelude/io.radindex 2af5f28..033e022 100644--- a/rad/prelude/io.rad+++ b/rad/prelude/io.rad@@ -40,7 +40,7 @@(def this-line (read-line-handle! hdl))(if (eq? this-line :eof)[]- (add-left this-line (read-contents-handle! hdl)))))+ (cons this-line (read-contents-handle! hdl)))))(def shell!"Executes `command` using the shell with `to-write` as input. Stdout and stderr are inherited.diff --git a/rad/prelude/lens.rad b/rad/prelude/lens.radindex 746d81e..89a051a 100644--- a/rad/prelude/lens.rad+++ b/rad/prelude/lens.rad@@ -96,7 +96,7 @@(make-lens(fn [s] (nth n s))(fn [val s]- (<> (take n s) (add-left val (drop (+ n 1) s)))))))+ (<> (take n s) (cons val (drop (+ n 1) s)))))))(:test "@nth"[ (view (@nth 1) [0 1]) ==> 1 ]diff --git a/rad/prelude/list.rad b/rad/prelude/list.radindex fd5db1a..0be4ced 100644--- a/rad/prelude/list.rad+++ b/rad/prelude/list.rad@@ -71,6 +71,8 @@:else (filter pred (tail ls)))))(:test "filter"- [ (filter (fn [x] (< x 10)) (list 3 10 11))- ==> (3) ]+ [ (filter (fn [x] (< x 10)) (list 3 10 11 5))+ ==> (3 5) ]+ [ (filter (fn [x] (< x 10)) [3 10 11 5])+ ==> [3 5] ])diff --git a/rad/prelude/patterns.rad b/rad/prelude/patterns.radindex 4784130..34b5a6d 100644--- a/rad/prelude/patterns.rad+++ b/rad/prelude/patterns.rad@@ -116,30 +116,32 @@[ (match :nothing (/just 'x) x _ :yes) ==> :yes ])(def /cons- "A pattern for lists with a head and a tail."+ "A pattern for sequences with a head and a tail."(fn [x-pat xs-pat](fn [v]- (if (list? v)+ (if (or (list? v) (vector? v))(if (> (length v) 0)(maybe->>=- (match-pat x-pat (head v))+ (match-pat x-pat (first v))(fn [b1](maybe->>=- (match-pat xs-pat (tail v))+ (match-pat xs-pat (rest v))(fn [b2] (non-linear-merge b1 b2))))):nothing):nothing))))(def /nil- "Empty-list pattern."+ "Empty-sequence pattern. Matches `[]` and `(list)`"(fn [v]- (if (eq? v (list))+ (if (empty-seq? v)[:just {}]:nothing)))-(:test "List pattern matching"+(:test "Sequence pattern matching"[ (match (list) /nil :yes _ :no) ==> :yes ]- [ (match (list 1 2 3) /nil :no (/cons 'x 'xs) [x xs]) ==> [1 (2 3)]])+ [ (match (list 1 2 3) /nil :no (/cons 'x 'xs) [x xs]) ==> [1 (2 3)]]+ [ (match [] /nil :yes _ :no) ==> :yes ]+ [ (match [1 2 3] /nil :no (/cons 'x 'xs) [x xs]) ==> [1 [2 3]] ])(def /?"Predicate pattern. Takes a predicate function as argument. Values match againstdiff --git a/rad/prelude/strings.rad b/rad/prelude/strings.radindex 9f7264f..5950092 100644--- a/rad/prelude/strings.rad+++ b/rad/prelude/strings.rad@@ -13,7 +13,7 @@""(foldlstring-append- (head strs)+ (first strs)(map (fn [ln] (string-append sep ln)) (tail strs))))))(:test "intercalate"diff --git a/rad/prelude/test-eval.rad b/rad/prelude/test-eval.radindex a995747..e311fb4 100644--- a/rad/prelude/test-eval.rad+++ b/rad/prelude/test-eval.rad@@ -15,7 +15,7 @@"Evaluation function that adds :test macro to register tests."(fn [expr env](if (list? expr)- (if (eq? (head expr) :test)+ (if (eq? (first expr) :test)(do(def bindings (lookup :env env))(def test-def {:tests (drop 2 expr) :name (nth 1 expr) :env env})diff --git a/rad/prelude/test.rad b/rad/prelude/test.radindex 26384d3..0481559 100644--- a/rad/prelude/test.rad+++ b/rad/prelude/test.rad@@ -40,7 +40,7 @@(def rhs (nth 2 single))(if (eq? marker '==>)(do- (def lhs-res (head (eval lhs senv)))+ (def lhs-res (first (eval lhs senv)))(if (eq? lhs-res rhs)(put-str! (string-append "Test '" test-name " (" (show ix) ")' succeeded"))(error (string-append "failed: got " (show lhs-res) " but expected " (show rhs)))))@@ -51,7 +51,7 @@;; If first block starts with :setup, run tests in that env(if (eq? (nth 0 maybe-setup) :setup)(do (def setup-env (nth 1 (eval (nth 1 maybe-setup) test-env)))- (foldl (fn [ix x] (check-single x ix setup-env)) 1 (tail test-contents)))+ (foldl (fn [ix x] (check-single x ix setup-env)) 1 (rest test-contents)))(foldl (fn [ix x] (check-single x ix test-env)) 1 test-contents))))diff --git a/reference-doc.yaml b/reference-doc.yamlindex 400d5bd..7dabc64 100644--- a/reference-doc.yaml+++ b/reference-doc.yaml@@ -37,9 +37,9 @@ primFns:# Pattern matching- match-pat# Sequences-- head- cons-- add-left+- first+- rest- add-right- <>- list@@ -51,7 +51,6 @@ primFns:- foldr- drop- sort-by-- tail- take- nth- seqdiff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hsindex f1f9e1e..e096ebd 100644--- a/src/Radicle/Internal/PrimFns.hs+++ b/src/Radicle/Internal/PrimFns.hs@@ -128,12 +128,6 @@ purePrimFns = fromList $ allDocs $xs -> throwErrorHere $ WrongNumberOfArgs "eq?" 2 (length xs))-- Vectors- , ( "add-left"- , "Adds an element to the left side of a vector."- , twoArg "add-left" $ \case- (x, Vec xs) -> pure $ Vec (x :<| xs)- (_, v) -> throwErrorHere $ TypeError "add-left" 1 TVec v- ), ( "add-right", "Adds an element to the right side of a vector.", twoArg "add-right" $ \case@@ -143,29 +137,30 @@ purePrimFns = fromList $ allDocs $-- Lists, ("cons"- , "Adds an element to the front of a list."- , \case- [x, List xs] -> pure $ List (x:xs)- [_, v] -> throwErrorHere $ TypeError "cons" 1 TList v- xs -> throwErrorHere $ WrongNumberOfArgs "cons" 2 (length xs))- , ("head"+ , "Adds an element to the front of a sequence."+ , twoArg "cons" $ \case+ (x, List xs) -> pure $ List (x:xs)+ (x, Vec xs) -> pure $ Vec (x Seq.:<| xs)+ (_, _) -> throwErrorHere $ OtherError "cons: second argument not a list or vector"+ )+ , ("first", "Retrieves the first element of a sequence if it exists. Otherwise throws an\\ exception."- , oneArg "head" $ \case+ , oneArg "first" $ \caseList (x:_) -> pure x- List [] -> throwErrorHere $ OtherError "head: empty list"+ List [] -> throwErrorHere $ OtherError "first: empty list"Vec (x Seq.:<| _) -> pure x- Vec Seq.Empty -> throwErrorHere $ OtherError "head: empty vector"- v -> throwErrorHere $ TypeError "head" 0 TSequence v)- , ("tail"+ Vec Seq.Empty -> throwErrorHere $ OtherError "first: empty vector"+ v -> throwErrorHere $ TypeError "first" 0 TSequence v)+ , ("rest", "Given a non-empty sequence, returns the sequence of all the elements but the\\ first. If the sequence is empty, throws an exception.", oneArg "tail" $ \caseList (_:xs) -> pure $ List xs- List [] -> throwErrorHere $ OtherError "tail: empty list"+ List [] -> throwErrorHere $ OtherError "rest: empty list"Vec (_ Seq.:<| xs) -> pure $ Vec xs- Vec Seq.Empty -> throwErrorHere $ OtherError "tail: empty vector"- v -> throwErrorHere $ TypeError "tail" 0 TSequence v)+ Vec Seq.Empty -> throwErrorHere $ OtherError "rest: empty vector"+ v -> throwErrorHere $ TypeError "rest" 0 TSequence v)-- Sequences: Lists and Vecs, ( "drop"diff --git a/test/spec/Radicle/Tests.hs b/test/spec/Radicle/Tests.hsindex a546316..1e2af98 100644--- a/test/spec/Radicle/Tests.hs+++ b/test/spec/Radicle/Tests.hs@@ -64,12 +64,12 @@ test_eval =let prog = [s|(cons #t (list #f))|]prog `succeedsWith` List [Boolean True, Boolean False]- , testCase "'head' returns the first element of a list" $ do- let prog = [s|(head (list #t #f))|]+ , testCase "'first' returns the first element of a list" $ do+ let prog = [s|(first (list #t #f))|]prog `succeedsWith` Boolean True- , testCase "'tail' returns the tail of a list" $ do- let prog = [s|(tail (list #t #f #t))|]+ , testCase "'rest' returns the tail of a list" $ do+ let prog = [s|(rest (list #t #f #t))|]prog `succeedsWith` List [Boolean False, Boolean True], testCase "'drop' drops" $ do@@ -198,18 +198,18 @@ test_eval =prog `succeedsWith` List [List [int 1, int 1]], testCase "'eval' evaluates the list" $ do- let prog = [s|(head (eval (quote #t) (get-current-env)))|]+ let prog = [s|(first (eval (quote #t) (get-current-env)))|]prog `succeedsWith` Boolean True, testCase "'eval' only evaluates the first quote" $ do- let prog1 = [s|(head (eval (quote (quote (+ 3 2))) (get-current-env)))|]+ let prog1 = [s|(first (eval (quote (quote (+ 3 2))) (get-current-env)))|]prog2 = [s|(quote (+ 3 2))|]res1 = runPureCode prog1res2 = runPureCode prog2res1 @?= res2, testProperty "'eval' does not alter functions" $ \(_v :: Value) -> do- let prog1 = [i| (head (eval (fn [] #{renderPrettyDef _v}) (get-current-env))) |]+ let prog1 = [i| (first (eval (fn [] #{renderPrettyDef _v}) (get-current-env))) |]prog2 = [i| (fn [] #{renderPrettyDef _v}) |]res1 = runPureCode $ toS prog1res2 = runPureCode $ toS prog2