f2f853a0d29e

Add string-join
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 23 Nov 2018 10:55:35 -0500
parents edbdc9c9cb0a
children 73c14a96cb0e
branches/tags (none)
files losh.asd package.lisp src/sequences.lisp

Changes

--- a/losh.asd	Sat Nov 10 19:35:46 2018 -0500
+++ b/losh.asd	Fri Nov 23 10:55:35 2018 -0500
@@ -47,6 +47,7 @@
                                               "chili-dogs"))
                  (:file "sequences" :depends-on ("chili-dogs"
                                                  "hash-tables"
+                                                 "functions"
                                                  "mutation"))
                  (:file "debugging" :depends-on ("control-flow"
                                                  "math"
--- a/package.lisp	Sat Nov 10 19:35:46 2018 -0500
+++ b/package.lisp	Fri Nov 23 10:55:35 2018 -0500
@@ -253,6 +253,7 @@
 (defpackage :losh.sequences
   (:use :cl :iterate :losh.quickutils
     :losh.chili-dogs
+    :losh.functions
     :losh.hash-tables
     :losh.iterate-pre
     :losh.mutation)
@@ -270,7 +271,8 @@
     :drop-while
     :summation
     :product
-    :doseq))
+    :doseq
+    :string-join))
 
 (defpackage :losh.debugging
   (:use :cl :iterate :losh.quickutils
--- a/src/sequences.lisp	Sat Nov 10 19:35:46 2018 -0500
+++ b/src/sequences.lisp	Fri Nov 23 10:55:35 2018 -0500
@@ -355,3 +355,18 @@
     result))
 
 
+(defun string-join (separator sequence)
+  "Join a `sequence` of objects into a string, separated by `separator`.
+
+  All objects in `sequence` (and `separator`) will be `princ-to-string`ed before
+  joining.
+
+  This is implemented simply, not efficiently, so consider implementing your own
+  if you're joining a lot of stuff.
+
+  "
+  (unless (stringp separator)
+    (callf separator #'princ-to-string))
+  (flet ((concat (current next)
+           (concatenate 'string current separator next)))
+    (reduce (nullary #'concat "") sequence :key #'princ-to-string)))