diff --git a/concat.go b/concat.go index d2a9af5..0abb899 100644 --- a/concat.go +++ b/concat.go @@ -12,11 +12,13 @@ type concatIterator[T any] struct { } func Concat[T any](itb1 Iterable[T], itb2 Iterable[T]) Iterable[T] { - if itb1 == nil { - itb1 = empty[T]() - } - if itb2 == nil { - itb2 = empty[T]() + switch { + case itb1 == nil && itb2 == nil: + return empty[T]() + case itb2 == nil: + return itb1 + case itb1 == nil: + return itb2 } return &concatIterable[T]{itb1, itb2} } diff --git a/filter.go b/filter.go index c7a5562..8bbdc48 100644 --- a/filter.go +++ b/filter.go @@ -13,12 +13,10 @@ type filterIterator[T any] struct { func Filter[T any](itb Iterable[T], predicate func(v T) bool) Iterable[T] { if itb == nil { - itb = empty[T]() + return empty[T]() } if predicate == nil { - predicate = func(v T) bool { - return true - } + return itb } return &filterIterable[T]{itb, predicate} } diff --git a/map.go b/map.go index dca687a..dd9b11b 100644 --- a/map.go +++ b/map.go @@ -1,29 +1,29 @@ package gcf type mapIterable[T any, R any] struct { - itb Iterable[T] - selector func(T) R + itb Iterable[T] + mapFunc func(T) R } type mapIterator[T any, R any] struct { - it Iterator[T] - selector func(T) R - current R + it Iterator[T] + mapFunc func(T) R + current R } -func Map[T any, R any](itb Iterable[T], f func(T) R) Iterable[R] { +func Map[T any, R any](itb Iterable[T], mapFunc func(T) R) Iterable[R] { if itb == nil { - itb = empty[T]() + return empty[R]() } - if f == nil { + if mapFunc == nil { r := zero[R]() - f = func(v T) R { return r } + mapFunc = func(v T) R { return r } } - return &mapIterable[T, R]{itb, f} + return &mapIterable[T, R]{itb, mapFunc} } func (itb *mapIterable[T, R]) Iterator() Iterator[R] { - return &mapIterator[T, R]{itb.itb.Iterator(), itb.selector, zero[R]()} + return &mapIterator[T, R]{itb.itb.Iterator(), itb.mapFunc, zero[R]()} } func (it *mapIterator[T, R]) MoveNext() bool { @@ -31,7 +31,7 @@ func (it *mapIterator[T, R]) MoveNext() bool { it.current = zero[R]() return false } - it.current = it.selector(it.it.Current()) + it.current = it.mapFunc(it.it.Current()) return true }