Back

append! (clj)

(source)

function

(append! acc x)
.adds x to acc and returns acc

Examples

clojure/clojurescript
(ns cljs.reducers-test
  (:require [cljs.test :refer-macros [deftest is]]
            [clojure.core.reducers :as r]))

(deftest test-builtin-impls
  (is (= 0 (r/fold + nil)))
  (is (= [1 2 3 4] (seq (r/reduce r/append! (r/cat) [1 2 3 4]))))
  (is (= 10 (r/reduce + (array 1 2 3 4))))
  (is (= 11 (r/reduce + 1 (array 1 2 3 4))))
  (is (= 10 (r/reduce + (list 1 2 3 4))))
  (is (= 11 (r/reduce + 1 (list 1 2 3 4))))
  (is (= (r/fold + + [1 2 3])
             (r/fold + [1 2 3])
             (r/reduce + [1 2 3])
             6))
  (is (= (r/fold + + (vec (range 2048)))
             (r/reduce + (vec (range 2048)))))
  (letfn [(f [[ks vs] k v]
              [(conj ks k) (conj vs v)])
          (g ([] [#{} #{}])
             ([[ks1 vs1] [ks2 vs2]]
              [(into ks1 ks2) (into vs1 vs2)]))]
    (is (= (r/reduce f (g) {:a 1 :b 2 :c 3})
               (r/fold g f {:a 1 :b 2 :c 3})
               [#{:a :b :c} #{1 2 3}]))
    (let [m (into {} (for [x (range 2048)] [x (- x)]))]
      (is (= (r/reduce f (g) m) (r/fold g f m)))))
  ;; CLJS-792
  (is (= (into [] (r/map identity {})) [])))
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/fold r/cat r/append! input)) ; <1>
;; (1 2 3 4 5)
stabilized/clojurescript
(ns cljs.reducers-test
  (:require [cljs.test :refer-macros [deftest is]]
            [clojure.core.reducers :as r]))

(deftest test-builtin-impls
  (is (= 0 (r/fold + nil)))
  (is (= [1 2 3 4] (seq (r/reduce r/append! (r/cat) [1 2 3 4]))))
  (is (= 10 (r/reduce + (array 1 2 3 4))))
  (is (= 11 (r/reduce + 1 (array 1 2 3 4))))
  (is (= 10 (r/reduce + (list 1 2 3 4))))
  (is (= 11 (r/reduce + 1 (list 1 2 3 4))))
  (is (= (r/fold + + [1 2 3])
             (r/fold + [1 2 3])
             (r/reduce + [1 2 3])
             6))
  (is (= (r/fold + + (vec (range 2048)))
             (r/reduce + (vec (range 2048)))))
  (letfn [(f [[ks vs] k v]
              [(conj ks k) (conj vs v)])
          (g ([] [#{} #{}])
             ([[ks1 vs1] [ks2 vs2]]
              [(into ks1 ks2) (into vs1 vs2)]))]
    (is (= (r/reduce f (g) {:a 1 :b 2 :c 3})
               (r/fold g f {:a 1 :b 2 :c 3})
               [#{:a :b :c} #{1 2 3}]))
    (let [m (into {} (for [x (range 2048)] [x (- x)]))]
      (is (= (r/reduce f (g) m) (r/fold g f m)))))
  ;; CLJS-792
  (is (= (into [] (r/map identity {})) [])))
logaan/promise-stream
;; The promise-stream.pstream namespace contains the public API.
(ns promise-stream.walkthrough
  (:use [promise-stream.pstream :only
         [open-pstream closed-pstream append! promise close! fmap mapd* map*]]
        [jayq.core :only [done]]
        [jayq.util :only [log]])
  (:require [clojure.core.reducers :as r])
  (:use-macros [promise-stream.macros :only [for-pstream]]))

  ;; (append! writer (js/jQuery.get "/kittens"))

  (append! writer (promise "puppies"))

  (append! writer (promise "ducklings"))