Back
letfn (clj)
(source)macro
(letfn fnspecs & body)
fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)
Takes a vector of function specs and a body, and generates a set of
bindings of functions to their names. All of the names are available
in all of the definitions of the functions, as well as the body.
Examples
metabase/metabase
(ns metabase.async.util-test
(:require
[clojure.core.async :as a]
[clojure.test :refer :all]
[metabase.async.util :as async.u]
[metabase.test.util.async :as tu.async]))
(testing "We should be able to combine the `promise-pipe` and `cancelable-thread` and get results"
(letfn [(f []
(Thread/sleep 10)
::success)]
(tu.async/with-open-channels [result-chan (a/promise-chan)]
(async.u/promise-pipe (async.u/cancelable-thread-call f) result-chan)
(is (= ::success
(first (a/alts!! [result-chan (a/timeout 500)]))))))))
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 {})) [])))
typedclojure/typedclojure
(ns clojure.core.typed.test.tools-analyzer-jvm-ann
(:require [clojure.core.typed :as t]))
(t/defalias Expr
(t/Rec [Expr]
; ----- start tools.analyzer specific ----
(t/U (t/HMap :mandatory
{:op ':const
:env Env
:type t/Keyword
:literal Boolean
:val Form
:form Form}
:optional
{:meta Expr
:children Children})
(t/HMap :mandatory
{:op ':vector
:env Env
:items (t/Vec Expr)
:form Form
:children Children})
(t/HMap :mandatory
{:op ':with-meta
:env Env
:form Form
:meta Expr
:expr Expr
:children Children})
(t/HMap :mandatory
{:op ':map
:env Env
:keys (t/Vec Expr)
:vals (t/Vec Expr)
:form Form
:children Children})
(t/HMap :mandatory
{:op ':set
:env Env
:items (t/Vec Expr)
:form Form
:children Children})
(t/HMap :mandatory
{:op ':local
:form Form
:name t/Symbol
:assignable? Boolean
:children Children}
:optional
;env is sometimes omitted. eg. :local entry in :fn-method
{:env Env})
(t/HMap :mandatory
{:op ':var
:env Env
:form Form
:assignable? Boolean
;:var t/AnyVar
})
(t/HMap :mandatory
{:op ':maybe-host-form
:env Env
:form Form
:class (t/U nil t/Symbol)
:field t/Symbol})
(t/HMap :mandatory
{:op ':maybe-class
:env Env
:form Form
; is this just Symbol?
:class (t/U nil t/Symbol)})
(t/HMap :mandatory
{:op ':do
:env Env
:form Form
:statements (t/Vec Expr)
:ret Expr
:children Children})
(t/HMap :mandatory
{:op ':if
:env Env
:form Form
:test Expr
:then Expr
:else Expr
:children Children})
(t/HMap :mandatory
{:op ':new
:env Env
:form Form
:class Class
:args (t/Vec Expr)
:children Children})
(t/HMap :mandatory
{:op ':quote
:env Env
:form Form
:expr Expr
:literal? Boolean
:children Children})
(t/HMap :mandatory
{:op ':set!
:env Env
:form Form
:target Expr
:val Expr
:children Children})
(t/HMap :mandatory
{:op ':try
:env Env
:form Form
:body Expr
:catches (t/Vec Expr)
:children Children}
:optional
{:finally Expr})
(t/HMap :mandatory
{:op ':binding
:env Env
:form Form
:name t/Symbol
:local t/Any}
:optional
{:tag Tag
:arg-id t/Int
:variadic? Boolean
;---- start t.a.j specific ----
; not sure what o-tag is
:o-tag OTag
; added in :deftype
:mutable t/Any
;---- end t.a.j specific ----
})
(t/HMap :mandatory
{:op ':catch
:env Env
:form Form
:class Class
:local Expr
:body Expr
:children Children})
(t/HMap :mandatory
{:op ':throw
:env Env
:form Form
:exception Expr})
(t/HMap :mandatory
{:op ':letfn
:env Env
:form Form
:body Expr
:bindings (t/Vec Expr)
:children Children})
(t/HMap :mandatory
{:op ':let
:form Form
:env Env
:body Expr
:bindings (t/Vec Expr)
:children Children})
(t/HMap :mandatory
{:op ':loop
:form Form
:env Env
:loop-id t/Symbol
:body Expr
:bindings (t/Vec Expr)
:children Children})
(t/HMap :mandatory
{:op ':recur
:env Env
:form Form
:exprs (t/Vec Expr)
:loop-id t/Symbol
:children Children})
(t/HMap :mandatory
{:op ':fn-method
:env Env
:form Form
:loop-id t/Symbol
:variadic? Boolean
:params (t/Vec Expr)
:fixed-arity t/Int
:body Expr
:children Children}
:optional
{:local Expr})
(t/HMap :mandatory
{:op ':fn
:env Env
:form Form
;unsure if nilable
:name (t/U nil t/Symbol)
:variadic? Boolean
:max-fixed-arity (t/U nil t/Int)
:methods (t/Vec Expr)
:children Children}
:optional
{:local Expr})
(t/HMap :mandatory
{:op ':def
:env Env
:form Form
:name t/Symbol
;:var t/AnyVar
}
:optional
{:meta Expr
:init Expr
:doc String
:children Children})
(t/HMap :mandatory
{:op ':host-call
:env Env
:form Form
:target Expr
:method t/Symbol
:args (t/Vec Expr)
:children Children})
(t/HMap :mandatory
{:op ':host-field
:env Env
:form Form
:target Expr
:field t/Symbol
:children Children})
(t/HMap :mandatory
{:op ':host-interop
:env Env
:form Form
:target Expr
:m-or-f t/Symbol
:children Children})
(t/HMap :mandatory
{:op ':invoke
:env Env
:form Form
:fn Expr
:args (t/Vec Expr)
:children Children}
:optional
{:meta Expr})
; ---- end tools.analyzer specific -----
; ---- start tools.analyzer.jvm specific ----
typedclojure/typedclojure
(ns ^:no-doc typed.cljc.checker.check-impl
(:require [clojure.core.typed.current-impl :as impl]
[typed.cljc.analyzer :as ana2]
[typed.cljc.checker.check.binding :as binding]
[typed.cljc.checker.check.catch :as catch]
[typed.cljc.checker.check.const :as const]
[typed.cljc.checker.check.do :as do]
[typed.cljc.checker.check.fn :as fn]
[typed.cljc.checker.check.if :as if]
[typed.cljc.checker.check.invoke :as invoke]
[typed.cljc.checker.check.let :as let]
[typed.cljc.checker.check.letfn :as letfn]
[typed.cljc.checker.check.local :as local]
[typed.cljc.checker.check.loop :as loop]
[typed.cljc.checker.check.map :as map]
[typed.cljc.checker.check.quote :as quote]
[typed.cljc.checker.check.recur :as recur]
[typed.cljc.checker.check.set :as set]
[typed.cljc.checker.check.set-bang :as set!]
[typed.cljc.checker.check.throw :as throw]
[typed.cljc.checker.check.try :as try]
[typed.cljc.checker.check.vector :as vec]
[typed.cljc.checker.check.with-meta :as with-meta]))
(defmethod -check ::ana2/binding [expr expected] (binding/check-binding expr expected))
(defmethod -check ::ana2/catch [expr expected] (catch/check-catch expr expected))
(defmethod -check ::ana2/const [expr expected] (const/check-const expr expected))
(defmethod -check ::ana2/do [expr expected] (do/check-do expr expected))
(defmethod -check ::ana2/fn [expr expected] (fn/check-fn expr expected))
(defmethod -check ::ana2/if [expr expected] (if/check-if expr expected))
(defmethod -check ::ana2/invoke [expr expected] (invoke/check-invoke expr expected))
(defmethod -check ::ana2/let [expr expected] (let/check-let expr expected))
(defmethod -check ::ana2/letfn [expr expected] (letfn/check-letfn expr expected))
(defmethod -check ::ana2/local [expr expected] (local/check-local expr expected))
(defmethod -check ::ana2/loop [expr expected] (loop/check-loop expr expected))
(defmethod -check ::ana2/map [expr expected] (map/check-map expr expected))
(defmethod -check ::ana2/quote [expr expected] (quote/check-quote expr expected))
(defmethod -check ::ana2/recur [expr expected] (recur/check-recur expr expected))
(defmethod -check ::ana2/set [expr expected] (set/check-set expr expected))
(defmethod -check ::ana2/set! [expr expected] (set!/check-set! expr expected))
(defmethod -check ::ana2/throw [expr expected] (throw/check-throw expr expected))
(defmethod -check ::ana2/try [expr expected] (try/check-try expr expected))
(defmethod -check ::ana2/vector [expr expected] (vec/check-vector expr expected))
(defmethod -check ::ana2/with-meta [expr expected] (with-meta/check-with-meta expr expected))
funcool/catacumba
(ns catacumba.tests.test-core
(:require [clojure.core.async :as a]
[clojure.test :refer :all]
[clojure.java.io :as io]
[clojure.pprint :refer [pprint]]
[beicon.core :as rx]
[clj-http.client :as client]
[promesa.core :as p]
[cuerdas.core :as str]
[manifold.stream :as ms]
[manifold.deferred :as md]
[catacumba.core :as ct]
[catacumba.http :as http]
[catacumba.testing :refer [with-server]]
[catacumba.tests.helpers :as th]
[catacumba.handlers.misc])
(:import ratpack.exec.Execution
ratpack.func.Action
ratpack.func.Block
ratpack.exec.ExecInterceptor
ratpack.exec.ExecInterceptor$ExecType
java.io.ByteArrayInputStream))
(deftest cookies-test
(testing "Setting new cookie."
(letfn [(handler [context]
(ct/set-cookies! context {:foo {:value "bar" :secure true :http-only true}})
"hello world")]
(with-server {:handler handler}
(let [response (th/get "/")]
(is (contains? response :cookies))
(is (= (get-in response [:cookies "foo" :path]) "/"))
(is (= (get-in response [:cookies "foo" :value]) "bar"))
(is (= (get-in response [:cookies "foo" :secure]) true)))))))
(testing "Using completable future as response."
(letfn [(handler [ctx]
(p/promise (fn [resolve reject]
(a/<!! (a/timeout 1000))
(resolve (http/ok "hello world")))))]
(with-server {:handler handler}
(let [response (th/get "/")]
(is (= (:body response) "hello world"))
(is (= (:status response) 200))))))
(testing "Using completable future as body."
(letfn [(handler [ctx]
(http/ok (p/promise (fn [resolve reject]
(a/<!! (a/timeout 1000))
(resolve "hello world")))))]
(with-server {:handler handler}
(let [response (th/get "/")]
(is (= (:body response) "hello world"))
(is (= (:status response) 200))))))
(testing "Using publisher as body"
(letfn [(handler [ctx]
(let [p (rx/from-coll ["hello" " " "world"])
p (rx/map str/upper p)]
(http/accepted p)))]
(with-server {:handler handler}
(let [response (th/get "/")]
(is (= (:body response) "HELLO WORLD"))
(is (= (:status response) 202))))))
(testing "Using manifold deferred as response."
(letfn [(handler [ctx]
(let [d (md/deferred)]
(a/thread
(md/success! d (http/accepted "hello world")))
d))]
(with-server {:handler handler}
(let [response (th/get "/")]
(is (= (:body response) "hello world"))
(is (= (:status response) 202))))))
(testing "Using manifold deferred as body."
(letfn [(handler [ctx]
(let [d (md/deferred)]
(a/thread
(md/success! d "hello world"))
(http/accepted d)))]
(with-server {:handler handler}
(let [response (th/get "/")]
(is (= (:body response) "hello world"))
(is (= (:status response) 202))))))
(testing "Using manifold stream as body."
(letfn [(handler [ctx]
(let [d (ms/stream 3)]
(a/thread
@(ms/put! d "hello")
@(ms/put! d " ")
@(ms/put! d "world")
(ms/close! d))
(http/accepted d)))]
(with-server {:handler handler}
(let [response (th/get "/")]
(is (= (:body response) "hello world"))
(is (= (:status response) 202))))))
(testing "Routing with regex matchig"
(letfn [(handler [ctx]
(let [params (:route-params ctx)]
(http/ok (:id params))))]
(let [app (ct/routes [[:prefix ":id:\\d+"
[:any handler]]])]
(with-server {:handler app}
(let [response (client/get (str base-url "/2"))]
(is (= (:body response) "2"))
(is (= (:status response) 200)))
(try
(client/get (str base-url "/foo"))
(throw (RuntimeException. "not expected"))
(catch clojure.lang.ExceptionInfo e
(let [data (ex-data e)]
(is (= (:status data) 404)))))))))
(testing "Serving authenticated assets in root."
(letfn [(handler1 [state context]
(reset! state 1)
"hello world")
(deftest context-data-forwarding-test
(letfn [(handler1 [context]
(ct/delegate {:foo 1}))
(handler2 [context]
(ct/delegate {:bar 2}))
(handler3 [context]
(ct/delegate {:baz (+ (:foo context)
(:bar context))}))
(handler4 [p context]
(deliver p (select-keys context [:foo :bar :baz]))
"hello world")]
(let [p (promise)]
(with-server {:handler (ct/routes [[:any handler1]
[:any handler2]
[:any handler3]
[:any (partial handler4 p)]])}
(let [response (th/get "/")]
(is (= (:body response) "hello world"))
(is (= (:status response) 200))
(is (= (deref p 1000 nil)
{:foo 1 :bar 2 :baz 3})))))))
(deftest async-context-delegation-test
(letfn [(handler1 [context]
(md/future
(ct/delegate)))
(handler2 [context]
(md/future
(http/accepted "hello world" {:Content-Type "plain/text"})))]
(with-server {:handler (ct/routes [[:any handler1]
[:any handler2]])}
(let [response (client/get (str base-url))]
(is (= (:body response) "hello world"))
(is (= (:status response) 202))))))
;; (deftest experiments
;; (letfn [(handler1 [context]
;; (println 1111)
;; "hello world")
;; (handler4 [context]
;; (println 4444)
;; (ct/delegate ))
;; (handler2 [context]
;; (println 2222)
;; "hello world")
;; (handler3 [context]
;; (println 3333)
;; "hello world")]
;; (with-server (ct/routes [[:insert
;; [:any handler4]
;; [:get "foo" handler1]]
;; [:insert
;; [:any handler4]
;; [:get "bar" handler2]]
;; [:any handler3]])
;; (let [response (client/get (str base-url "/bar"))]
;; (is (= (:body response) "hello world"))
;; (is (= (:status response) 200))))))