Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Move LazyList.range implementation closer to other overrides
  • Loading branch information
BalmungSan committed Apr 30, 2024
commit f7745a48f0f71c71ed9558e9ed32164aec30fc53
21 changes: 10 additions & 11 deletions src/library/scala/collection/immutable/LazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -968,17 +968,6 @@ final class LazyList[+A] private(private[this] var lazyState: () => LazyList.Sta
*/
@SerialVersionUID(3L)
object LazyList extends SeqFactory[LazyList] {
// Override SeqFactory methods.
override def range[A: Integral](start: A, end: A): LazyList[A] =
range(start, end, step = Integral[A].one)

override def range[A](start: A, end: A, step: A)(implicit ev: Integral[A]): LazyList[A] =
newLL(rangeImpl(start, end, step))

private[this] def rangeImpl[A](start: A, end: A, step: A)(implicit ev: Integral[A]): State[A] =
if (ev.lt(start, end)) sCons(start, newLL(rangeImpl(ev.plus(start, step), end, step)))
else State.Empty

// Eagerly evaluate cached empty instance
private[this] val _empty = newLL(State.Empty).force

Expand Down Expand Up @@ -1241,6 +1230,16 @@ object LazyList extends SeqFactory[LazyList] {
at(0)
}

override def range[A: Integral](start: A, end: A): LazyList[A] =
range(start, end, step = Integral[A].one)
Comment thread
BalmungSan marked this conversation as resolved.
Outdated

override def range[A](start: A, end: A, step: A)(implicit ev: Integral[A]): LazyList[A] =
newLL(rangeImpl(start, end, step))
Comment thread
BalmungSan marked this conversation as resolved.
Outdated

private[this] def rangeImpl[A](start: A, end: A, step: A)(implicit ev: Integral[A]): State[A] =
if (ev.lt(start, end)) sCons(start, newLL(rangeImpl(ev.plus(start, step), end, step)))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm, wait, this does not allow reversed ranges, does it? Like LazyList.range(start = 100, end = 0, step = -2)
I guess rather than check for lt we have to check for !ev.equiv(start, end)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this made me think. This could be a good way to test this without a CPU trap. We just request the reversed range and then we can indeed ask for the head and check it is bigger than Int.MaxValue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, the check is incorrect, hmm. probably need to check the sign or something as well. I'll have another look shortly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my thought is to check the sign of step in the 3 parameter overload of range, and then pass it as a Boolean parameter (probably named positiveStep) to rangeImpl, thus checking the condition only once rather than for each element

Copy link
Copy Markdown
Contributor Author

@BalmungSan BalmungSan Apr 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed an implementation that allows for reverse ranges.
Let me know what you think about it.

else State.Empty

// significantly simpler than the iterator returned by Iterator.unfold
override def unfold[A, S](init: S)(f: S => Option[(A, S)]): LazyList[A] =
newLL {
Expand Down