Skip to content

Commit

Permalink
Fix error in scanLeft
Browse files Browse the repository at this point in the history
Closes #256
  • Loading branch information
noelwelsh committed Dec 20, 2024
1 parent 24a3bed commit dd0f9ca
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/pages/codata/structural.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ trait Stream[A] {
def scanLeft[B](zero: B)(f: (B, A) => B): Stream[B] = {
val self = this
new Stream[B] {
def head: B = f(zero, self.head)
def head: B = zero

def tail: Stream[B] =
self.tail.scanLeft(this.head)(f)
self.tail.scanLeft(f(zero, self.head))(f)
}
}
}
Expand Down Expand Up @@ -380,10 +380,10 @@ trait Stream[A] {
def scanLeft[B](zero: B)(f: (B, A) => B): Stream[B] = {
val self = this
new Stream[B] {
def head: B = f(zero, self.head)
def head: B = zero

def tail: Stream[B] =
self.tail.scanLeft(this.head)(f)
self.tail.scanLeft(f(zero, self.head))(f)
}
}

Expand Down Expand Up @@ -475,10 +475,10 @@ trait Stream[A] {
def scanLeft[B](zero: B)(f: (B, A) => B): Stream[B] = {
val self = this
new Stream[B] {
def head: B = f(zero, self.head)
def head: B = zero

def tail: Stream[B] =
self.tail.scanLeft(this.head)(f)
self.tail.scanLeft(f(zero, self.head))(f)
}
}

Expand Down Expand Up @@ -555,7 +555,7 @@ def filter(pred: A => Boolean): Stream[A] = {
}
```

We know that delaying the computation until the method is called is important, because that is how we can handle infinite and self-referential data. However we don't need to redo this computation on succesive calls. We can instead cache the result from the first call and use that next time.
We know that delaying the computation until the method is called is important, because that is how we can handle infinite and self-referential data. However we don't need to redo this computation on successive calls. We can instead cache the result from the first call and use that next time.
Scala makes this easy with `lazy val`, which is a `val` that is not computed until its first call.
Additionally, Scala's use of the *uniform access principle* means we can implement a method with no parameters using a `lazy val`.
Here's a quick example demonstrating it in use.
Expand Down

0 comments on commit dd0f9ca

Please sign in to comment.