Advertisement
Guest User

Untitled

a guest
May 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. (def example-history
  2. {:txns [{:ops [{:f :read, :k :x, :v 1}]}]})
  3.  
  4. (defn index-txns
  5. "Takes a raw history, and adds a sequential integer index :i to each txn in the history."
  6. [raw-history]
  7. (update example-history :txns
  8. (partial map-indexed (fn [i txn] (assoc txn :i i)))))
  9.  
  10. ; So in core.typed I'd type this as (ignoring type functions and just writing things out literally)
  11.  
  12. (defalias Read (HMap :mandatory {:f :read, :k Any, :v Any}))
  13. (defalias Write (HMap :mandatory {:f :write, :k Any, :v Any}))
  14. (defalias Op (U Read Write))
  15.  
  16. (defalias Txn (HMap :mandatory {:ops (Vec Op)}))
  17. (defalias IndexedTxn (I Txn (HMap :mandatory {:i Long})))
  18.  
  19. (defalias RawHistory (HMap :mandatory {:txns (Vec Txn)}))
  20. (defalias IndexedHistory (HMap :mandatory {:txns (Vec IndexedTxn)}))
  21.  
  22. (ann index-txns [RawHistory -> IndexedHistory])
  23.  
  24. ; I often write programs with several passes like `index-txns`, and many functions that use
  25. ; various intermediate types--perhaps depending on a particular index structure having been computed.
  26. ; I'd like the type system, or clojure.spec, to enforce correctness here--but it seems silly to pick
  27. ; new key names for every single interpretation of a history or txn. Clojure.spec seems to suggest
  28. ; that I choose new namespaced keys, like :my.project.history.indexed/txns,
  29. ; :my.project.history.raw/txns, etc, which I could... see working internally, but is both a.) clunky
  30. ; and b.) at odds with what the library as a whole is supposed to do, e.g, take and return maps with
  31. ; unqualified keywords with slightly different interpretations, owing, for example, to the introduction
  32. ; of new fields.
  33.  
  34. ; What I really want, I suppose, is a clojure.spec equivalent for HMap.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement