Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #33

Merged
merged 4 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
6 changes: 2 additions & 4 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
24 changes: 12 additions & 12 deletions map.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
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 {
if !it.it.MoveNext() {
it.current = zero[R]()
return false
}
it.current = it.selector(it.it.Current())
it.current = it.mapFunc(it.it.Current())
return true
}

Expand Down