This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Node(value: Int, next: Option[Node]) | |
def reverse(node: Node, prev: Option[Node] = None): Node = { | |
val reversed = node.copy(next = prev) | |
node.next map {reverse(_, Some(reversed))} getOrElse reversed | |
} | |
/****************************************************************/ | |
val one = Node(1,Some(Node(2,Some(Node(3,None))))) | |
println(s"$one\n${reverse(one)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Node(value: Int, next: Option[Node]) | |
def reverse(node: Node, prev: Option[Node] = None): Node = { | |
val reversed = node.copy(next = prev) | |
node.next map {reverse(_, Some(reversed))} getOrElse reversed | |
} | |
/****************************************************************/ | |
val one = Node(1,Some(Node(2,Some(Node(3,None))))) | |
println(s"$one\n${reverse(one)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.math.{BigInt, BigDecimal} | |
object RecursiveStreams | |
{ | |
// natural numbers | |
lazy val N: Stream[BigInt] = Stream.cons(BigInt(1), N.map(_ + 1)) | |
// fibonacci series | |
lazy val fib: Stream[BigInt] = Stream.cons(BigInt(0), Stream.cons(BigInt(1), fib.zip(fib.tail).map(a => a._1 + a._2))) |