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

Empty chaining #67

Closed
meian opened this issue Jan 27, 2022 · 1 comment · Fixed by #69
Closed

Empty chaining #67

meian opened this issue Jan 27, 2022 · 1 comment · Fixed by #69
Labels
improvement Some improvement

Comments

@meian
Copy link
Owner

meian commented Jan 27, 2022

gcf/reverse.go

Lines 18 to 20 in de7836c

if itb == nil {
return empty[T]()
}

この書き方をしている場合、引数の itb にnilを渡すと emptyIterable を返すので処理が軽いが、その次に他の関数を適用したときに empty ではなくなるので空の時にも処理が軽くならない

itb := gcf.Filter[int](nil, func(v int) bool { return v > 10})  // この時点では itb は empty
itb = gcf.Filter(itb, func(v int) bool { return v%2 == 0})      // この時点では itb は filterIterable だが要素は1件も取得できない

そのため以下のように修正することで emptyIterable をチェーンできるようにする

func SomeFunc[T any](itb Iterable[T]) Iterable[T] {
    if isEmpty(itb) {
        return orEmpty(itb)
    }
    ....
}
  • isEmpty
    • itb がnilか emptyIterable であればtrue
  • orEmtpy
    • itb がnilなら emptyIterable、nilでなければ emptyIterable を作って返す

こうすることで empty に対する処理に対して常に empty を返せるので、空の処理の場合に負荷がかからなくなる

itb := gcf.Filter[int](nil, func(v int) bool { return v > 10})  // この時点では itb は empty
itb = gcf.Filter(itb, func(v int) bool { return v%2 == 0})      // empty に対する処理なので empty になる
@meian meian added the improvement Some improvement label Jan 27, 2022
@meian meian changed the title Empty chain Empty chaining Jan 27, 2022
meian added a commit that referenced this issue Jan 29, 2022
meian added a commit that referenced this issue Jan 29, 2022
meian added a commit that referenced this issue Jan 29, 2022
meian added a commit that referenced this issue Jan 29, 2022
@meian
Copy link
Owner Author

meian commented Jan 29, 2022

負荷がかからないことを確認するためのテストを用意するかな

meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
@meian meian closed this as completed in #69 Jan 30, 2022
meian added a commit that referenced this issue Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Some improvement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant