Files
lambda/pkg/iterator/iterator.go
M.V. Hutz da3da70855 refactor: extract shared token package (#46)
## Description

Both the `saccharine` and `lambda` packages need tokenizing and parsing primitives.
This PR extracts shared token infrastructure into a new `pkg/token` package, then wires both languages up to use it.

- Add `pkg/token` with a generic `Token[T]` type, `Scan`, `ScanAtom`, `ScanRune`, `ScanCharacter`, `IsVariable`, `ParseRawToken`, and `ParseList`.
- Refactor `pkg/saccharine` to delegate to `pkg/token`, removing duplicated scanning and parsing helpers.
- Implement `Codec.Decode` for `pkg/lambda` (scanner + parser) using the shared token package.
- Add `iterator.While` for predicate-driven iteration.
- Rename `iterator.Do` to `iterator.Try` to better describe its rollback semantics.

### Decisions

- The `Type` constraint (`comparable` + `Name() string`) keeps the generic token flexible while ensuring every token type can produce readable error messages.
- `iterator.Do` was renamed to `iterator.Try` since it describes a try/rollback operation, not a side-effecting "do".

## Benefits

- Eliminates duplicated token, scanning, and parsing code between languages.
- Enables the `lambda` package to decode (parse) lambda calculus strings, which was previously unimplemented.
- Makes it straightforward to add new languages by reusing `pkg/token` primitives.

## Checklist

- [x] Code follows conventional commit format.
- [x] Branch follows naming convention (`<type>/<description>`). Always use underscores.
- [x] Tests pass (if applicable).
- [ ] Documentation updated (if applicable).

Reviewed-on: #46
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
2026-02-12 00:25:18 +00:00

108 lines
2.6 KiB
Go

// Package iterator defines a generic way to iterator over a slice of data.
package iterator
import "fmt"
// An Iterator traverses over slices.
type Iterator[T any] struct {
items []T
index int
}
// Of creates a new iterator, over a set of defined items.
func Of[T any](items []T) *Iterator[T] {
return &Iterator[T]{items: items, index: 0}
}
// Index returns the current position of the iterator.
func (i Iterator[T]) Index() int {
return i.index
}
// Copy returns a identical clone of the iterator. The underlying data structure
// is not cloned.
func (i Iterator[T]) Copy() *Iterator[T] {
return &Iterator[T]{items: i.items, index: i.index}
}
// Sync returns the iterator to the position of another. It is assumed that the
// iterators both operate on the same set of data.
func (i *Iterator[T]) Sync(o *Iterator[T]) {
i.index = o.index
}
// Get returns the datum at the current position of the iterator.
func (i Iterator[T]) Get() (T, error) {
var null T
if i.Done() {
return null, fmt.Errorf("iterator is exhausted")
}
return i.items[i.index], nil
}
// MustGet is a version of Get, that panics if the datum cannot be returned.
func (i Iterator[T]) MustGet() T {
t, err := i.Get()
if err != nil {
panic(fmt.Errorf("cannot get current token: %w", err))
}
return t
}
// Forward increments the iterator if the iterator is not yet at the end of the
// slice.
func (i *Iterator[T]) Forward() {
if !i.Done() {
i.index++
}
}
// Next attempts to increment the iterator. Returns an error if it cannot be
// incremented.
func (i *Iterator[T]) Next() (T, error) {
item, err := i.Get()
if err == nil {
i.index++
}
return item, err
}
// Back decrements the iterator. If the iterator is already at the beginning of
// the slice, this is a no-op.
func (i *Iterator[T]) Back() {
i.index = max(i.index-1, 0)
}
// Done returns whether the iterator is at the end of the slice or not.
func (i Iterator[T]) Done() bool {
return i.index == len(i.items)
}
// While increments the iterator as long as the current item satisfies the
// predicate. The first item that does not match is left unconsumed.
func (i *Iterator[T]) While(fn func(T) bool) {
for !i.Done() {
if !fn(i.MustGet()) {
return
}
i.Forward()
}
}
// Try attempts to perform an operation using the iterator. If the operation
// succeeds, the iterator is updated. If the operation fails, the iterator is
// rolled back, and an error is returned.
func Try[T any, U any](i *Iterator[T], fn func(i *Iterator[T]) (U, error)) (U, error) {
i2 := i.Copy()
out, err := fn(i2)
if err == nil {
i.Sync(i2)
}
return out, err
}