A lightweight utility for establishing and querying symmetric one-to-one relationships.
A lightweight utility for establishing and querying symmetric one-to-one relationships.
(look-up s)(look-up s ->rec)Establishes symmetric one-to-one relationships and returns a LookUp
instance which provides efficient look ups.
A lone s is a sequential of hash-maps. If given a sequential s of field
values and a record constructor ->rec, invokes the constructor with the
elements of s. Prefer this later, two-arity version.
Throws an exception:
Example, 1-arity consuming a sequence of hash-maps:
(look-up [{:flower "rose" :color :red :id 101}
{:flower "hibiscus" :color :orange :id 102}
{:flower "sunflower" :color :sunflower :id 103}])
Example, 2-arity consuming a sequence of field values plus a record constructor (preferred):
(defrecord Flower [flower color id])
(look-up [["rose" :red 101]
["hibiscus" :orange 102]
["sunflower" :yellow 103]]
->Flower)
Establishes symmetric one-to-one relationships and returns a [[LookUp]]
instance which provides efficient look ups.
A lone `s` is a sequential of hash-maps. If given a sequential `s` of field
values and a record constructor `->rec`, invokes the constructor with the
elements of `s`. Prefer this later, two-arity version.
Throws an exception:
1. If key/fields don't exactly match, or
2. If a value is not unique within its column.
Example, 1-arity consuming a sequence of hash-maps:
```clojure
(look-up [{:flower "rose" :color :red :id 101}
{:flower "hibiscus" :color :orange :id 102}
{:flower "sunflower" :color :sunflower :id 103}])
```
Example, 2-arity consuming a sequence of field values plus a record
constructor (preferred):
```clojure
(defrecord Flower [flower color id])
(look-up [["rose" :red 101]
["hibiscus" :orange 102]
["sunflower" :yellow 103]]
->Flower)
```Methods for arranging symmetric one-to-one relationships and performing efficient look ups.
Use look-up for creating an instance. Instances implement the invoke
methods of clojure.lang.IFn with get-val.
Sample data for the following examples:
(defrecord Flower [flower color id])
(def flowers (look-up [["rose" :red 101]
["hibiscus" :orange 102]
["sunflower" :yellow 103]]
->Flower))
Methods for arranging symmetric one-to-one relationships and performing
efficient look ups.
Use [[look-up]] for creating an instance. Instances implement the `invoke`
methods of `clojure.lang.IFn` with [[get-val]].
Sample data for the following examples:
```clojure
(defrecord Flower [flower color id])
(def flowers (look-up [["rose" :red 101]
["hibiscus" :orange 102]
["sunflower" :yellow 103]]
->Flower))
```(expansion this)Returns a hash-map of the expanded key-vals which enable efficient look ups.
Example:
(expansion flowers)
Returns a hash-map of the expanded key-vals which enable efficient look ups. Example: ```clojure (expansion flowers) ```
(get-row this key value)(get-row this key value not-found)Returns the row containing val in column key.
Example query: Return the row whose :flower is "rose".
(get-row flowers :flower "rose")
;; => #one_see.Flower{:flower "rose", :color :red, :id 101}
Returns the row containing `val` in column `key`.
Example query: *Return the row whose `:flower` is `"rose"`.*
```clojure
(get-row flowers :flower "rose")
;; => #one_see.Flower{:flower "rose", :color :red, :id 101}
```(get-val this key-1 val-1 key-2)(get-val this key-1 val-1 key-2 not-found)Efficiently finds the row whose val-1 is associated to key-1, then
returns val-2 associated with key-2. If not found, returns nil or
not-found.
get-val provides the invoke implementation, so a LookUp instance in the
function position delegates to get-val.
Example query: What is the :id of the row whose :flower is "rose"?
Example, explicit invocation:
(get-val flowers :flower "rose" :id)
;; => 101
Example, implicit invocation:
(flowers :flower "rose" :id)
;; => 101
Efficiently finds the row whose `val-1` is associated to `key-1`, then returns `val-2` associated with `key-2`. If not found, returns `nil` or `not-found`. `get-val` provides the `invoke` implementation, so a `LookUp` instance in the function position delegates to `get-val`. Example query: *What is the `:id` of the row whose `:flower` is `"rose"`?* Example, explicit invocation: ```clojure (get-val flowers :flower "rose" :id) ;; => 101 ``` Example, implicit invocation: ```clojure (flowers :flower "rose" :id) ;; => 101 ```
(size this)Returns an array-map of a LookUp instance's row and column counts.
Example:
(size flowers)
;; => {:n-rows 3, :n-cols 3}
Returns an array-map of a `LookUp` instance's row and column counts.
Example:
```clojure
(size flowers)
;; => {:n-rows 3, :n-cols 3}
```(table this)Returns the original data in tabular form, as provided to [[look-up]]. Example: ```clojure (table flowers) ```
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |