Back

function? (clj)

(source)

function

(function? x)
Returns true if argument is a function or a symbol that resolves to a function (not a macro).

Examples

clojure/core.typed
(ns clojure.core.typed.test.testnew
  (:require [clojure.core.typed :as t] 
            [clojure.test :refer :all]                
            [clojure.core.typed.test.test-utils :refer :all]
            [clojure.core.typed.test.destructure]))

(deftest function?-test
  (is-tc-e (function? function?) Boolean
             :requires [[clojure.test :refer [function?]]])
  (is-tc-err (function? function?) String
             :requires [[clojure.test :refer [function?]]]))
  
(deftest assert-any-test
  (is-tc-e (assert-any "Hi" (= 4 (+ 2 2))) Any
           :requires [[clojure.test :refer [assert-any]]]))
           
(deftest do-report-test
  (is-tc-e #(do-report 1)
           :requires [[clojure.test :refer [do-report]]]))
            
(deftest run-tests-test
  (is-tc-e #(run-tests) [-> (Map Any Any)]
           :requires [[clojure.test :refer [run-tests]]])
  (is-tc-err #(run-tests) [-> String]
             :requires [[clojure.test :refer [run-tests]]]))
            
(deftest run-all-tests-test
  (is-tc-e #(run-all-tests) [-> (Map Any Any)]
           :requires [[clojure.test :refer [run-all-tests]]])
  (is-tc-e #(run-all-tests #"asdf") [-> (Map Any Any)]
           :requires [[clojure.test :refer [run-all-tests]]])
  (is-tc-err #(run-all-tests) [:-> String]
             :requires [[clojure.test :refer [run-all-tests]]]))
            
(deftest successful?-test
  (is-tc-e #(successful? {}) [-> Boolean]
           :requires [[clojure.test :refer [successful?]]]))
            
(deftest compose-fixtures-test
  (is-tc-e (compose-fixtures (fn [a :- [-> Any]] (a)) 
                             (fn [b :- [-> Any]] (b)))
           [[-> Any] -> Any]
           :requires [[clojure.test :refer [compose-fixtures]]]))
           
(deftest testing-vars-str-test
  (is-tc-e #(testing-vars-str {}) [-> String]
           :requires [[clojure.test :refer [testing-vars-str]]])
  (is-tc-err (testing-vars-str 1) Int
             :requires [[clojure.test :refer [testing-vars-str]]]))
           
(deftest testing-contexts-str-test
  (is-tc-e (testing-contexts-str)  String
           :requires [[clojure.test :refer [testing-contexts-str]]])
  (is-tc-err (testing-contexts-str) (Map Any Any)
             :requires [[clojure.test :refer [testing-contexts-str]]])
  (is-tc-err (testing-contexts-str 1)  String
             :requires [[clojure.test :refer [testing-contexts-str]]]))
juxt/bidi
(ns bidi.schema-test
  (:require
    #?(:clj  [clojure.test :refer :all]
       :cljs [cljs.test :refer-macros [deftest is testing]])
    [schema.core :as s]
    #?(:clj [schema.macros :refer [if-cljs]])
    [bidi.bidi :as bidi]
    [bidi.schema :as bs])
  #?(:cljs (:require-macros [bidi.schema-test :refer [is-valid is-invalid testing-schema]])))

      (is-invalid
        [(named [nil [(named (not #?(:clj  (bidi.schema/valid-qualifier-function? a-clojure.core$symbol)
                                     :cljs (bidi$schema$valid-qualifier-function? a-function)))
                             "qual")
                      nil]]
                "Pattern")
         nil]
        [["/" [symbol :i]] :target]))
typedclojure/typedclojure
(ns clojure.core.typed.test.testnew
  (:require [typed.clojure :as t]
            [clojure.test :refer :all]
            [typed.clj.checker.test-utils :refer :all]))

