In programming, there's time when we have 2 arrays and we want values at the same index of both arrays to have operation on each other producing an array of result. In imperative style, we have to setup a temporary counter variable to store index the current operation is on, increment it until reaching the last value of an array. For example, we want to sum values of each index.
In functional style, there's a function called 'zip' (Haskell, Ruby) which allows us to pair up values of each index preparing to apply operation on each pair later.
In Haskell, there's even 'zipWith' which apply operation on the pair immediately instead of producing an intermediate array.
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.
But as you can see, it doesn't preserve the order of an original vectors.
The next idea I have is to use 'interleave' and 'partition'
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'!
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.
Since the second argument can accept any function, creating Clojure records is as easy as this.
Subscribe to:
Post Comments (Atom)
Collectd PostgreSQL Plugin
I couldn't find this link when searching with google https://www.collectd.org/documentation/manpages/collectd.conf.html#plugin-postgresq...
-
sudo su postgres supply your Mac password modify /Library/PostgreSQL/9.x/data/pg_hba.conf from local all all md5 to local all all ...
-
Put your cursor on the text box of the post/comment you want to indent Go to the menubar at the top of the screen Edit > Emoji & ...
-
I ran to this error the other day when I tried to export a dump file from a slave Postgres database. cpg_dump: Dumping the contents of ta...
No comments:
Post a Comment