Skip to content

Instantly share code, notes, and snippets.

View pavithranrao's full-sized avatar

Pavithran Ramachandran pavithranrao

View GitHub Profile
@pavithranrao
pavithranrao / RecursiveStreams.scala
Created October 11, 2017 19:03 — forked from jeffreyolchovy/RecursiveStreams.scala
Recursive Streams in Scala
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)))
@pavithranrao
pavithranrao / Node.scala
Created February 17, 2018 23:36 — forked from pathikrit/Node.scala
Reverse a LinkedList in Scala
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)}")
@pavithranrao
pavithranrao / Node.scala
Created February 17, 2018 23:36 — forked from pathikrit/Node.scala
Reverse a LinkedList in Scala
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)}")