Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (def example-history
- {:txns [{:ops [{:f :read, :k :x, :v 1}]}]})
- (defn index-txns
- "Takes a raw history, and adds a sequential integer index :i to each txn in the history."
- [raw-history]
- (update example-history :txns
- (partial map-indexed (fn [i txn] (assoc txn :i i)))))
- ; So in core.typed I'd type this as (ignoring type functions and just writing things out literally)
- (defalias Read (HMap :mandatory {:f :read, :k Any, :v Any}))
- (defalias Write (HMap :mandatory {:f :write, :k Any, :v Any}))
- (defalias Op (U Read Write))
- (defalias Txn (HMap :mandatory {:ops (Vec Op)}))
- (defalias IndexedTxn (I Txn (HMap :mandatory {:i Long})))
- (defalias RawHistory (HMap :mandatory {:txns (Vec Txn)}))
- (defalias IndexedHistory (HMap :mandatory {:txns (Vec IndexedTxn)}))
- (ann index-txns [RawHistory -> IndexedHistory])
- ; I often write programs with several passes like `index-txns`, and many functions that use
- ; various intermediate types--perhaps depending on a particular index structure having been computed.
- ; I'd like the type system, or clojure.spec, to enforce correctness here--but it seems silly to pick
- ; new key names for every single interpretation of a history or txn. Clojure.spec seems to suggest
- ; that I choose new namespaced keys, like :my.project.history.indexed/txns,
- ; :my.project.history.raw/txns, etc, which I could... see working internally, but is both a.) clunky
- ; and b.) at odds with what the library as a whole is supposed to do, e.g, take and return maps with
- ; unqualified keywords with slightly different interpretations, owing, for example, to the introduction
- ; of new fields.
- ; What I really want, I suppose, is a clojure.spec equivalent for HMap.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement