Public Vars

Back

even? (clj)

(source)

function

(even? n)
Returns true if n is even, throws an exception if n is not an integer

Examples

clojure
(ns clojure.test-clojure.reducers
  (:require [clojure.core.reducers :as r]
            [clojure.test.generative :refer (defspec)]
            [clojure.data.generators :as gen])
  (:use clojure.test))

(defequivtest test-filter
  [filter r/filter #(into [] %)]
  [even? odd? #(< 200 %) identity])
penpot/penpot
#_:clj-kondo/ignore
(ns app.common.data.macros
  "Data retrieval & manipulation specific macros."
  (:refer-clojure :exclude [get-in select-keys str with-open min max])
  #?(:cljs (:require-macros [app.common.data.macros]))
  (:require
   #?(:clj [clojure.core :as c]
      :cljs [cljs.core :as c])
   [app.common.data :as d]
   [cljs.analyzer.api :as aapi]
   [cuerdas.core :as str]))

(defmacro with-open
  [bindings & body]
  {:pre [(vector? bindings)
         (even? (count bindings))
         (pos? (count bindings))]}
  (reduce (fn [acc bindings]
            `(let ~(vec bindings)
               (try
                 ~acc
                 (finally
                   (d/close! ~(first bindings))))))
          `(do ~@body)
          (reverse (partition 2 bindings))))
typedclojure/typedclojure
(ns typed.clojure.jvm
  "JVM-specific annotations and operations.
  See typed.clojure for cross-platform ops."
  (:require clojure.core.typed
            [clojure.core.typed.current-impl :as impl]
            [clojure.core.typed.internal :refer [take-when]]
            [typed.cljc.runtime.env-utils :refer [delay-type]]
            [clojure.core.typed.macros :as macros]))

(defmacro override-classes [& args]
  (assert (even? (count args)))
  `(do ~@(map (fn [[nme [frees & {:as opts}]]]
                `(override-class ~@(some-> (not-empty frees) vector) ~nme ~(or (not-empty opts) {})))
              (partition-all 2 args))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure.jvm
  "JVM-specific type annotations for the base Clojure distribution."
  (:require [typed.clojure :as t]
            [typed.clojure.jvm :refer [override-classes]]
            clojure.core.typed)
  (:import (clojure.lang Named IMapEntry AMapEntry Seqable
                         LazySeq PersistentHashSet PersistentTreeSet PersistentTreeMap PersistentList APersistentVector
                         APersistentSet IPersistentSet IPersistentMap IPersistentVector
                         APersistentMap IDeref ISeq IPersistentCollection
                         ILookup Indexed Associative IPersistentStack PersistentVector Cons
                         IPersistentList IRef ARef Reversible
                         ITransientCollection ITransientSet ITransientAssociative ITransientMap
                         ITransientVector PersistentHashMap Reduced MultiFn Sorted)
           (java.util Collection RandomAccess)))

; 1. what is the variance of the dispatch function? covariant?
;
;   (ann f (MultiFn [t/Int -> t/Int] [t/Int -> Bool]))
;   (defmulti f even?)
;
;   ; make return types less specific -- seems ok. just means
;   ; we can infer less in defmethod's.
;   (ann-form f (MultiFn [t/Int -> t/Any] [t/Int -> t/Any]))
;
;   (ann g (MultiFn [t/Int -> t/Int] [t/Int -> Bool]))
;   (defmulti g odd?)
;
; 2. what bound do we want on `f` and `d`?
;   ; say if we don't have `d`'s lower bound as EveryIFn..
;   (ann f (MultiFn .. ':a))
;   (defmulti f :a)
;
;   (ann g (MultiFn .. (ToIFn ':a)))
;   (defmulti g (fn [a] (:a a)))
;
;   ; is this desirable? let's use the lower bound, since we can always
;   ; make the type more permissive later.
;   (ann-form f (TypeOf g)) ;ok
;   (ann-form g (TypeOf f)) ;fails
nubank/nodely
(ns nodely.samples.tutorial
  (:require [nodely.api.v0 :as nodely :refer [>value >leaf >if >cond >sequence >and >or]]
            [clojure.core.async :as async]))

;; A branch node is able to express conditional dependencies
;; We can create a branch with >if, the first argument is a condition node,
;; the second argument is the truthy node and the third argument is the falsey node
;; At evaluation, if condition node is truthy, truthy node will be evaluated
;; otherwise, falsey node is evaluated
(def a-branch (>if (>leaf (even? ?a))
                   a-value
                   a-leaf))
;; => #:nodely.data{:type :branch,
;;                  :condition #:nodely.data{:type :leaf
;;                                           :inputs #{:a}
;;                                           :fn #function[nodely.samples.tutorial/fn--28863]}
;;                  :truthy #:nodely.data{:type :value :value 1}
;;                  :falsey #:nodely.data{:type :leaf
;;                                        :inputs #{:b :a}
;;                                        :fn #function[nodely.samples.tutorial/fn--28857]}}

;; Value nodes are automatically inferred
(def a-branch-2 (>if (>leaf (even? ?a))
                     1
                     a-leaf))
(:nodely.data/truthy a-branch-2)
;; => #:nodely.data{:type :value :value 1}

;; We can also define a branch with >cond
;; Similar to clojure.core/cond, if no clauses match, >cond evaluates to nil
(def another-branch (>cond (>leaf (even? ?a)) 1
                           :else              (>leaf (+ ?a ?b))))

(def env-with-branch
  {:x (>leaf (+ 1 1))
   :y (>leaf (do (Thread/sleep 10000) 1))
   :z (>if (>leaf (even? ?x))
           (>leaf ?x)
           (>leaf ?y))})

;; y represents an expensive call that we want to avoid when possible
;; In this case, y is not needed because (even? ?x) evaluates to true
;; So the dependency on why is still a leaf (unevaluated) and not a value
jdevuyst/termcat
(ns termcat.core-macros
  (:require [clojure.core.match :refer (match)]
            [clojure.core.reducers :as r]
            [termcat.term :as t]
            [termcat.rewrite :as rw]))

(defmacro window [init-state proj [& arg-list] & body]
  (assert (or (nil? init-state) (map? init-state)))
  (assert (reduce #(and %1 (symbol? %2)) true arg-list))
  (assert (even? (count body)))
  (let [argc (-> arg-list count dec)]
    `(fn
       ([] ~init-state)
       ([state# input#]
        (let [padded-input# (concat input# (repeat nil))
              [~@arg-list] (cons state# (take ~argc padded-input#))]
          (if-let [r# (match (->> padded-input#
                                  (r/map ~proj)
                                  (r/take ~argc)
                                  (r/reduce conj [state#]))
                             ~@body
                             :else nil)]
            [(or (first r#) state#)
             (concat (next r#)
                     (drop ~argc input#))]))))))