Skip to content

Commit f0cde8c

Browse files
committed
Fix for keys/vals and keyword namespace
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent c469359 commit f0cde8c

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

pkg/lang/interfaces.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,12 +486,14 @@ func Count(coll any) int {
486486
return count
487487
}
488488

489-
func Keys(m Associative) ISeq {
490-
return NewMapKeySeq(Seq(m))
489+
func Keys(x any) ISeq {
490+
// TODO: optimize for map case
491+
return NewMapKeySeq(Seq(x))
491492
}
492493

493-
func Vals(m Associative) ISeq {
494-
return NewMapValSeq(Seq(m))
494+
func Vals(x any) ISeq {
495+
// TODO: optimize for map case
496+
return NewMapValSeq(Seq(x))
495497
}
496498

497499
func Subvec(v IPersistentVector, start, end int) IPersistentVector {

pkg/lang/keyword.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,14 @@ func (k Keyword) value() string {
4343
return k.kw.Get().(string)
4444
}
4545

46-
func (k Keyword) Namespace() string {
47-
// Return the namespace of the keyword, or the empty string if it
48-
// doesn't have one.
46+
func (k Keyword) Namespace() any {
47+
// Return the namespace of the keyword, or nil if it doesn't have
48+
// one.
49+
// TODO: support both nil and empty string namespace as clojure does
4950
if i := strings.Index(k.value(), "/"); i != -1 {
5051
return k.value()[:i]
5152
}
52-
return ""
53+
return nil
5354
}
5455

5556
func (k Keyword) Name() string {
@@ -106,14 +107,14 @@ func (k Keyword) Compare(other any) int {
106107
if s == os {
107108
return 0
108109
}
109-
ns := k.Namespace()
110-
if ns == "" {
111-
if otherKw.Namespace() != "" {
110+
ns, ok := k.Namespace().(string)
111+
if !ok {
112+
if otherKw.Namespace() != nil {
112113
return -1
113114
}
114115
} else {
115-
ons := otherKw.Namespace()
116-
if ons == "" {
116+
ons, ok := otherKw.Namespace().(string)
117+
if !ok {
117118
return 1
118119
}
119120
nsc := strings.Compare(ns, ons)

pkg/lang/persistentarraymap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ func (s *MapSeq) Drop(n int) Sequential {
490490
////////////////////////////////////////////////////////////////////////////////
491491

492492
func NewMapKeySeq(s ISeq) ISeq {
493-
if s == nil {
493+
if IsNil(s) {
494494
return nil
495495
}
496496
return &MapKeySeq{s: s}
@@ -566,7 +566,7 @@ func (s *MapKeySeq) HashEq() uint32 {
566566
////////////////////////////////////////////////////////////////////////////////
567567

568568
func NewMapValSeq(s ISeq) ISeq {
569-
if s == nil {
569+
if IsNil(s) {
570570
return nil
571571
}
572572
return &MapValSeq{s: s}

pkg/lang/stringseq.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ func NewStringSeq(s string, i int) *StringSeq {
2121
if len(s) == 0 {
2222
return nil
2323
}
24-
return &StringSeq{str: []rune(s), i: i}
24+
runes := []rune(s)
25+
if i >= len(runes) {
26+
return nil
27+
}
28+
return &StringSeq{str: runes, i: i}
2529
}
2630

2731
func newStringSeq(s []rune, i int) *StringSeq {
28-
if len(s) == 0 {
32+
if len(s) == 0 || i >= len(s) {
2933
return nil
3034
}
3135
return &StringSeq{str: s, i: i}

0 commit comments

Comments
 (0)