coll.clj 466 B

12345678910111213141516171819202122232425
  1. (ns fira-code.coll)
  2. (defn index-of [pred xs]
  3. (let [res (reduce (fn [i x] (if (pred x) (reduced i) (inc i))) 0 xs)]
  4. (if (>= res (count xs))
  5. -1
  6. res)))
  7. (defn group-by-to [key-fn value-fn xs]
  8. (reduce-kv
  9. (fn [m k vs]
  10. (assoc m k (value-fn vs)))
  11. {}
  12. (group-by key-fn xs)))
  13. (defn multimap-by [f & kvs]
  14. (reduce
  15. (fn [m [k v]]
  16. (if (contains? m k)
  17. (update m k f v)
  18. (assoc m k v)))
  19. {} (partition 2 kvs)))