such.function-makers

Functions that make other functions.

Commonly used with a naming convention that flags such functions with mkfn:

(ns ...
  (:require [such.function-makers :as mkfn]))
...
(def stringlike? (mkfn/any-pred string? regex?))

lazyseq:criticize-deviationism

(lazyseq:criticize-deviationism deviancy-detector reporter)

Produces a function that inspects a given coll according to the deviancy-detector. When a deviant element is found, the reporter is called for side-effect. It is given the coll and the deviant element as arguments.

All deviant elements are reported. The original collection is returned.

   (def bad-attitude? neg?)
   (def flagged-negativity 
        (lazyseq:criticize-deviationism (comp neg? second)
                                        (fn [coll elt]
                                          (println "Bad attitude from" elt))))

   (def attitudes [ [:fred 32] [:joe 23] [:gary -10] [:brian -30] [:corey 10326] ])
   (flagged-negativity attitudes) ;; :gary and :brian are flagged.

lazyseq:x->abc

(lazyseq:x->abc f)(lazyseq:x->abc f pred)

Takes a transformer function and an optional predicate. The transformer function must produce a collection, call it coll. pred defaults to (constantly true). Produces a function that converts one lazy sequence into another. For each element of the input sequence:

  • If pred is falsey, the unchanged element is in the output sequence.

  • If pred is truthy, the new coll is “spliced” into the output sequence in place of the original element.

    (let [replace-with-N-copies (lazyseq:x->abc #(repeat % %))]
      (replace-with-N-copies [0 1 2 3]) => [1 2 2 3 3 3])
    
    (let [replace-evens-with-N-copies (lazyseq:x->abc #(repeat % %) even?)]
      (replace-evens-with-N-copies [0 1 2 3]) => [1 2 2 3])
    

lazyseq:x->xabc

(lazyseq:x->xabc f)(lazyseq:x->xabc f pred)

The same behavior as lazyseq:x->abc, except that the generated collection is spliced in after the original element, rather than replacing it.

    (let [augment-with-N-negatives (lazyseq:x->xabc #(repeat % (- %)))]
      (augment-with-N-negatives [0 1 2 3]) => [0 1 -1 2 -2 -2 3 -3 -3 -3])

lazyseq:x->y

(lazyseq:x->y f)(lazyseq:x->y f pred)

Takes an arbitrary function and an optional predicate. pred defaults to (constantly true). Produces a function that converts one lazy sequence into another. It differs from the input sequence in that elements for which pred is truthy are replaced with (f elt).

    (let [force-positive (lazyseq:x->y - neg?)]
      (force-positive [0 1 -2 -3]) => [0 1 2 3])

mkfn:lazyseq

(mkfn:lazyseq prefixer)

This is used to generate the lazyseq:x->... functions. See the source.

pred:any?

(pred:any? & preds)

Constructs a strict predicate that takes a single argument. That predicate returns true iff any of the preds is truthy of that argument.

 (def stringlike? (mkfn/pred:any? string? regex?))
 (stringlike? []) => false
 (stringlike? "") => true

 (def has-favs? (mkfn/pred:any? (partial some #{0 4}) odd?)
 (has-favs? [2 4]) => true
 (has-favs? [1 6]) => true

Stops checking after the first success. A predicate created from no arguments always returns true.

Note: this predates some-fn. It differs in that it always returns true or false, and that it allows zero arguments (which produces a function that always returns true).

none-of? is the complement.

pred:exception->false

(pred:exception->false pred)

Produces a new function. It returns whatever value pred does, except that it traps exceptions and returns false.

 ( (pred:exception->false even?) 4) => true

 (even? :hi) => (throws)
 ( (pred:exception->false even?) :hi) => false

pred:none-of?

(pred:none-of? & preds)

Constructs a strict predicate that takes a single argument. That predicate returns false iff any of the preds is truthy of that argument.

 (def not-function?
      (mkfn/pred:none-of? fn?
                          (partial instance? clojure.lang.MultiFn)))
 (not-function? even?) => false
 (not-function? "") => true

Stops checking after the first success. A predicate created from no arguments always returns false.

pred:any? is the complement.

pred:not-any?

Synonym for pred:none-of?.