(deftest function?-test
  (is-tc-e (function? function?) t/Bool
           :requires [[clojure.test :refer [function?]]])
  (is-tc-err (function? function?) t/Str
             :requires [[clojure.test :refer [function?]]]))
  
(deftest assert-any-test
  (is-tc-e (assert-any "Hi" (= 4 (+ 2 2))) t/Any
           :requires [[clojure.test :refer [assert-any]]]))
           
(deftest do-report-test
  (is-tc-e #(do-report 1)
           :requires [[clojure.test :refer [do-report]]]))
            
(deftest run-tests-test
  (is-tc-e #(run-tests) [-> (t/Map t/Any t/Any)]
           :requires [[clojure.test :refer [run-tests]]])
  (is-tc-err #(run-tests) [-> t/Str]
             :requires [[clojure.test :refer [run-tests]]]))
            
(deftest run-all-tests-test
  (is-tc-e #(run-all-tests) [-> (t/Map t/Any t/Any)]
           :requires [[clojure.test :refer [run-all-tests]]])
  (is-tc-e #(run-all-tests #"asdf") [-> (t/Map t/Any t/Any)]
           :requires [[clojure.test :refer [run-all-tests]]])
  (is-tc-err #(run-all-tests) [:-> t/Str]
             :requires [[clojure.test :refer [run-all-tests]]]))
            
(deftest successful?-test
  (is-tc-e #(successful? {}) [-> t/Bool]
           :requires [[clojure.test :refer [successful?]]]))
            
(deftest compose-fixtures-test
  (is-tc-e (compose-fixtures (fn [a :- [-> t/Any]] (a)) 
                             (fn [b :- [-> t/Any]] (b)))
           [[-> t/Any] -> t/Any]
           :requires [[clojure.test :refer [compose-fixtures]]]))
           
(deftest testing-vars-str-test
  (is-tc-e #(testing-vars-str {}) [-> t/Str]
           :requires [[clojure.test :refer [testing-vars-str]]])
  (is-tc-err (testing-vars-str 1) t/Int
             :requires [[clojure.test :refer [testing-vars-str]]]))
           
(deftest testing-contexts-str-test
  (is-tc-e (testing-contexts-str) t/Str
           :requires [[clojure.test :refer [testing-contexts-str]]])
  (is-tc-err (testing-contexts-str) (t/Map t/Any t/Any)
             :requires [[clojure.test :refer [testing-contexts-str]]])
  (is-tc-err (testing-contexts-str 1) t/Str
             :requires [[clojure.test :refer [testing-contexts-str]]]))
typedclojure/typedclojure
(ns typed-example.symbolic-guide
  "A guide on symbolic execution in Typed Clojure.
  
  `preamble` explains the testing primitives we use to demonstrate typing
  checking behavior.
  
  `guide` is the guide"
  (:require [clojure.test :refer [deftest is]]
            [typed.clj.checker.test-utils :refer [is-tc-e is-tc-err tc-e]]
            [typed.clojure :as t]))

  ;; Error messages involving symbolic closures passed to polymorphic functions are problematic
  ;; since it is not obvious who exactly to blame for the error (the symbolic closure's body,
  ;; or the arguments to the polymorphic function?).
  ;;
  ;; If you are having trouble identifying why a symbolic closure fails to type check,
  ;; try annotating the function's arguments to localize the error message.
  (is-tc-err (map (fn [a]           (inc a)) [true])) ;; not much information is given.
  (is-tc-err (map (fn [a :- t/Bool] (inc a)) [true])) ;; the `inc` call is the problem.
exercism/clojure
(ns coordinate-transformation-test
  (:require [clojure.test :refer [deftest testing is function?]]
            [coordinate-transformation :refer [translate2d scale2d compose-transform memoize-transform]]))

