Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement clojure.core/sort #178

Closed
jeaye opened this issue Jan 23, 2025 · 2 comments
Closed

Implement clojure.core/sort #178

jeaye opened this issue Jan 23, 2025 · 2 comments

Comments

@jeaye
Copy link
Member

jeaye commented Jan 23, 2025

We currently have this:

(defn sort
  "Returns a sorted sequence of the items in coll. If no comparator is
  supplied, uses compare.  comparator must implement
  java.util.Comparator.  Guaranteed to be stable: equal elements will
  not be reordered.  If coll is a Java array, it will be modified.  To
  avoid this, sort a copy of the array."
  ([coll]
   (sort compare coll))
  ([#_java.util.Comparator comp coll]
  ;;  (if (seq coll)
  ;;    (let [a (to-array coll)]
  ;;      (. java.util.Arrays (sort a comp))
  ;;      (with-meta (seq a) (meta coll)))
  ;;    ())
   (throw "TODO: port")))

The Clojure JVM version is included in there. As we can see, the collection is put into an array, the array is sorted, and then we return a sequence (carrying over the meta from the collection).

References

Steps

  • Add a object_ptr sort(object_ptr coll); to runtime/core/seq.hpp
  • Implement it in runtime/core/seq.cpp
  • Use visit_seqable to get a seqable, turn it into a seq, and then fill up a native_vector<object_ptr>
  • Use std::stable_sort on the vector
  • Return a native_vector_sequence box which takes ownership of the sorted vector
  • Use core_native.cpp to add an intern_fn call to wrap your sort fn
  • Update clojure/core.jank to forward the clojure.core-native/sort def
@bpiel
Copy link
Contributor

bpiel commented Jan 23, 2025

I'll take this.

@jeaye
Copy link
Member Author

jeaye commented Jan 30, 2025

Done in #218.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants