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

Unnecessary extra case for takeViaUnfold #682

Open
georgms opened this issue Aug 12, 2023 · 0 comments
Open

Unnecessary extra case for takeViaUnfold #682

georgms opened this issue Aug 12, 2023 · 0 comments

Comments

@georgms
Copy link

georgms commented Aug 12, 2023

The special case for handling n = 1 in the implementation of takeViaUnfold is not necessary:

def takeViaUnfold(n: Int): Stream[A] =
unfold((this,n)) {
case (Cons(h,t), 1) => Some((h(), (empty, 0)))
case (Cons(h,t), n) if n > 1 => Some((h(), (t(), n-1)))
case _ => None
}

It's already covered by the general case:

case (Cons(h,t), n) if n > 1 => Some((h(), (t(), n-1)))

Test:

test("takeUnfold") {
  assertEquals(Stream(1, 2, 3).takeUnfold(0).toList, List.empty)
  assertEquals(Stream(1, 2, 3).takeUnfold(1).toList, List(1))
  assertEquals(Stream(1, 2, 3).takeUnfold(2).toList, List(1, 2))
  assertEquals(Stream(1, 2, 3).takeUnfold(5).toList, List(1, 2, 3))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant