Liking cljdoc? Tell your friends :D

robertluo.fun-map

fun-map Api

fun-map Api
raw docstring

closeableclj/s

(closeable r close-fn)

Returns a wrapped plain value which implements IDeref, Haltable, and (in CLJ) java.io.Closeable. The close-fn is an effectful function with no arguments.

When used inside a life-cycle-map, close-fn will be called when halting the map via (halt! the-map).

In Clojure, the returned value works with with-open:

(with-open [conn (closeable (create-conn) #(close-conn conn))] (use-conn @conn))

Returns a wrapped plain value which implements IDeref, Haltable, and (in CLJ)
java.io.Closeable. The close-fn is an effectful function with no arguments.

When used inside a life-cycle-map, close-fn will be called when
halting the map via `(halt! the-map)`.

In Clojure, the returned value works with `with-open`:

  (with-open [conn (closeable (create-conn) #(close-conn conn))]
    (use-conn @conn))
sourceraw docstring

fnkclj/smacro

(fnk args & body)

A shortcut for fw macro. Returns a cached FunctionWrapper that:

  1. Destructures the specified keys from the fun-map
  2. Automatically focuses on those keys (re-evaluates when they change)

Equivalent to: (fnk [a b] body) => (fw {:keys [a b] :focus [a b]} body)

Note: Namespace qualifiers on keys are used for destructuring but stripped for focus comparison. E.g., (fnk [:ns/a] ...) destructures :ns/a but focuses on the local binding a.

Works in both Clojure and ClojureScript.

A shortcut for `fw` macro. Returns a cached FunctionWrapper that:
1. Destructures the specified keys from the fun-map
2. Automatically focuses on those keys (re-evaluates when they change)

Equivalent to:
  (fnk [a b] body) => (fw {:keys [a b] :focus [a b]} body)

Note: Namespace qualifiers on keys are used for destructuring but stripped
for focus comparison. E.g., `(fnk [:ns/a] ...)` destructures `:ns/a` but
focuses on the local binding `a`.

Works in both Clojure and ClojureScript.
sourceraw docstring

fun-mapclj/s

(fun-map m & {:keys [trace-fn keep-ref]})

Returns a new fun-map.

A fun-map is a special map which will automatically unwrap a value if it's a wrapper when accessed by key. A wrapper is anything which wrapped a ordinary value inside. Many clojure data structures are wrapper, such as atom, ref, future, delay, agent etc. In fact, anything implements clojure.lang.IDeref interface is a wrapper.

FunctionWrapper is another wrapper can be used in a fun-map, which wraps a function, it will be called with the fun-map itself as the argument.

Map m is the underlying storage of a fun-map, fun-map does not change its property except accessing values.

Options:

  • :trace-fn An effectful function for globally tracing FunctionWrapper calls. Accepts key and value as arguments.
  • :keep-ref When true, IDeref values (delay, future, atom, etc.) will NOT be automatically dereferenced. Use this when you want to store refs as actual values. Individual values can still use fnk or fw to opt into lazy evaluation.

Example:

(fun-map {:a 35 :b (delay (+ 5 3))}) ; (:b m) => 8 (fun-map {:a (atom 1)} :keep-ref true) ; (:a m) => #<Atom@... 1>

Returns a new fun-map.

A fun-map is a special map which will automatically *unwrap* a value if it's a
wrapper when accessed by key. A wrapper is anything which wrapped a ordinary value
inside. Many clojure data structures are wrapper, such as atom, ref, future, delay,
agent etc. In fact, anything implements clojure.lang.IDeref interface is a wrapper.

FunctionWrapper is another wrapper can be used in a fun-map, which wraps a function,
it will be called with the fun-map itself as the argument.

Map m is the underlying storage of a fun-map, fun-map does not change its property
except accessing values.

Options:

 - :trace-fn   An effectful function for globally tracing FunctionWrapper calls.
               Accepts key and value as arguments.
 - :keep-ref   When true, IDeref values (delay, future, atom, etc.) will NOT be
               automatically dereferenced. Use this when you want to store refs
               as actual values. Individual values can still use `fnk` or `fw`
               to opt into lazy evaluation.

Example:

  (fun-map {:a 35 :b (delay (+ 5 3))})  ; (:b m) => 8
  (fun-map {:a (atom 1)} :keep-ref true) ; (:a m) => #<Atom@... 1>
sourceraw docstring

fun-map?clj/s

(fun-map? m)

If m is a fun-map

If m is a fun-map
sourceraw docstring

fwclj/smacro

(fw arg-map & body)

Returns a FunctionWrapper of an anonymous function defined by body.

Since a FunctionWrapper's function will be called with the map itself as the argument, this macro uses a map arg-map as its argument. It follows the same syntax of Clojure's associative destructuring. You may use :keys, :as, :or inside.

Options (special keys in arg-map):

  • :wrappers Controls caching and tracing behavior:

    • [] No caching, no tracing. Function called on every access.
    • (default) [:trace :cache] - cached and traceable (see below).
  • :focus A form evaluated to determine if cached value is stale. Must be pure and efficient. If the focus value changes, the function is re-evaluated. Without :focus, the function is called only once (memoized).

  • :trace A function (fn [k v] ...) called when the wrapped function is actually invoked (not on cache hits).

  • :par? When true, dependencies are accessed in parallel using manifold's let-flow. Requires manifold on classpath.

Example:

(fw {:keys [a b] :as m :trace (fn [k v] (println k v)) :focus (select-keys m [:a :b])} (+ a b))

Works in both Clojure and ClojureScript.

Returns a FunctionWrapper of an anonymous function defined by body.

Since a FunctionWrapper's function will be called with the map itself as the
argument, this macro uses a map `arg-map` as its argument. It follows the
same syntax of Clojure's associative destructuring. You may use `:keys`, `:as`,
`:or` inside.

Options (special keys in arg-map):

- `:wrappers` Controls caching and tracing behavior:
  - `[]`      No caching, no tracing. Function called on every access.
  - (default) `[:trace :cache]` - cached and traceable (see below).

- `:focus`   A form evaluated to determine if cached value is stale.
             Must be pure and efficient. If the focus value changes,
             the function is re-evaluated. Without `:focus`, the function
             is called only once (memoized).

- `:trace`   A function `(fn [k v] ...)` called when the wrapped function
             is actually invoked (not on cache hits).

- `:par?`    When true, dependencies are accessed in parallel using
             manifold's `let-flow`. Requires manifold on classpath.

Example:

 (fw {:keys  [a b]
      :as    m
      :trace (fn [k v] (println k v))
      :focus (select-keys m [:a :b])}
   (+ a b))

Works in both Clojure and ClojureScript.
sourceraw docstring

Haltableclj/sprotocol

Life cycle protocol, signature just like java.io.Closeable, being a protocol gives user ability to extend

Life cycle protocol, signature just like java.io.Closeable,
being a protocol gives user ability to extend

halt!clj/s

(halt! this)
sourceraw docstring

life-cycle-mapclj/s

(life-cycle-map m)

Returns a fun-map that can be shutdown orderly.

Any value satisfying the Haltable protocol in this map will be considered a component. Its halt! method will be called in reverse order of creation when the map itself is halted via (halt! the-map).

Note: Only accessed components will be shutdown.

Returns a fun-map that can be shutdown orderly.

Any value satisfying the `Haltable` protocol in this map will be considered
a component. Its `halt!` method will be called in reverse order of creation
when the map itself is halted via `(halt! the-map)`.

Note: Only accessed components will be shutdown.
sourceraw docstring

lookupclj

(lookup f)

Returns a ILookup object for calling f on k

Returns a ILookup object for calling f on k
sourceraw docstring

touchclj/s

(touch m)

Forcefully evaluate all entries of a map and returns itself.

Forcefully evaluate all entries of a map and returns itself.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close