vector-map(3scm)
π vector-map β bestie's guide to mapping over vectors π
okay so you found the vector-map(3scm) page in the Scheme Programmer's Manual and you're like "what is this beautiful little function" β don't worry, i got you. this is the whole thing, no skips, no cap. π«Ά
β¨ NAME β¨
vector-map β maps a procedure over the elements of vectors. that's it. that's the vibe. πΈ
π LIBRARY π
import it like this, depending on which scheme you're living in:
(import (rnrs)) ;R6RS
(import (rnrs base)) ;R6RS
(import (scheme base)) ;R7RS
π SYNOPSIS π
(vector-map proc vector1 vector2 ...)
π DESCRIPTION π
so vector-map applies proc element-wise to the elements of the vectors and returns a vector of the results, in order. literally the glow-up machine: vector in, vector out, each element gets the proc treatment. β¨
proc is always called in the same dynamic environment as vector-map itself. same vibe, same surroundings, no swaps. π¦
the order in which proc is applied to the elements of the vectors is unspecified. so don't go assuming anything about order, bestie, that's not the flex you think it is. π
if multiple returns occur from vector-map, the return values returned by earlier returns are not mutated. (ngl this is the kind of caveat you only care about when you're doing something unhinged with continuations, but it's here, so it's here. π)
R6RS β the vectors must all have the same length. all of them. no exceptions. π―
R7RS β if more than one vector is given and not all vectors have the same length, vector-map terminates when the shortest vector runs out. shortest one decides when the party's over. π
β RETURN VALUES β
returns a single value which is a vector object. one value. a vector. iconic. π
π EXAMPLES π
grab the second element of each pair, easy:
(vector-map cadr '#((a b) (d e) (g h)))
=> #(b e h)
exponentiate each number by itself, my beloved:
(vector-map (lambda (n) (expt n n))
'#(1 2 3 4 5))
=> #(1 4 27 256 3125)
now here's the messy one β different lengths, fresh drama:
(vector-map + '#(1 2 3) '#(4 5 6 7))
=> #(5 7 9) ; R7RS
raises &assertion ; R6RS
and the order-is-unspecified demo, watch the counter do its little dance:
(let ((count 0))
(vector-map
(lambda (ignored)
(set! count (+ count 1))
count)
'#(a b)))
=> #(1 2) or #(2 1)
either result is legit! that's the whole point! π«
π§ APPLICATION USAGE π§
this procedure is used in place of map(3scm) when vectors are used instead of lists. same energy, different container. π
π₯ COMPATIBILITY π₯
the R6RS version is consistent with the R6RS version of map(3scm) in that it requires all vectors to be the same length, and R7RS is likewise consistent with its version of map(3scm) in instead using the shortest vector. the two standards just have different opinions and honestly, both are valid. π
π ERRORS π
this procedure can raise exceptions with the following condition types:
&assertion(R6RS) β the wrong number of arguments was passed or an argument was outside its domain. likewise this is raised ifprocdoes not accept a single value, and may be raised if it does not return a single value. π- R7RS β the assertions described above are errors. additionally it is an error if
procdoes not return a single value. implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically. (yes. catastrophically. it says that. be careful out there. π)
π SEE ALSO π
map(3scm)string-map(3scm)vector-for-each(3scm)
π STANDARDS π
R6RS, R7RS, SRFI-43. all three, collectively stanning. π
π HISTORY π
the first RnRS report to carry this procedure was R6RS. R7RS also included it, basing it on the semantics from SRFI-43. a little origin story for your girl. β¨
π«Ά AUTHORS π«Ά
this page is part of the scheme-manpages project. it includes materials from the RnRS documents. more information can be found at https://github.com/schemedoc/manpages/.
and that's the whole page, bestie!! π this one's from 2022-03-29, part of the scheme-manpages project (source/version energy, you know how it is), and it lives at vector-map(3scm) in the Scheme Programmer's Manual. now go map something pretty. π
β¨
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2022 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms