This file contains 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
var a = [1,2,3,4]; | |
var b = [9,8,7,6]; | |
var c = [0,0,0,0]; | |
for (var i = 0; i < a.length; i++) { | |
c[i] = a[i] + b[i]; | |
} | |
c // => [10, 10, 10] |
This file contains 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
a = [1,2,3,4] | |
b = [9,8,7,6] | |
a.zip(b) # => [[1, 9], [2, 8], [3, 7], [4, 6]] | |
a.zip(b).map { |(x,y)| x + y } # => [10, 10, 10] |
But when it comes to Clojure, I wasn't be able to find a function in its standard library with the same behavior. The closest I could find is 'zipmap' that returns back a map which is not exactly what I want. We definitely can work around a little bit to get a vector.
This file contains 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
(zipmap [1 2 3] [9 8 7]) ;; => {3 7, 2 8, 1 9} | |
(vec (zipmap [1 2 3] [9 8 7])) ;; => [[3 7] [2 8] [1 9]] |
The next idea I have is to use 'interleave' and 'partition'
This file contains 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
(interleave [1 2 3] [9 8 7]) ;; => (1 9 2 8 3 7) | |
(partition 2 (interleave [1 2 3] [9 8 7])) ;; => ((1 9) (2 8) (3 7)) |
It's kinda work, but the fact that we have to call 2 functions is not so satisfied.
I went look up and found this Stack Overflow answer. Yes. Just use simple 'map'!
This file contains 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
(map vector [1 2 3] [9 8 7]) ;; => ([1 9] [2 8] [3 7]) | |
;; (vector 1 2 3) => [1 2 3] |
I think, I overlooked this solution because in Ruby, the language the I'm most comfortable with, 'map' can only operate on only one array.
Learning that Clojure's map can takes any number of collections is an aha moment for me. So now I can sum elements of more than 2 vectors easily.
This file contains 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
(map + [1 2 3] [9 8 7] [5 5 5]) ;; => (15 15 15) |
Since the second argument can accept any function, creating Clojure records is as easy as this.
This file contains 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
(defrecord Coordinate [x y]) | |
(map ->Coordinate [1 2 3] [9 8 7]) | |
;; => (#user.Coordinate{:x 1, :y 9} #user.Coordinate{:x 2, :y 8} #user.Coordinate{:x 3, :y 7}) |
No comments:
Post a Comment