magnars/s.el

feature request: add function inverse to `string-to-list`.

Fuco1 opened this issue · 13 comments

Add a function inverse to string-to-list.

Currently, one must do (apply 'string list) which is less than ideal in case we want to map or compose the (proposed, non existing) list-to-string function.

Could be called s-list-to-string or just s-to-string.

Awesome task for beginners :) !!!

Apparently concat does this... it might still warrant an alias because concat is totally undiscoverable when one wants to create a string from list of characters.

s.el doesn't have s-string-to-list or s-to-list...

Am I missing something? Isn't this s-split ?

Fuco1 commented

I wanted a function doing this:

(string-to-list "hello")       ;; => (104 101 108 108 111)
;; inverse function
(concat (list ?h ?e ?l ?l ?o)) ;; => "hello"

Let's call it something more specific, how about s-to-chars or s-to-charlist ... something like that.

For me personally s-to-chars hits brevity and specificity on the nail, and gets my vote.

(defun s-to-chars (s)
 "split S into a list of chars."
 (mapcar 
   (lambda (it) (string-to-char it)) 
   (s-split "" s))

something like that

Or a bit tighter.

(defun s-to-chars (s)
 "split S into a list of chars."
 (mapcar 'string-to-char (s-split "" s))
(defexamples s-to-chars ()
  "Test splitting a string into a list of chars."
  (s-chars "hellloooo") => (0 104 101 108 108 108 111 111 111 111 0))

Ok, will have to strip those nils.

The inverse...

(s-from-chars (chars) ... 
;; or
(s-to-string (chars) ... 

The second one works best for me, the signature is telegraphing the source type. It's clear.

(defun s-to-string (chars)
  "Convert a list of CHARS to a string."
  (concat chars))
(defexamples s-to-string ()
  "Test char list to string conversion."
  (s-to-string '(104 101 108 108 111)) => "hello")

No nil problems this way around.

I can integrate these tomorrow.

Fuco1 commented

string-to-list is a built-in which does this. You don't need to implement it.

This issue was only to provide an s api to that and the inverse function, which is also built in (concat).

You don't need to implement it.

The inverse is for symmetric integrity of the API (towards the goal of discoverable functionality.)

Is a PR coming?

Closing