Public Vars

Back

repeatedly (clj)

(source)

function

(repeatedly f) (repeatedly n f)
Takes a function of no args, presumably with side effects, and returns an infinite (or length n if supplied) lazy sequence of calls to it

Examples

clojure/core.async
;; The clojure.core.async namespace contains the public API.
(require '[clojure.core.async :as async :refer :all])

(let [n 1000
      cs (repeatedly n chan)
      begin (System/currentTimeMillis)]
  (doseq [c cs] (go (>! c "hi")))
  (dotimes [i n]
    (let [[v c] (alts!! cs)]
      (assert (= "hi" v))))
  (println "Read" n "msgs in" (- (System/currentTimeMillis) begin) "ms"))
hoplon/hoplon
(ns hoplon.binding
  (:refer-clojure :exclude [binding bound-fn])
  (:require [clojure.core  :as clj]
            [cljs.analyzer :as a]))

(defmacro binding
  "See clojure.core/binding."
  [bindings & body]
  (let [env           (assoc &env :ns (a/get-namespace a/*cljs-ns*))
        value-exprs   (take-nth 2 (rest bindings))
        bind-syms     (map #(:name (a/resolve-existing-var env %)) (take-nth 2 bindings))
        bind-syms'    (map (partial list 'quote) bind-syms)
        set-syms      (repeatedly (count bind-syms) gensym)
        setfn         (fn [x y]
                        {:push! `(fn []
                                   (let [z# ~x]
                                     (set! ~x ~y)
                                     (fn [] (set! ~x z#))))})
        thunkmaps     (map setfn bind-syms set-syms)]
    (a/confirm-bindings env bind-syms)
    `(let [~@(interleave set-syms value-exprs)]
       (hoplon.binding/push-thread-bindings ~(zipmap bind-syms' thunkmaps))
       (try ~@body (finally (hoplon.binding/pop-thread-bindings))))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
  "Type annotations for the base Clojure distribution."
  #?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
  (:require [clojure.core :as cc]
            [typed.clojure :as t]
            #?(:clj [typed.ann-macros.clojure :as macros])
            #?(:clj typed.ann.clojure.jvm) ;; jvm annotations
            #?(:clj clojure.core.typed))
  #?(:clj
     (:import (clojure.lang PersistentHashSet PersistentList
                            APersistentMap #_IPersistentCollection
                            #_ITransientSet
                            IRef)
              (java.util Comparator Collection))))

cc/repeatedly (t/All [x] (t/IFn [[:-> x] :-> (t/NonEmptyASeq x)]
                                [t/Int [:-> x] :-> (t/ASeq x)]))
rufoa/try-let
(ns try-let
	(:require
		[clojure.spec.alpha :as s]
		[clojure.core.specs.alpha :as cs]))


(defmacro try-let
	[bindings & body]
	(let [[_ {:keys [thens catches finally]}] (s/conform ::try-let-body body)
	      bindings-destructured (destructure bindings)
	      bindings-ls (take-nth 2 bindings-destructured)
	      gensyms (take (count bindings-ls) (repeatedly gensym))]
		`(let [[ok# ~@gensyms]
				(try
					(let [~@bindings-destructured] [true ~@bindings-ls])
					~@(map
						(fn [stanza]
							(let [[x y z & body] stanza]
								`(~x ~y ~z [false (do ~@body)])))
						catches)
					~@(when finally [finally]))]
			(if ok#
				(let [~@(interleave bindings-ls gensyms)]
					~@thens)
				~(first gensyms)))))

(defmacro try+-let
	[bindings & body]
	(let [[_ {:keys [thens catches else finally]}] (s/conform ::try+-let-body body)
	      bindings-destructured (destructure bindings)
	      bindings-ls (take-nth 2 bindings-destructured)
	      gensyms (take (count bindings-ls) (repeatedly gensym))]
		`(let [[ok# ~@gensyms]
				(slingshot.slingshot/try+
					(let [~@bindings-destructured] [true ~@bindings-ls])
					~@(map
						(fn [stanza]
							(let [[x y z & body] stanza]
								`(~x ~y ~z [false (do ~@body)])))
						catches)
					~@(when else [else])
					~@(when finally [finally]))]
			(if ok#
				(let [~@(interleave bindings-ls gensyms)]
					~@thens)
				~(first gensyms)))))
metosin/clojure-bootcamp
(ns twitter-sphere.play-ground
  (:require [clojure.core.async
                :as async
                :refer [chan go >!! >! <!! <!
                        alt! alts! alt!! alts!!]]))

; create a lot of channels:
(def channels (repeatedly 1e6 chan))