When programming in a functional language, the often comes a time when we must couple data to functions like in an Object Oriented language. I mostly see this happen in terms of databases and relations.
In my hypothetical application I have users that have many documents. Here’s what their Hyperion protocols might look like.
(ns sample.user.user
(:require [hyperion.api :refer [defentity]]))
(defentity User
[name])
(ns sample.document.document
(:require [hyperion.api :refer [defentity]]))
(defentity Document
[name]
[body]
[user-key :type :key])
What if we wanted to add a method for users to get their corresponding documents? Follow certain conventions we can create a method that looks very OO.
(ns sample.user.user
(:require [hyperion.api :refer [defentity find-by-kind]]))
;defentity
(defn documents [this]
(find-by-kind "document" :filters [:= :user-key (:key this)]))
By using this
as the name of the argument we are but into the mindset of
manipulating User hashes. Every function defined inside this namespace should
take a user hash as the first argument.