Skip to content

Commit 24a46ff

Browse files
committed
Fixes for nth
Signed-off-by: James Hamlin <jfhamlin@gmail.com>
1 parent 8490b62 commit 24a46ff

File tree

1 file changed

+7
-153
lines changed

1 file changed

+7
-153
lines changed

pkg/lang/iteration.go

Lines changed: 7 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,15 @@ func MustNth(x interface{}, i int) interface{} {
2323

2424
func Nth(x interface{}, n int) (interface{}, bool) {
2525
switch x := x.(type) {
26+
// Deprecate this
2627
case Nther:
2728
return x.Nth(n)
29+
case Indexed:
30+
val := x.NthDefault(n, notFound)
31+
if val == notFound {
32+
return nil, false
33+
}
34+
return val, true
2835
case ISeq:
2936
x = Seq(x)
3037
for i := 0; i <= n; i++ {
@@ -61,156 +68,3 @@ func Nth(x interface{}, n int) (interface{}, bool) {
6168

6269
return nil, false
6370
}
64-
65-
// // NewIterator returns a lazy sequence of x, f(x), f(f(x)), ....
66-
// func NewIterator(f func(interface{}) interface{}, x interface{}) ISeq {
67-
// return iterator{f: f, x: x}
68-
// }
69-
70-
// type iterator struct {
71-
// f func(interface{}) interface{}
72-
// x interface{}
73-
// }
74-
75-
// func (i iterator) xxx_sequential() {}
76-
77-
// func (i iterator) Seq() ISeq {
78-
// return i
79-
// }
80-
81-
// func (i iterator) First() interface{} {
82-
// return i.x
83-
// }
84-
85-
// func (i iterator) Next() ISeq {
86-
// return NewIterator(i.f, i.f(i.x))
87-
// }
88-
89-
// func (i iterator) More() ISeq {
90-
// nxt := i.Next()
91-
// if nxt == nil {
92-
// return emptyList
93-
// }
94-
// return nxt
95-
// }
96-
97-
// // NewValueIterator returns a lazy sequence of the values of x.
98-
// func NewVectorIterator(x IPersistentVector, start, step int) ISeq {
99-
// if x.Count() == 0 {
100-
// return emptyList
101-
// }
102-
// return vectorIterator{v: x, start: start, step: step}
103-
// }
104-
105-
// type vectorIterator struct {
106-
// v IPersistentVector
107-
// start int
108-
// step int
109-
// }
110-
111-
// func (it vectorIterator) xxx_sequential() {}
112-
113-
// func (it vectorIterator) Seq() ISeq {
114-
// return it
115-
// }
116-
117-
// func (it vectorIterator) First() interface{} {
118-
// return it.v.Nth(it.start)
119-
// }
120-
121-
// func (it vectorIterator) Next() ISeq {
122-
// next := it.start + it.step
123-
// if next >= it.v.Count() || next < 0 {
124-
// return nil
125-
// }
126-
// return &vectorIterator{v: it.v, start: next, step: it.step}
127-
// }
128-
129-
// func (it vectorIterator) More() ISeq {
130-
// nxt := it.Next()
131-
// if nxt == nil {
132-
// return emptyList
133-
// }
134-
// return nxt
135-
// }
136-
137-
// // NewConcatIterator returns a sequence concatenating the given
138-
// // sequences.
139-
// func NewConcatIterator(colls ...interface{}) ISeq {
140-
// var it *concatIterator
141-
// for i := len(colls) - 1; i >= 0; i-- {
142-
// iseq := Seq(colls[i])
143-
// if iseq == nil {
144-
// continue
145-
// }
146-
// it = &concatIterator{seq: iseq, next: it}
147-
// }
148-
// if it == nil {
149-
// return emptyList
150-
// }
151-
// return it
152-
// }
153-
154-
// type concatIterator struct {
155-
// seq ISeq
156-
// next *concatIterator
157-
// }
158-
159-
// func (i *concatIterator) xxx_sequential() {}
160-
161-
// func (i *concatIterator) Seq() ISeq {
162-
// return i
163-
// }
164-
165-
// func (i *concatIterator) First() interface{} {
166-
// return i.seq.First()
167-
// }
168-
169-
// func (i *concatIterator) Next() ISeq {
170-
// i = &concatIterator{seq: i.seq.Next(), next: i.next}
171-
// for i.seq == nil {
172-
// i = i.next
173-
// if i == nil {
174-
// return nil
175-
// }
176-
// }
177-
// return i
178-
// }
179-
180-
// func (i *concatIterator) More() ISeq {
181-
// nxt := i.Next()
182-
// if nxt == nil {
183-
// return emptyList
184-
// }
185-
// return nxt
186-
// }
187-
188-
// ////////////////////////////////////////////////////////////////////////////////
189-
190-
// func chunkIteratorSeq(iter *reflect.MapIter) ISeq {
191-
// const chunkSize = 32
192-
193-
// return NewLazySeq(func() interface{} {
194-
// chunk := make([]interface{}, 0, chunkSize)
195-
// exhausted := false
196-
// for n := 0; n < chunkSize; n++ {
197-
// chunk = append(chunk, NewMapEntry(iter.Key().Interface(), iter.Value().Interface()))
198-
// if !iter.Next() {
199-
// exhausted = true
200-
// break
201-
// }
202-
// }
203-
// if exhausted {
204-
// return NewChunkedCons(NewSliceChunk(chunk), nil)
205-
// }
206-
// return NewChunkedCons(NewSliceChunk(chunk), chunkIteratorSeq(iter))
207-
// })
208-
// }
209-
210-
// func NewGoMapSeq(x interface{}) ISeq {
211-
// rng := reflect.ValueOf(x).MapRange()
212-
// if !rng.Next() {
213-
// return nil
214-
// }
215-
// return chunkIteratorSeq(rng)
216-
// }

0 commit comments

Comments
 (0)