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 #218

Merged
merged 6 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler+runtime/include/cpp/jank/runtime/core/seq.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <jank/runtime/object.hpp>
#include <jank/option.hpp>

namespace jank::runtime::obj
{
Expand All @@ -18,6 +19,7 @@ namespace jank::runtime::obj
native_vector_sequence(native_vector_sequence const &) = default;
native_vector_sequence(native_vector<object_ptr> const &data, size_t index);
native_vector_sequence(native_vector<object_ptr> &&data);
native_vector_sequence(option<object_ptr> const &meta, native_vector<object_ptr> &&data);
native_vector_sequence(native_vector<object_ptr> &&data, size_t index);

/* behavior::object_like */
Expand All @@ -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;
Expand All @@ -42,6 +44,10 @@ 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;
option<object_ptr> meta;

bpiel marked this conversation as resolved.
Show resolved Hide resolved
object base{ obj_type };
native_vector<object_ptr> data{};
size_t index{};
Expand Down
1 change: 1 addition & 0 deletions compiler+runtime/src/cpp/clojure/core_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<native_real (*)(object_ptr)>(&runtime::sqrt));
Expand Down
2 changes: 1 addition & 1 deletion compiler+runtime/src/cpp/jank/runtime/core/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
26 changes: 26 additions & 0 deletions compiler+runtime/src/cpp/jank/runtime/core/seq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <jank/runtime/behavior/indexable.hpp>
#include <jank/runtime/behavior/stackable.hpp>
#include <jank/runtime/behavior/chunkable.hpp>
#include "jank/runtime/behavior/metadatable.hpp"
bpiel marked this conversation as resolved.
Show resolved Hide resolved
#include <jank/runtime/core.hpp>

namespace jank::runtime
Expand Down Expand Up @@ -1178,4 +1179,29 @@ namespace jank::runtime
{
return make_box<obj::repeat>(n, val);
}

object_ptr sort(object_ptr const coll)
{
return visit_seqable(
[](auto const typed_coll) -> object_ptr {
native_vector<object_ptr> 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<T>) {
return make_box<obj::native_vector_sequence>(typed_coll->meta, std::move(vec));
} else {
return make_box<obj::native_vector_sequence>(std::move(vec));
bpiel marked this conversation as resolved.
Show resolved Hide resolved
}
},
coll);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ namespace jank::runtime::obj
assert(!this->data.empty());
}

native_vector_sequence::native_vector_sequence(option<object_ptr> const &meta, native_vector<object_ptr> &&data)
: meta{ meta }
, data{ std::move(data) }
bpiel marked this conversation as resolved.
Show resolved Hide resolved
{
}

/* behavior::objectable */
native_bool native_vector_sequence::equal(object const &o) const
{
Expand Down Expand Up @@ -61,7 +67,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<native_vector_sequence>(data, index);
}
Expand Down Expand Up @@ -108,4 +114,13 @@ namespace jank::runtime::obj
{
return make_box<cons>(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;
}

}
14 changes: 3 additions & 11 deletions compiler+runtime/src/jank/clojure/core.jank
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
(println "Bottom of clojure.core")