diff --git a/compiler+runtime/include/cpp/jank/runtime/core/seq.hpp b/compiler+runtime/include/cpp/jank/runtime/core/seq.hpp index 48e3d1d11..71b41f933 100644 --- a/compiler+runtime/include/cpp/jank/runtime/core/seq.hpp +++ b/compiler+runtime/include/cpp/jank/runtime/core/seq.hpp @@ -144,4 +144,6 @@ namespace jank::runtime object_ptr repeat(object_ptr val); object_ptr repeat(object_ptr n, object_ptr val); + + object_ptr sort(object_ptr coll); } diff --git a/compiler+runtime/include/cpp/jank/runtime/obj/native_vector_sequence.hpp b/compiler+runtime/include/cpp/jank/runtime/obj/native_vector_sequence.hpp index d6073c618..977e4c8b4 100644 --- a/compiler+runtime/include/cpp/jank/runtime/obj/native_vector_sequence.hpp +++ b/compiler+runtime/include/cpp/jank/runtime/obj/native_vector_sequence.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace jank::runtime::obj { @@ -18,6 +19,7 @@ namespace jank::runtime::obj native_vector_sequence(native_vector_sequence const &) = default; native_vector_sequence(native_vector const &data, size_t index); native_vector_sequence(native_vector &&data); + native_vector_sequence(option const &meta, native_vector &&data); native_vector_sequence(native_vector &&data, size_t index); /* behavior::object_like */ @@ -29,7 +31,7 @@ namespace jank::runtime::obj /* behavior::seqable */ native_vector_sequence_ptr seq(); - native_vector_sequence_ptr fresh_seq(); + native_vector_sequence_ptr fresh_seq() const; /* behavior::countable */ size_t count() const; @@ -42,8 +44,12 @@ namespace jank::runtime::obj /* behavior::sequenceable_in_place */ native_vector_sequence_ptr next_in_place(); + /* behavior::metadatable */ + native_vector_sequence_ptr with_meta(object_ptr const m) const; + object base{ obj_type }; native_vector data{}; size_t index{}; + option meta; }; } diff --git a/compiler+runtime/src/cpp/clojure/core_native.cpp b/compiler+runtime/src/cpp/clojure/core_native.cpp index 27ba38d51..b69c28a5b 100644 --- a/compiler+runtime/src/cpp/clojure/core_native.cpp +++ b/compiler+runtime/src/cpp/clojure/core_native.cpp @@ -504,6 +504,7 @@ jank_object_ptr jank_load_clojure_core_native() intern_fn("tagged-literal", &tagged_literal); intern_fn("tagged-literal?", &is_tagged_literal); intern_fn("sorted?", &is_sorted); + intern_fn("sort", &sort); /* TODO: jank.math? */ intern_fn("sqrt", static_cast(&runtime::sqrt)); diff --git a/compiler+runtime/src/cpp/jank/runtime/core/math.cpp b/compiler+runtime/src/cpp/jank/runtime/core/math.cpp index 9a49623ee..abca99c69 100644 --- a/compiler+runtime/src/cpp/jank/runtime/core/math.cpp +++ b/compiler+runtime/src/cpp/jank/runtime/core/math.cpp @@ -1419,7 +1419,7 @@ namespace jank::runtime native_real tan(object_ptr const l) { return visit_number_like( - [](auto const typed_l) -> native_real { return std::tanf(typed_l->to_real()); }, + [](auto const typed_l) -> native_real { return tanf(typed_l->to_real()); }, l); } diff --git a/compiler+runtime/src/cpp/jank/runtime/core/seq.cpp b/compiler+runtime/src/cpp/jank/runtime/core/seq.cpp index 2ed76a8cc..ddb2a8110 100644 --- a/compiler+runtime/src/cpp/jank/runtime/core/seq.cpp +++ b/compiler+runtime/src/cpp/jank/runtime/core/seq.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace jank::runtime @@ -1178,4 +1179,32 @@ namespace jank::runtime { return make_box(n, val); } + + object_ptr sort(object_ptr const coll) + { + return visit_seqable( + [](auto const typed_coll) -> object_ptr { + native_vector vec; + for(auto it(typed_coll->fresh_seq()); it != nullptr; it = it->next_in_place()) + { + vec.push_back(it->first()); + } + + std::stable_sort(vec.begin(), vec.end(), [](object_ptr const a, object_ptr const b) { + return runtime::compare(a, b) < 0; + }); + + using T = typename decltype(typed_coll)::value_type; + + if constexpr(behavior::metadatable) + { + return make_box(typed_coll->meta, std::move(vec)); + } + else + { + return make_box(std::move(vec)); + } + }, + coll); + } } diff --git a/compiler+runtime/src/cpp/jank/runtime/obj/native_vector_sequence.cpp b/compiler+runtime/src/cpp/jank/runtime/obj/native_vector_sequence.cpp index e00e72af2..60f176815 100644 --- a/compiler+runtime/src/cpp/jank/runtime/obj/native_vector_sequence.cpp +++ b/compiler+runtime/src/cpp/jank/runtime/obj/native_vector_sequence.cpp @@ -25,6 +25,13 @@ namespace jank::runtime::obj assert(!this->data.empty()); } + native_vector_sequence::native_vector_sequence(option const &meta, + native_vector &&data) + : data{ std::move(data) } + , meta{ meta } + { + } + /* behavior::objectable */ native_bool native_vector_sequence::equal(object const &o) const { @@ -61,7 +68,7 @@ namespace jank::runtime::obj return data.empty() ? nullptr : this; } - native_vector_sequence_ptr native_vector_sequence::fresh_seq() + native_vector_sequence_ptr native_vector_sequence::fresh_seq() const { return data.empty() ? nullptr : make_box(data, index); } @@ -108,4 +115,13 @@ namespace jank::runtime::obj { return make_box(head, data.empty() ? nullptr : this); } + + native_vector_sequence_ptr native_vector_sequence::with_meta(object_ptr const m) const + { + auto const meta(behavior::detail::validate_meta(m)); + auto ret(fresh_seq()); + ret->meta = meta; + return ret; + } + } diff --git a/compiler+runtime/src/jank/clojure/core.jank b/compiler+runtime/src/jank/clojure/core.jank index 71d8f2f77..4ddf94964 100644 --- a/compiler+runtime/src/jank/clojure/core.jank +++ b/compiler+runtime/src/jank/clojure/core.jank @@ -4924,21 +4924,13 @@ (fn [x y] (cond (pred x y) -1 (pred y x) 1 :else 0))) -(defn sort +(def 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"))) + clojure.core-native/sort) (defn sort-by "Returns a sorted sequence of the items in coll, where the sort @@ -7335,4 +7327,4 @@ fails, attempts to require sym's namespace and retries." "Returns true if num is negative or positive infinity, else false" clojure.core-native/infinite?) -(println "Bottom of clojure.core") \ No newline at end of file +(println "Bottom of clojure.core")