(deftest translate2d-test
  (testing "should return a function"
    (is (true? (function? translate2d))))
  (testing "should be predictable"
    (is (let [dx 3 dy -5
              translator (translate2d dx dy)
              x1 0 y1 0
              expected [3 -5]]
          (= expected (translator x1 y1)))))
  (testing "should be reusable"
    (is (let [dx 3 dy -5
              translator (translate2d dx dy)
              x2 4 y2 5
              reusedExpected [7 0]]
          (= reusedExpected (translator x2 y2))))))

(deftest scale2d-test
  (testing "should return a function"
    (is (true? (function? scale2d))))
  (testing "should be predictable"
    (is (let [dx             4
              dy             2
              scaler         (scale2d dx dy)
              x1             1
              y1             1
              expected       [4 2]]
          (= expected (scaler x1 y1)))))
  (testing "should be reusable"
    (is (let [dx             4
              dy             2
              scaler         (scale2d dx dy)
              x2             -2
              y2             5
              reusedExpected [-8 10]]
          (= reusedExpected (scaler x2 y2))))))

(deftest compose-transform-test
 (testing "should return a function"
   (is (let [dx         -6
             dy         10
             translator (translate2d dx dy)
             sx         3
             sy         2
             scaler     (scale2d sx sy)]
         (true? (function? (compose-transform translator scaler))))))
  (testing "should compose two translate functions"
   (is (let [dx         -6
             dy         10
             translator (translate2d dx dy)
             compose-translate (compose-transform translator translator)]
         (= [-12 20] (compose-translate 0 0)))))
         (testing
          "should compose two scale functions"
          (is (let [sx         3
                    sy         2
                    scaler     (scale2d sx sy)
                    compose-scale (compose-transform scaler scaler)]
                (= [9 4] (compose-scale 1 1)))))
         (testing "should compose in the correct order: g(f(x))"
          (is
            (let [dx         -6
                  dy         10
                  translator (translate2d dx dy)
                  sx         3
                  sy         2
                  scaler     (scale2d sx sy)
                  composed (compose-transform scaler translator)]
              (= [-6 10] (composed 0 0)))))
         (testing "should compose in the opposite order: g(f(x))"
          (is (let [dx         -6
                    dy         10
                    translator (translate2d dx dy)
                    sx         3
                    sy         2
                    scaler     (scale2d sx sy)
                    composed (compose-transform translator scaler)]
                (= [-18 20] (composed 0 0))))))

(deftest memoize-transform-test
  (testing "should return a function"
    (is (function? (memoize-transform (translate2d 2 2)))))
  (testing "should return the same result if given the same input"
    (is (= [4 4] (let [memoized-translate (memoize-transform (translate2d 2 2))]
                   (memoized-translate 2 2))))
    (is (= [4 4] (let [memoized-translate (memoize-transform (translate2d 2 2))]
                   (memoized-translate 2 2)))))
  (testing "should return different results for different inputs"
    (is (= [3 4] (let [memoized-translate (memoize-transform (translate2d 1 2))]
                   (memoized-translate 2 2))))
    (is (= [7 8] (let [memoized-translate (memoize-transform (translate2d 1 2))]
                   (memoized-translate 6 6)))))
  (testing "should not call the memoized function if the input is the same"
    (let [fake-first (atom true)
          fake-transform (fn [_ _]
                           (if @fake-first
                             (do (reset! fake-first false)
                                 [1 1])
                             false))
          memoized-transform (memoize-transform fake-transform)]
      (is (= [1 1] (memoized-transform 5 5)))
      (is (= [1 1] (memoized-transform 5 5)))))
  (testing "should only remember the last result"
    (let [mock-fn (let [n (atom 0)]
                    (with-meta
                      (fn [x y]
                        (swap! n inc)
                        [(* x 2) (* y 2)])
                      {::call-count (fn [] @n)}))
          memoized-transform (memoize-transform mock-fn)]
      (is (= [2 2] (memoized-transform 1 1)))
      (is (= [4 4] (memoized-transform 2 2)))
      (is (= [2 2] (memoized-transform 1 1)))
      (is (= 3 ((::call-count (meta mock-fn))))))))