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

add for/string and for*/string #2555

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

AlexKnauth
Copy link
Member

The for/string and for*/string forms are a lot like for/vector and for*/vector, so I use the same common helper function for all four macro definitions.

@rfindler
Copy link
Member

rfindler commented Mar 25, 2019 via email

@AlexKnauth
Copy link
Member Author

Thanks, fixed now.

@mflatt
Copy link
Member

mflatt commented Mar 25, 2019

Would this be better in racket/string? (I think we included too much in racket/base in the first place.)

@rfindler
Copy link
Member

rfindler commented Mar 25, 2019 via email

@pmatos
Copy link
Collaborator

pmatos commented Mar 25, 2019

And docs?

@rfindler
Copy link
Member

rfindler commented Mar 25, 2019 via email

@gus-massa
Copy link
Contributor

May I ask for for[*]/bytes too? I like to maintain the symmetry of the universe.

I actually needed for/bytes once to implement a no so secure version of crypto-random-bytes:

(define (random-bytes n)
  (list->bytes
   (for/list ([i (in-range n)])
     (random 256))))

@LeifAndersen
Copy link
Member

At this point I can't help but wonder if something like define-for-macro is in order...

@AlexKnauth
Copy link
Member Author

AlexKnauth commented Mar 25, 2019

for/vector, for/string, for/bytes, for/fxvector, and for/flvector would all be very similar, since vectors, strings, and bytes are all "fixed-length-array" style data structures. All of them could share code, but what works for them wouldn't work as well for other data structures like lists, streams, or hash-tables.

@mfelleisen
Copy link
Collaborator

Why don't we have

(for {sum|vector|string|bytes|...} ((i e)) e0 ...)

and even an expression in this position? For efficiency I am sure but when did that hold us back in the past?

@mflatt
Copy link
Member

mflatt commented Mar 25, 2019

In suggesting racket/string, I really meant to avoid adding code to "for.rkt", and not just avoiding bindings. Sorry for being unclear.

Maybe "racket/private/vector-wraps.rkt" is useful. Its macro would define a string-copy! that isn't needed, though, and maybe other useless things.

@AlexKnauth
Copy link
Member Author

AlexKnauth commented Mar 25, 2019

All the logic for for/string and for/bytes is already in the compile-time helper function for_/vector though. It would be a shame not to reuse that code.

(Currently its already duplicated, in both "racket/private/for.rkt" and "racket/private/vector-wraps.rkt")

@soegaard
Copy link
Member

soegaard commented Mar 25, 2019 via email

@jackfirth
Copy link
Sponsor Contributor

jackfirth commented Sep 13, 2019

Why don't we have

(for {sum|vector|string|bytes|...} ((i e)) e0 ...)

and even an expression in this position? For efficiency I am sure but when did that hold us back in the past?

@mfelleisen Following up on this for posterity, this concept is now implemented in the reducers library I wrote:

> (for/reducer into-string
               ([ch "hello world"] [_ (in-range 5)])
    (char-upcase ch))
"HELLO"

There's a for*/reducer macro too.

@shhyou shhyou added the api design Related to the design of core libraries and language features label Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api design Related to the design of core libraries and language features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants