Skip to content

A peer-to-peer stack for code collaboration

Add 'length' primitive

Thomas Scholtes2060102c9cc5
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index c4a778e..da09f59 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -168,6 +168,11 @@ Given a function ``f`` and a sequence (list or vector) ``xs``, returns a
sequence of the same size and type as ``xs`` but with ``f`` applied to
all the elements.
+``length``
+~~~~~~~~~~
+
+Returns the length of a vector, list, or string.
+
``foldl``
~~~~~~~~~
@@ -277,7 +282,7 @@ arguments isn't a string then an exception is thrown.
``string-length``
~~~~~~~~~~~~~~~~~
-Returns the length of a string.
+DEPRECATED Use ``length`` instead. Returns the length of a string.
``string-replace``
~~~~~~~~~~~~~~~~~~
@@ -566,10 +571,10 @@ Checks that there is a least one truthy value in a list.
Returns true if ``xs`` is an empty sequence (either list or vector).
-``(length xs)``
-~~~~~~~~~~~~~~~
+``length``
+~~~~~~~~~~
-Returns the length of ``xs``.
+Returns the length of a vector, list, or string.
``(maybe->>= v f)``
~~~~~~~~~~~~~~~~~~~
diff --git a/rad/monadic/issue-remote.rad b/rad/monadic/issue-remote.rad
index 4284387..be2dcec 100644
--- a/rad/monadic/issue-remote.rad
+++ b/rad/monadic/issue-remote.rad
@@ -59,7 +59,7 @@
(validator/and
[(validator/type :string)
(validator/pred (string-append "< " (show max-len) " chars in length")
- (fn [s] (< (string-length s) max-len)))])))
+ (fn [s] (< (length s) max-len)))])))
(def input-keys
{:nonce validator/nonce
diff --git a/rad/prelude/basic.rad b/rad/prelude/basic.rad
index bae328d..4a79f15 100644
--- a/rad/prelude/basic.rad
+++ b/rad/prelude/basic.rad
@@ -45,17 +45,15 @@
(or (eq? xs (list))
(eq? xs []))))
-;; length
-
-(def length
- "Returns the length of `xs`."
- (fn [xs]
- (foldr (fn [x acc] (+ acc 1)) 0 xs)))
-
+;; TODO move to file for primitive tests
(:test "length"
[ (length (list)) ==> 0]
[ (length (list 2)) ==> 1]
-)
+ [ (length []) ==> 0]
+ [ (length [1 2]) ==> 2]
+ [ (length "") ==> 0]
+ [ (length "abc") ==> 3]
+ )
(def maybe->>=
"Monadic bind for the maybe monad."
diff --git a/reference-doc.yaml b/reference-doc.yaml
index 4d0515c..7670026 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -47,6 +47,7 @@ primFns:
- vec-to-list
- zip
- map
+- length
- foldl
- foldr
- drop
diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hs
index 7a78a1e..bdb6c66 100644
--- a/src/Radicle/Internal/PrimFns.hs
+++ b/src/Radicle/Internal/PrimFns.hs
@@ -170,6 +170,14 @@ purePrimFns = fromList $ allDocs $
v -> throwErrorHere $ TypeError "rest" 0 TSequence v)
-- Sequences: Lists and Vecs
+ , ( "length"
+ , "Returns the length of a vector, list, or string."
+ , oneArg "length" $ \case
+ List xs -> pure . Number . fromIntegral . length $ xs
+ Vec xs -> pure . Number . fromIntegral . length $ xs
+ String s -> pure . Number . fromIntegral . T.length $ s
+ v -> throwErrorHere $ TypeError "length" 0 TSequence v
+ )
, ( "drop"
, "Returns all but the first `n` items of a sequence, unless the sequence is empty,\
\ in which case an exception is thrown."
@@ -297,7 +305,7 @@ purePrimFns = fromList $ allDocs $
)
, ( "string-length"
- , "Returns the length of a string."
+ , "DEPRECATED Use `length` instead. Returns the length of a string."
, oneArg "string-length" $ \case
String s -> pure . Number . fromIntegral . T.length $ s
v -> throwErrorHere $ TypeError "string-length" 0 TString v