Back
foldcat (clj)
(source)function
(foldcat coll)
Equivalent to (fold cat append! coll)
Examples
seancorfield/next-jdbc
(ns next.jdbc-test
"Basic tests for the primary API of `next.jdbc`."
(:require [clojure.core.reducers :as r]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[next.jdbc :as jdbc]
[next.jdbc.connection :as c]
[next.jdbc.test-fixtures
:refer [with-test-db db ds column
default-options stored-proc?
derby? hsqldb? jtds? mssql? mysql? postgres? sqlite?]]
[next.jdbc.prepare :as prep]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs]
[next.jdbc.types :as types])
(:import (com.zaxxer.hikari HikariDataSource)
(com.mchange.v2.c3p0 ComboPooledDataSource PooledDataSource)
(java.sql ResultSet ResultSetMetaData)))
(deftest folding-test
(jdbc/execute-one! (ds) ["delete from fruit"])
(with-open [con (jdbc/get-connection (ds))
ps (jdbc/prepare con ["insert into fruit(name) values (?)"])]
(jdbc/execute-batch! ps (mapv #(vector (str "Fruit-" %)) (range 1 1001))))
(testing "foldable result set"
(testing "from a Connection"
(let [result
(with-open [con (jdbc/get-connection (ds))]
(r/foldcat
(r/map (column :FRUIT/NAME)
(jdbc/plan con ["select * from fruit order by id"]
(default-options)))))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))
(testing "from a DataSource"
(doseq [n [2 3 4 5 100 300 500 700 900 1000 1100]]
(testing (str "folding with n = " n)
(let [result
(try
(r/fold n r/cat r/append!
(r/map (column :FRUIT/NAME)
(jdbc/plan (ds) ["select * from fruit order by id"]
(default-options))))
(catch java.util.concurrent.RejectedExecutionException _
[]))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))))
(testing "from a PreparedStatement"
(let [result
(with-open [con (jdbc/get-connection (ds))
stmt (jdbc/prepare con
["select * from fruit order by id"]
(default-options))]
(r/foldcat
(r/map (column :FRUIT/NAME)
(jdbc/plan stmt nil (default-options)))))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))
(testing "from a Statement"
(let [result
(with-open [con (jdbc/get-connection (ds))
stmt (prep/statement con (default-options))]
(r/foldcat
(r/map (column :FRUIT/NAME)
(jdbc/plan stmt ["select * from fruit order by id"]
(default-options)))))]
(is (= 1000 (count result)))
(is (= "Fruit-1" (first result)))
(is (= "Fruit-1000" (last result)))))))
reborg/clojure-essential-reference
(require '[clojure.core.reducers :as r])
(def input (r/map inc (into [] (range 1000))))
(take 5 (r/foldcat input)) ; <2>
;; (1 2 3 4 5)
reborg/clojure-essential-reference
(require '[clojure.core.reducers :as r])
(require '[clojure.string :as s])
(def words (r/foldcat (r-word text))) ; <3>