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

Fix sort fails for lists and vectors (#66) #69

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_darwin_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_darwin_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_js_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_linux_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_linux_arm64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_windows_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
Expand Down
1 change: 1 addition & 0 deletions pkg/gen/gljimports/gljimports_windows_arm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3466,6 +3466,7 @@ func RegisterImports(_register func(string, interface{})) {
_register("github.com/glojurelang/glojure/pkg/lang.ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.*ChunkedCons", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.ChunkedCons)(nil)))
_register("github.com/glojurelang/glojure/pkg/lang.CloneThreadBindingFrame", github_com_glojurelang_glojure_pkg_lang.CloneThreadBindingFrame)
_register("github.com/glojurelang/glojure/pkg/lang.Compare", github_com_glojurelang_glojure_pkg_lang.Compare)
_register("github.com/glojurelang/glojure/pkg/lang.Comparer", reflect.TypeOf((*github_com_glojurelang_glojure_pkg_lang.Comparer)(nil)).Elem())
_register("github.com/glojurelang/glojure/pkg/lang.ConcatStrings", github_com_glojurelang_glojure_pkg_lang.ConcatStrings)
_register("github.com/glojurelang/glojure/pkg/lang.Conj", github_com_glojurelang_glojure_pkg_lang.Conj)
Expand Down
72 changes: 72 additions & 0 deletions pkg/lang/compare.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package lang

import (
"fmt"
"strings"
)

func Compare(a, b any) int {
if s1, ok := a.(string); ok {
if s2, ok := b.(string); ok {
return strings.Compare(s1, s2)
}
panic(NewIllegalArgumentError(fmt.Sprintf("cannot compare unrelated types: %T, %T", a, b)))
} else if i1, ok := a.(int); ok {
if i2, ok := b.(int); ok {
if i1 > i2 {
return 1
}
if i1 < i2 {
return -1
}
return 0
}
panic(NewIllegalArgumentError(fmt.Sprintf("cannot compare unrelated types: %T, %T", a, b)))
} else if i1, ok := a.(int32); ok {
if i2, ok := b.(int32); ok {
if i1 > i2 {
return 1
}
if i1 < i2 {
return -1
}
return 0
}
panic(NewIllegalArgumentError(fmt.Sprintf("cannot compare unrelated types: %T, %T", a, b)))
} else if i1, ok := a.(int64); ok {
if i2, ok := b.(int64); ok {
if i1 > i2 {
return 1
}
if i1 < i2 {
return -1
}
return 0
}
panic(NewIllegalArgumentError(fmt.Sprintf("cannot compare unrelated types: %T, %T", a, b)))
} else if i1, ok := a.(float32); ok {
if i2, ok := b.(float32); ok {
if i1 > i2 {
return 1
}
if i1 < i2 {
return -1
}
return 0
}
panic(NewIllegalArgumentError(fmt.Sprintf("cannot compare unrelated types: %T, %T", a, b)))
} else if i1, ok := a.(float64); ok {
if i2, ok := b.(float64); ok {
if i1 > i2 {
return 1
}
if i1 < i2 {
return -1
}
return 0
}
panic(NewIllegalArgumentError(fmt.Sprintf("cannot compare unrelated types: %T, %T", a, b)))
}

panic(NewIllegalArgumentError(fmt.Sprintf("unable to compare types: %T, %T", a, b)))
}
14 changes: 14 additions & 0 deletions pkg/lang/slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ func ToSlice(x any) []any {
}
return res
}
if s, ok := x.(Vector); ok {
res := make([]interface{}, 0, s.Count())
for s := Seq(s); s != nil; s = s.Next() {
res = append(res, s.First())
}
return res
}
if s, ok := x.(*Vector); ok {
res := make([]interface{}, 0, s.Count())
for s := Seq(s); s != nil; s = s.Next() {
res = append(res, s.First())
}
return res
}
xVal := reflect.ValueOf(x)
if xVal.Kind() == reflect.Slice || xVal.Kind() == reflect.Array {
res := make([]interface{}, xVal.Len())
Expand Down
14 changes: 7 additions & 7 deletions pkg/stdlib/glojure/core.glj
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@

(defn identical?
"Tests if 2 arguments are the same object"
{:inline (fn [x y] `(. glojure.lang.Util identical ~x ~y))
{:inline (fn [x y] `(. github.com$glojurelang$glojure$pkg$lang.Identical ~x ~y))
:inline-arities #{2}
:added "1.0"}
([x y] (github.com$glojurelang$glojure$pkg$lang.Identical x y)))
Expand All @@ -779,7 +779,7 @@
numbers and collections in a type-independent manner. Clojure's immutable data
structures define equals() (and thus =) as a value, not an identity,
comparison."
{:inline (fn [x y] `(. glojure.lang.Util equiv ~x ~y))
{:inline (fn [x y] `(. github.com$glojurelang$glojure$pkg$lang.Equiv ~x ~y))
:inline-arities #{2}
:added "1.0"}
([x] true)
Expand All @@ -797,7 +797,7 @@
x.equals(y) except it also works for nil. Boxed numbers must have
same type. Clojure's immutable data structures define equals() (and
thus =) as a value, not an identity, comparison."
{:inline (fn [x y] `(. glojure.lang.Util equals ~x ~y))
{:inline (fn [x y] `(. github.com$glojurelang$glojure$pkg$lang.Equals ~x ~y))
:inline-arities #{2}
:added "1.0"}
([x] true)
Expand Down Expand Up @@ -828,9 +828,9 @@
compares numbers and collections in a type-independent manner. x
must implement Comparable"
{
:inline (fn [x y] `(. glojure.lang.Util compare ~x ~y))
:inline (fn [x y] `(. github.com$glojurelang$glojure$pkg$lang.Compare ~x ~y))
:added "1.0"}
[x y] (. glojure.lang.Util (compare x y)))
[x y] (github.com$glojurelang$glojure$pkg$lang.Compare x y))

(defmacro and
"Evaluates exprs one at a time, from left to right. If a form
Expand Down Expand Up @@ -3087,10 +3087,10 @@
:static true}
([coll]
(sort compare coll))
([^java.util.Comparator comp coll]
([^java.util.Comparator comparator coll]
(if (seq coll)
(let [a (to-array coll)]
(. java.util.Arrays (sort a comp))
(sort.Slice a (comp neg? comparator))
(with-meta (seq a) (meta coll)))
())))

Expand Down
1 change: 1 addition & 0 deletions scripts/rewrite-core/rewrite.clj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
(sexpr-replace '(clojure.lang.Range/create start end step)
'(github.com$glojurelang$glojure$pkg$lang.NewRange start end step))

(sexpr-replace '(. java.util.Arrays (sort a comp)) '(sort.Slice(a, comp)))

(sexpr-replace '(. clojure.lang.PersistentHashMap (create keyvals))
'(github.com$glojurelang$glojure$pkg$lang.CreatePersistentHashMap keyvals))
Expand Down
8 changes: 8 additions & 0 deletions test/glojure/test_glojure/sort.glj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns glojure.test-glojure.sort
(:use glojure.test))

(deftest Sort-Vector
(is (= [1 2 3] (sort [1 3 2]))))

(deftest Sort-Arrays
(is (= '(1 2 3) (sort '(1 3 2)))))