so i have a vector of structs and i want to check if a name exists and return that index: (struct Person name age)
(define v (vector (Person "bob" 44) ("alice" 33) ("steven" 22))
(??? (lambda (x) (string=? (person-name x) "alice") v)) ; should return the index location of alice, which is 1
basically is there a built in vector-memq
that takes a function instead of the value to search?
a memf for vector
I don’t think vector-memf
exists.
actually memf doesn’t seem to return the index but the item itself :s
thanks! a little off putting that i have to vector->list
you don’t
that function will work on vectors
:open_mouth:
(index-of (vector 1 2 3) 2 =)
, i get index-of: contract violation expected: list? given: ’#(1 2 3) argument position: 1st other arguments…:
that’s a different index-of
Sam pointed you toward the one in the collections
package. The one you’re using is in racket/list
.
(for/first ([p (in-vector people)]
[i (in-naturals)]
#:when (equal? (Person-name p) "bob"))
i)
ahhhh
Just as I was typing this: (define (vector-index-of v p?)
(for/first ([x (in-vector v)]
[i (in-naturals)]
#:when (p? x))
i))
(vector-index-of #(1 3 5 8 9) even?)
thanks guys!
One thing: if you do decide to go with collections
, it tends to be a kind of all-in decision. It gives you a nice api for working with sequences of any kind, but it also produces lazy sequences, so if you try to mix-and-match functions from collections
and ones from the built-in library, you can run into problems. So that could be a reason to go with the approach that @samdphillips and @soegaard2 are proposing.
im surprised vector-index-of isn’t built in :s
I’d guess that people would be receptive to a PR against racket/vector
. (It should probably be called vector-memf
for consistency with the list libs.)
It’s in srfi/43:
(require srfi/43)
(vector-index even? #(1 3 5 8 9))
someone PR it pls :heart:
Would anyone like to help me test RacketCon streaming?
Thanks Michael, seemed fine?
Until the zoom feed froze/stopped, yes. Audio and video was fine for me.
so im writing s-expressions to a file and i don’t want the prefix of struct:
with the name of the struct. I just want (atlas (frame))
#(#(struct:atlas (frame (w . 13) (x . 1146) (y . 34) (h . 8)) (source (w . 13) (x . 14) (y . 9) (h . 8)) (id . 532737) (duration . -1)))
I’m reading the docs on how to customize display
but how to customize the reader but im unsure how to use them
and the hash that comes before it, #(struct:atlas)
. I think print
is closer to what i want but i dont need the single quotes or (vector )
Depending on your use-case you may just want to use #:prefab
structs. They read and write without hassle.
Is there a reason why struct
doesn’t allow each automatic field to have a different automatic value?
you mean default value?
@kellysmith12.21 default values are a basically useless feature that is only still around for compatibility
why are default values a useless feature :0?
ah, before i write it to a file i can just replace all the '
and keywords. My usecase is that im serializing the s-xpressions in rust using https://docs.rs/serde-lexpr/0.1.1/serde_lexpr/
a bit hacky but lol (define final (string-append "#" (string-replace (string-replace (get-output-string acc) "vector" "") "'" "")))
lengthy but it works
Because they are limited and you should use a ‘smart’ constructor instead, where you can can specify default values but also handle domains and such and report more meaningful errors