A peer-to-peer stack for code collaboration
Add '/prefix' matcher
Thomas Scholtes60f4adf22c9b
diff --git a/docs/source/reference.rst b/docs/source/reference.rstindex 6f84fa2..c3e70b7 100644--- a/docs/source/reference.rst+++ b/docs/source/reference.rst@@ -917,6 +917,12 @@ Returns all elements of a sequence ``ls`` until one does not satisfyReturns ``#t`` if ``prefix`` is a prefix of the sequence ``s``. Alsoworks for strings+``(/prefix prefix rest-pat)``+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~++Matches sequences that start with ``prefix`` and bind the rest of that+sequence to ``rest-pat``. Also works for strings.+``prelude/set``---------------diff --git a/rad/prelude/seq.rad b/rad/prelude/seq.radindex 46ca0e1..03c8861 100644--- a/rad/prelude/seq.rad+++ b/rad/prelude/seq.rad@@ -1,6 +1,6 @@{:module 'prelude/seq:doc "Functions for manipulating sequences, that is lists and vectors."- :exports '[empty? seq? reverse filter take-while starts-with?+ :exports '[empty? seq? reverse filter take-while starts-with? /prefix]}(import prelude/basic :unqualified)@@ -100,3 +100,47 @@[ (starts-with? [] []) ==> #t ][ (starts-with? [] [1]) ==> #f ])++(def /prefix+ "Matches sequences that start with `prefix` and bind the rest of that+ sequence to `rest-pat`. Also works for strings."+ (fn [prefix rest-pat]+ (fn [value]+ (if (or (string? value) (seq? value))+ (if (starts-with? value prefix)+ (match-pat rest-pat (drop (length prefix) value))+ :nothing)+ :nothing)+ )))++(:test "/prefix"+ [ (match [0 2 3]+ (/prefix [0 2] 'rs) rs+ )+ ==> [3] ]+ [ (match '(1 2 3)+ (/prefix '(1 2) 'rs) rs+ )+ ==> (3) ]+ [ (match "abcd"+ (/prefix "ab" 'rs) rs+ )+ ==> "cd" ]+ ;; nested pattern+ [ (match [1 2 3]+ (/prefix [1 2] (/as 'rs _)) rs+ )+ ==> [3] ]+ ;; fail on keyword+ [ (match :foo+ (/prefix [] _) #f+ _ :fall-through+ )+ ==> :fall-through ]+ ;; fail if type differs+ [ (match '()+ (/prefix [] _) #f+ _ :fall-through+ )+ ==> :fall-through ]+ )