Compare commits
No commits in common. "e58c399bad764ef6e8a59500dfe24af3e15da1f0" and "d8b22157c56f5743b8446abee6a03e04ec196394" have entirely different histories.
e58c399bad
...
d8b22157c5
@ -1,35 +0,0 @@
|
|||||||
trait Stack[+A] {
|
|
||||||
def push[B >: A](elem: B) : Stack[B] = ElemStack(elem, this)
|
|
||||||
def top: A
|
|
||||||
def pop: Stack[A]
|
|
||||||
}
|
|
||||||
|
|
||||||
case class EmptyStack[+A]() extends Stack[A] {
|
|
||||||
override def top: A = throw new IndexOutOfBoundsException("Stack is empty")
|
|
||||||
override def pop: Stack[A] = this
|
|
||||||
}
|
|
||||||
|
|
||||||
case class ElemStack[+A](elmt: A, base: Stack[A]) extends Stack[A] {
|
|
||||||
override def top: A = elmt
|
|
||||||
override def pop: Stack[A] = base
|
|
||||||
override def toString: String = elmt.toString + "," + base.toString
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construction, pop and toString
|
|
||||||
val a = EmptyStack().push("hello").push("world").push("it's fun").pop
|
|
||||||
assert(a.toString() == "world,hello,EmptyStack()")
|
|
||||||
|
|
||||||
// Getting top
|
|
||||||
val b = EmptyStack().push(1).push(3)
|
|
||||||
assert(b.top == 3)
|
|
||||||
|
|
||||||
// Variance checks
|
|
||||||
class Foo
|
|
||||||
class Bar extends Foo
|
|
||||||
val c: Stack[Bar] = EmptyStack().push(new Bar()).push(new Bar())
|
|
||||||
assert(c.top.isInstanceOf[Bar] == true)
|
|
||||||
assert(c.top.isInstanceOf[Foo] == true)
|
|
||||||
|
|
||||||
// Variance check 2
|
|
||||||
val d: Stack[Foo] = EmptyStack().push(new Bar()).push(new Bar())
|
|
||||||
assert(d.top.isInstanceOf[Foo])
|
|
@ -1,9 +0,0 @@
|
|||||||
def intsFrom(n: Int): LazyList[Int] = {
|
|
||||||
n #:: intsFrom(n + 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
def primeNumbers(list: LazyList[Int]): LazyList[Int] = {
|
|
||||||
list.head #:: primeNumbers(list.filter(n => n % list.head != 0))
|
|
||||||
}
|
|
||||||
val ints: LazyList[Int] = intsFrom(2)
|
|
||||||
primeNumbers(ints).take(10).toList
|
|
@ -1,12 +0,0 @@
|
|||||||
def addStream(s1: LazyList[Int], s2: LazyList[Int]): LazyList[Int] = {
|
|
||||||
s1 zip s2 map (p => p._1 + p._2)
|
|
||||||
}
|
|
||||||
|
|
||||||
def fibonacci(): LazyList[Int] = {
|
|
||||||
0 #:: addStream(
|
|
||||||
1 #:: fibonacci(),
|
|
||||||
fibonacci()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fibonacci().take(10).toList
|
|
@ -1,19 +0,0 @@
|
|||||||
|
|
||||||
def THRESHOLD: Double = 0.0001
|
|
||||||
|
|
||||||
def sqr(x: Double): Double = x * x
|
|
||||||
|
|
||||||
def sqrt_stream(value: Double): LazyList[Double] = {
|
|
||||||
def helper(target: Double, approx: Double): LazyList[Double] = {
|
|
||||||
approx #:: helper(target, approx - (sqr(approx) - target) / (2 * approx))
|
|
||||||
}
|
|
||||||
helper(value, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
sqrt_stream(2).take(10).toList
|
|
||||||
|
|
||||||
def threshold(list: LazyList[Double], thresh: Double) = {
|
|
||||||
list.zip(list.drop(1)).filter(p => math.abs(p._2 - p._1) < thresh).head._2
|
|
||||||
}
|
|
||||||
|
|
||||||
threshold(sqrt_stream(2), 1e-15)
|
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
1
|
|
||||||
1 1
|
|
||||||
2 1
|
|
||||||
1 2 1 1
|
|
||||||
1 1 1 2 2 1
|
|
||||||
3 1 2 2 1 1
|
|
||||||
Next :
|
|
||||||
1 3 1 1 2 2 2 1
|
|
||||||
*/
|
|
||||||
|
|
||||||
def nextLine(current: List[Int]) : List[Int] = {
|
|
||||||
current.foldRight(List.empty[(Int, Int)])((x, acc) => {
|
|
||||||
(x, acc) match {
|
|
||||||
case (a, p :: rest) if a == p._2 => (p._1 + 1, p._2) :: rest
|
|
||||||
case _ => (1, x) :: acc
|
|
||||||
}
|
|
||||||
}).flatten(p => List(p._1, p._2))
|
|
||||||
}
|
|
||||||
|
|
||||||
def makeSequence(start: List[Int]): LazyList[List[Int]] = {
|
|
||||||
start #:: makeSequence(nextLine(start))
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy val sequence: LazyList[List[Int]] = makeSequence(List(1))
|
|
||||||
sequence.take(7).toList
|
|
Loading…
x
Reference in New Issue
Block a user