Skip to content

Commit 4e3baf3

Browse files
authored
Implement sort and sort-by functions (#78)
* Implement sort function for Glojure - Add SortSlice function in pkg/lang/sort.go that performs stable in-place sorting - Add Compare function to support Clojure's compare semantics (nil handling, cross-type numeric comparison) - Update ToSlice to handle all required types: nil→empty, IPersistentVector, IPersistentMap, string→char array - Add transformation in rewrite.clj to replace java.util.Arrays.sort with SortSlice - Add transformation for clojure.lang.Util.compare to use Compare function The implementation matches Clojure JVM semantics: - Stable sort (equal elements maintain order) - Comparator contract (-1/0/1 return values) - Proper nil handling (nil sorts before non-nil) - Support for custom comparators * Add sort tests Signed-off-by: James Hamlin <[email protected]> * Update number compare Signed-off-by: James Hamlin <[email protected]> * Use Comparer interface a la Java Comparable Signed-off-by: James Hamlin <[email protected]> * Sort test files for consistent run order Signed-off-by: James Hamlin <[email protected]> * Fix sort-by Signed-off-by: James Hamlin <[email protected]> * Add more sort-by tests Signed-off-by: James Hamlin <[email protected]> --------- Signed-off-by: James Hamlin <[email protected]>
1 parent 55fff5d commit 4e3baf3

22 files changed

+605
-12
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ STDLIB := $(STDLIB_ORIGINALS:scripts/rewrite-core/originals/%=%)
55
STDLIB_ORIGINALS := $(addprefix scripts/rewrite-core/originals/,$(STDLIB))
66
STDLIB_TARGETS := $(addprefix pkg/stdlib/glojure/,$(STDLIB:.clj=.glj))
77

8-
TEST_FILES := $(shell find ./test -name '*.glj')
8+
TEST_FILES := $(shell find ./test -name '*.glj' | sort)
99
TEST_TARGETS := $(addsuffix .test,$(TEST_FILES))
1010

1111
GOPLATFORMS := darwin_arm64 darwin_amd64 linux_arm64 linux_amd64 windows_amd64 windows_arm js_wasm
@@ -60,3 +60,10 @@ $(TEST_TARGETS): gocmd
6060

6161
.PHONY: test
6262
test: vet $(TEST_TARGETS)
63+
64+
.PHONY: format
65+
format:
66+
@if go fmt ./... | grep -q .; then \
67+
echo "Files were formatted. Please commit the changes."; \
68+
exit 1; \
69+
fi

internal/deps/pull.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
package deps
2-
3-

internal/persistent/vector/vector.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ type Vector interface {
4646

4747
// Iterator is an iterator over vector elements. It can be used like this:
4848
//
49-
// for it := v.Iterator(); it.HasElem(); it.Next() {
50-
// elem := it.Elem()
51-
// // do something with elem...
52-
// }
49+
// for it := v.Iterator(); it.HasElem(); it.Next() {
50+
// elem := it.Elem()
51+
// // do something with elem...
52+
// }
5353
type Iterator interface {
5454
// Elem returns the element at the current position.
5555
Elem() interface{}

pkg/gen/gljimports/gljimports_darwin_amd64.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
34663466
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
34673467
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
34683468
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
3469+
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
34693470
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
34703471
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
34713472
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
@@ -3888,6 +3889,7 @@ func RegisterImports(_register func(string, interface{})) {
38883889
_register("github.com/glojurelang/glojure/pkg/lang.SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)).Elem())
38893890
_register("github.com/glojurelang/glojure/pkg/lang.*SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)))
38903891
_register("github.com/glojurelang/glojure/pkg/lang.SliceSet", github_com_glojurelang_glojure_pkg_lang.SliceSet)
3892+
_register("github.com/glojurelang/glojure/pkg/lang.SortSlice", github_com_glojurelang_glojure_pkg_lang.SortSlice)
38913893
_register("github.com/glojurelang/glojure/pkg/lang.StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)).Elem())
38923894
_register("github.com/glojurelang/glojure/pkg/lang.*StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)))
38933895
_register("github.com/glojurelang/glojure/pkg/lang.Stacker", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Stacker)(nil)).Elem())

pkg/gen/gljimports/gljimports_darwin_arm64.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
34663466
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
34673467
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
34683468
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
3469+
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
34693470
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
34703471
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
34713472
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
@@ -3888,6 +3889,7 @@ func RegisterImports(_register func(string, interface{})) {
38883889
_register("github.com/glojurelang/glojure/pkg/lang.SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)).Elem())
38893890
_register("github.com/glojurelang/glojure/pkg/lang.*SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)))
38903891
_register("github.com/glojurelang/glojure/pkg/lang.SliceSet", github_com_glojurelang_glojure_pkg_lang.SliceSet)
3892+
_register("github.com/glojurelang/glojure/pkg/lang.SortSlice", github_com_glojurelang_glojure_pkg_lang.SortSlice)
38913893
_register("github.com/glojurelang/glojure/pkg/lang.StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)).Elem())
38923894
_register("github.com/glojurelang/glojure/pkg/lang.*StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)))
38933895
_register("github.com/glojurelang/glojure/pkg/lang.Stacker", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Stacker)(nil)).Elem())

pkg/gen/gljimports/gljimports_js_wasm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
34663466
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
34673467
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
34683468
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
3469+
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
34693470
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
34703471
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
34713472
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
@@ -3888,6 +3889,7 @@ func RegisterImports(_register func(string, interface{})) {
38883889
_register("github.com/glojurelang/glojure/pkg/lang.SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)).Elem())
38893890
_register("github.com/glojurelang/glojure/pkg/lang.*SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)))
38903891
_register("github.com/glojurelang/glojure/pkg/lang.SliceSet", github_com_glojurelang_glojure_pkg_lang.SliceSet)
3892+
_register("github.com/glojurelang/glojure/pkg/lang.SortSlice", github_com_glojurelang_glojure_pkg_lang.SortSlice)
38913893
_register("github.com/glojurelang/glojure/pkg/lang.StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)).Elem())
38923894
_register("github.com/glojurelang/glojure/pkg/lang.*StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)))
38933895
_register("github.com/glojurelang/glojure/pkg/lang.Stacker", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Stacker)(nil)).Elem())

pkg/gen/gljimports/gljimports_linux_amd64.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
34663466
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
34673467
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
34683468
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
3469+
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
34693470
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
34703471
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
34713472
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
@@ -3888,6 +3889,7 @@ func RegisterImports(_register func(string, interface{})) {
38883889
_register("github.com/glojurelang/glojure/pkg/lang.SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)).Elem())
38893890
_register("github.com/glojurelang/glojure/pkg/lang.*SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)))
38903891
_register("github.com/glojurelang/glojure/pkg/lang.SliceSet", github_com_glojurelang_glojure_pkg_lang.SliceSet)
3892+
_register("github.com/glojurelang/glojure/pkg/lang.SortSlice", github_com_glojurelang_glojure_pkg_lang.SortSlice)
38913893
_register("github.com/glojurelang/glojure/pkg/lang.StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)).Elem())
38923894
_register("github.com/glojurelang/glojure/pkg/lang.*StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)))
38933895
_register("github.com/glojurelang/glojure/pkg/lang.Stacker", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Stacker)(nil)).Elem())

pkg/gen/gljimports/gljimports_linux_arm64.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
34663466
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
34673467
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
34683468
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
3469+
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
34693470
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
34703471
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
34713472
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
@@ -3888,6 +3889,7 @@ func RegisterImports(_register func(string, interface{})) {
38883889
_register("github.com/glojurelang/glojure/pkg/lang.SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)).Elem())
38893890
_register("github.com/glojurelang/glojure/pkg/lang.*SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)))
38903891
_register("github.com/glojurelang/glojure/pkg/lang.SliceSet", github_com_glojurelang_glojure_pkg_lang.SliceSet)
3892+
_register("github.com/glojurelang/glojure/pkg/lang.SortSlice", github_com_glojurelang_glojure_pkg_lang.SortSlice)
38913893
_register("github.com/glojurelang/glojure/pkg/lang.StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)).Elem())
38923894
_register("github.com/glojurelang/glojure/pkg/lang.*StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)))
38933895
_register("github.com/glojurelang/glojure/pkg/lang.Stacker", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Stacker)(nil)).Elem())

pkg/gen/gljimports/gljimports_windows_amd64.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
34663466
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
34673467
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
34683468
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
3469+
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
34693470
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
34703471
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
34713472
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
@@ -3888,6 +3889,7 @@ func RegisterImports(_register func(string, interface{})) {
38883889
_register("github.com/glojurelang/glojure/pkg/lang.SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)).Elem())
38893890
_register("github.com/glojurelang/glojure/pkg/lang.*SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)))
38903891
_register("github.com/glojurelang/glojure/pkg/lang.SliceSet", github_com_glojurelang_glojure_pkg_lang.SliceSet)
3892+
_register("github.com/glojurelang/glojure/pkg/lang.SortSlice", github_com_glojurelang_glojure_pkg_lang.SortSlice)
38913893
_register("github.com/glojurelang/glojure/pkg/lang.StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)).Elem())
38923894
_register("github.com/glojurelang/glojure/pkg/lang.*StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)))
38933895
_register("github.com/glojurelang/glojure/pkg/lang.Stacker", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Stacker)(nil)).Elem())

pkg/gen/gljimports/gljimports_windows_arm.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
34663466
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
34673467
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
34683468
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
3469+
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
34693470
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
34703471
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
34713472
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
@@ -3888,6 +3889,7 @@ func RegisterImports(_register func(string, interface{})) {
38883889
_register("github.com/glojurelang/glojure/pkg/lang.SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)).Elem())
38893890
_register("github.com/glojurelang/glojure/pkg/lang.*SliceSeq", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.SliceSeq)(nil)))
38903891
_register("github.com/glojurelang/glojure/pkg/lang.SliceSet", github_com_glojurelang_glojure_pkg_lang.SliceSet)
3892+
_register("github.com/glojurelang/glojure/pkg/lang.SortSlice", github_com_glojurelang_glojure_pkg_lang.SortSlice)
38913893
_register("github.com/glojurelang/glojure/pkg/lang.StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)).Elem())
38923894
_register("github.com/glojurelang/glojure/pkg/lang.*StackFrame", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.StackFrame)(nil)))
38933895
_register("github.com/glojurelang/glojure/pkg/lang.Stacker", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Stacker)(nil)).Elem())

0 commit comments

Comments
 (0)