JanetDocsSourcePlaygroundTutorialsI'm Feeling luckyCommunityGitHub sign in

map

core-api


    function
    boot.janet on line 1210, column 1

    (map f x & xs)

    Map a function `f` over every value in `x` and return an array of 
    results.

    Also functions variadically, i.e. takes additional arguments, with 
    suitable changes to the arity of `f`. `f` is called with both 
    values from `x` and additional arguments, and only as many times as 
    the length of the shortest of `x` and each element of `xs`. If `x` 
    or any element of `xs` is empty, returns an empty array.

    `x` and elements of `xs` can be values of type bytes, indexed, 
    dictionary, fiber, or abstract with suitable `get` and `next` 
    methods.


7 examplesSign in to add an example
Loading...
(->> (range 10) (map (fn [arg] (* arg arg))))

# => @[0 1 4 9 16 25 36 49 64 81]
Geo-7Playground
(map (fn [arg] (* arg arg)) (range 10))

# => @[0 1 4 9 16 25 36 49 64 81]
Geo-7Playground
(map tuple ["a" "b" "c" "d"] [1 2 3 4] "abcd")

# => @[("a" 1 97) ("b" 2 98) ("c" 3 99) ("d" 4 100)]
felixrPlayground
# Map over multiple structures -- Stops after the shortest is exhausted.
(map (fn [& args] ;args) [1] [1 2] [1 2 3])

# => @[(1 1 1)]
GrazfatherPlayground
# build own map-indexed with map

(defn map-indexed [f ds]
    (map f (range 0 (length ds)) ds))

(map-indexed (fn [i v] [i v] ) ["a" "b" "c" "d"])

# => @[(0 "a") (1 "b") (2 "c") (3 "d")]
leobmPlayground
(map |(+ $0 $1) [1 2 3] [4 5 6]) # @[5 7 9] - uses the fn shorthand
ahungryPlayground
(map string/from-bytes "Hello, world!")  # => @["H" "e" "l" "l" "o" "," " " "w" "o" "r" "l" "d" "!"]
GrayJackPlayground