Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std/misc/string: str #543

Merged
merged 1 commit into from
Jun 9, 2020
Merged

Conversation

thibran
Copy link

@thibran thibran commented Jun 7, 2020

Handy Clojure like procedure that converts a single thing into a string or appends multiple string into one.

> (str 1.0)
"1.0"

> (str "foo" "bar")
"foobar"

I tried to get the output as human friendly as possible by e.g. converting numbers from 2. to 2.0.

@thibran thibran force-pushed the misc/string/str branch 3 times, most recently from acbb347 to 1c0b2a5 Compare June 7, 2020 13:23
@thibran thibran force-pushed the misc/string/str branch 2 times, most recently from 6088847 to b493b70 Compare June 7, 2020 16:11
@thibran thibran force-pushed the misc/string/str branch from b493b70 to 0bbde00 Compare June 7, 2020 20:54
@fare
Copy link
Collaborator

fare commented Jun 8, 2020

What are the proposed use cases, compared to repr?

My proposed use cases are for constructing identifiers, etc., in which case I basically want to throw away all the structure, and concatenate the strings, symbols and numbers, without spaces.

(def (str . x) (str1 x))
(def (str1 x (port #f)) (if port (str2 x port) (call-with-output-string (cut str2 x <>))))
(def (str2 x port)
   (cond
    ((or (string? x) (symbol? x) (number? x) (keyword? x) (char? x)) (display x port))
    ((pair? x) (str2 (car x) port) (str2 (cdr x) port))
    ((vector? x) (vector-for-each (cut str2 <> port) x))
    ((member x `(() #f #t #!void #!eof)) (void))
    ((procedure? x) (x port))
    ((displayable? x) => (lambda (m) (m x port)))
    (else (display x port))))

(def (displayable? x)
  (and (object? x) (find-method (object-type x) ':display)))

@thibran thibran force-pushed the misc/string/str branch from 0bbde00 to 9edf8c9 Compare June 8, 2020 16:48
Copy link
Collaborator

@vyzo vyzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thibran
Copy link
Author

thibran commented Jun 8, 2020

@fare str It allows someone to convert a thing to a "human friendly" string without thinking much about it.

Copy link
Collaborator

@vyzo vyzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to merge imo.

@thibran thibran force-pushed the misc/string/str branch from 76c3186 to 6611134 Compare June 8, 2020 17:09
@thibran
Copy link
Author

thibran commented Jun 8, 2020

When I started this PR in never thought it would take sooo long.

@vyzo
Copy link
Collaborator

vyzo commented Jun 8, 2020

One of those things :)

@fare
Copy link
Collaborator

fare commented Jun 9, 2020

Frankly, this thing is enough of its own thing that I would move it out of std/misc/string into its own module.

@thibran
Copy link
Author

thibran commented Jun 9, 2020

@fare What would be the benefit to move it out of misc/string? As user I would have to type more (one more import) and as its own module I don't see much growth potentials, what else could be str like and could be added there? Clojure has str in core. But you are right that str is its own little snowflake.

@vyzo Would it be a good idea to check if all values are strings and just call string-concatenate and thereby skipping call-with-output-string when possible?

@vyzo
Copy link
Collaborator

vyzo commented Jun 9, 2020

Maybe, but how often do we pass a bunch of strings? We can string-append in that case.

@thibran
Copy link
Author

thibran commented Jun 9, 2020

@vyzo If someone just wants to get the job done he can go with str. From a use perspective str can "replace" string-append string-concatenate append-strings and handling custom format strings for basic use cases. It's like saying: "just give me a string for x". Very simple, but not as efficient as the other more specialized options.

@vyzo
Copy link
Collaborator

vyzo commented Jun 9, 2020

ok, let's make it more efficient then.

@thibran thibran force-pushed the misc/string/str branch from 6611134 to 67d4dbe Compare June 9, 2020 08:37
@vyzo
Copy link
Collaborator

vyzo commented Jun 9, 2020

ok, I am going to merge it.

@vyzo vyzo merged commit 9076d6a into mighty-gerbils:master Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants