JanetDocsPlaygroundI'm feeling luckyGitHub sign in

map



    function
    boot.janet on line 921, column 1

    (map f & inds)

    Map a function over every value in a data structure and return an 
    array of the results.


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