Skip to content

Commit 8464f21

Browse files
committed
Add more sort-by tests
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent 60d90fa commit 8464f21

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

pkg/lang/slices.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ func ToSlice(x any) []any {
1515
if IsNil(x) {
1616
return []any{}
1717
}
18-
18+
1919
// Handle []any - return as-is
2020
if slice, ok := x.([]any); ok {
2121
return slice
2222
}
23-
23+
2424
// Handle IPersistentVector
2525
if vec, ok := x.(IPersistentVector); ok {
2626
count := vec.Count()
@@ -30,7 +30,7 @@ func ToSlice(x any) []any {
3030
}
3131
return res
3232
}
33-
33+
3434
// Handle IPersistentMap - convert to array of MapEntry objects
3535
if m, ok := x.(IPersistentMap); ok {
3636
seq := m.Seq()
@@ -41,7 +41,7 @@ func ToSlice(x any) []any {
4141
}
4242
return res
4343
}
44-
44+
4545
// Handle Set - convert to array of values
4646
if s, ok := x.(*Set); ok {
4747
seq := s.Seq()
@@ -52,7 +52,7 @@ func ToSlice(x any) []any {
5252
}
5353
return res
5454
}
55-
55+
5656
// Handle string - convert to character array
5757
if s, ok := x.(string); ok {
5858
runes := []rune(s) // Important: use runes for proper Unicode handling
@@ -62,7 +62,7 @@ func ToSlice(x any) []any {
6262
}
6363
return res
6464
}
65-
65+
6666
// Handle ISeq
6767
if s, ok := x.(ISeq); ok {
6868
res := make([]interface{}, 0, Count(x))
@@ -71,7 +71,7 @@ func ToSlice(x any) []any {
7171
}
7272
return res
7373
}
74-
74+
7575
// Handle reflection-based slice/array
7676
xVal := reflect.ValueOf(x)
7777
if xVal.Kind() == reflect.Slice || xVal.Kind() == reflect.Array {
@@ -81,7 +81,7 @@ func ToSlice(x any) []any {
8181
}
8282
return res
8383
}
84-
84+
8585
// Error with Clojure-style message
8686
panic(NewIllegalArgumentError(fmt.Sprintf("Unable to convert: %T to Object[]", x)))
8787
}

pkg/lang/subvector.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,17 @@ func (v *SubVector) Compare(other any) int {
171171
if !ok {
172172
panic(NewIllegalArgumentError(fmt.Sprintf("Cannot compare SubVector with %T", other)))
173173
}
174-
174+
175175
myCount := v.Count()
176176
otherCount := otherVec.Count()
177-
177+
178178
// Compare lengths first
179179
if myCount < otherCount {
180180
return -1
181181
} else if myCount > otherCount {
182182
return 1
183183
}
184-
184+
185185
// Compare element by element
186186
for i := 0; i < myCount; i++ {
187187
cmp := Compare(v.Nth(i), otherVec.Nth(i))

test/glojure/test_glojure/sort.glj

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
(is (= '(1 2 3 4 5) (sort [3 1 4 2 5])))
1212
(is (= '(1 1 3 4 5) (sort [3 1 4 1 5]))) ; duplicates preserved
1313
(is (= '(1.0 2.5 3 4.7) (sort [4.7 1.0 3 2.5]))) ; mixed numeric types
14+
(is (= '(-5 0 1.5 2 3.14 10) (sort [3.14 2 1.5 10 -5 0])))
1415

1516
;; Strings
1617
(is (= '("apple" "banana" "cherry") (sort ["cherry" "apple" "banana"])))
@@ -251,5 +252,40 @@
251252
(is (= -1 (compare 'ns/a 'ns/b)))
252253
(is (= 1 (compare 'ns/b 'ns/a)))))
253254

255+
(deftest sort-by-clojuredocs-examples
256+
(testing "Examples from ClojureDocs sort-by documentation"
257+
258+
(let [words ["banana" "apple" "cherry" "date"]]
259+
(is (= '("date" "apple" "banana" "cherry")
260+
(sort-by count words))))
261+
262+
(let [words ["banana" "apple" "cherry" "date"]]
263+
(is (= (sort-by count > words)
264+
'("banana" "cherry" "apple" "date"))))
265+
266+
(let [people [{:name "Alice" :age 30 :city "NYC"}
267+
{:name "Bob" :age 25 :city "LA"}
268+
{:name "Charlie" :age 35 :city "NYC"}
269+
{:name "David" :age 25 :city "LA"}]]
270+
(is (= (sort-by (juxt :city :age) people)
271+
'({:name "Bob" :age 25 :city "LA"}
272+
{:name "David" :age 25 :city "LA"}
273+
{:name "Alice" :age 30 :city "NYC"}
274+
{:name "Charlie" :age 35 :city "NYC"}))))
275+
276+
(let [numbers [3 1 4 1 5 9 2 6]]
277+
(is (= '(3 9 6 1 4 1 5 2)
278+
(sort-by #(mod % 3) numbers))))
279+
280+
(let [items [nil "hello" 42 :keyword]]
281+
(is (= (sort-by str items)
282+
'(nil 42 :keyword "hello"))))
283+
284+
(is (= (sort-by identity []) '()))
285+
(is (= (sort-by count []) '()))
286+
287+
(is (= (sort-by identity [42]) '(42)))
288+
(is (= (sort-by count ["hello"]) '("hello")))))
289+
254290
;; Run tests
255291
(run-tests)

0 commit comments

Comments
 (0)