In the previous post https://statcompute.wordpress.com/2018/03/23/subset-by-values-in-clojure, it’s been demonstrated how to subset by value in Clojure. In the example below, I would show how to subset by index by using the keep-indexed function. The key is to use the keep-indexed function locating the position of each lookup value in the country list and then subset the order list by positions, e.g. index.
(require '[clojure.pprint :as p] '[clojure.java.jdbc :as j]) (def db {:classname "org.sqlite.JDBC" :subprotocol "sqlite" :subname "/home/liuwensui/Downloads/chinook.db"}) (def orders (j/query db "select billingcountry as country, count(*) as orders from invoices group by billingcountry;")) (def clist '("USA" "India" "Canada" "France") (def country (map #(get % :country) orders)) (p/print-table (map #(nth orders %) (flatten (pmap (fn [c] (keep-indexed (fn [i v] (if (= v c) i)) country)) clist)))) ;| :country | :orders | ;|----------+---------| ;| USA | 91 | ;| India | 13 | ;| Canada | 56 | ;| France | 35 |