Skip to content
Mike Fikes edited this page Feb 24, 2016 · 3 revisions

Note: There is now an IComparable protocol, which cljs.core/compare will make use of.

Problem statement

TODO

Terms

  • comparison fn: boolean valued fn
  • comparator: -/0/+ valued fn
  • comp: either of the above

(These terms are not good, but I wanted one-word ways to talk about each idea below.)

Issues

  • different underlying platform API
    • Java expects comparators (instances of Comparator)
    • goog expects comparison fns
  • users care about composability, not abstractions
    • Clojure fns can be either comparators or comparison fns
    • implementation via wrapper in AFn's Comparable implementation
    • newly created fns just work
  • protocols not helpful for implementation
    • if fns were some kind of IComparable, would just have to convert back to plain fns for goog to use them
    • don't see any obvious performance win
  • protocols not helpful for consumers
    • making comp writers implement a protocol is just introducing a Javaism

Proposed Approach

  • usage
    • no Comparable protocol
    • just use fns
  • implementation
    • do AFn's wrapping work in a helper fn that wraps user fns inside sort

I am pretty confident about the usage. Less confident about implementation, but this can be improved later.

Clone this wiki locally