Last active
August 29, 2015 14:08
-
-
Save seanhess/b58b009af26365807380 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- https://stackoverflow.com/questions/1651351/clojure-call-a-function-for-each-element-in-a-vector-with-it-index/1651736#1651736 | |
-- (map #(setCell 0 %1 %2) (iterate inc 0) data) | |
words = ["Hello", "World", "Test", "This"] | |
-- here's a direct translation | |
setCells words = map (\(i, word) -> setCell 0 i word) $ zip [1..] words | |
-- here are some different ways you could write it | |
setCells = map toCell . zip [1..] | |
where | |
toCell (i, word) = setCell 0 i word | |
setCells = map (uncurry (setCell 0)) . zip [1..] | |
-- let's make a map that works like Clojure for 2 lists | |
-- I'm sure this exists somewhere on hackage | |
map2 f xs ys = map (uncurry f) (zip xs ys) | |
-- then it gets simpler | |
setCells words = map2 (setCells 0) [1..] words |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment