Skip to content

Instantly share code, notes, and snippets.

View andrewharmellaw's full-sized avatar

Andrew Harmel-Law andrewharmellaw

View GitHub Profile
@andrewharmellaw
andrewharmellaw / function_type_alias_2.scala
Created December 23, 2015 07:29
The function type alias itself
type Rand[+A] = RNG => (A, RNG)
@andrewharmellaw
andrewharmellaw / function_type_alias_1.scala
Created December 23, 2015 07:28
The function to alias - nextInt
def nextInt: (Int, RNG) = {
val newSeed = (seed * 0x5DEECE66DL + 0xBL) & 0xFFFFFFFFFFFFL
val nextRNG = SimpleRNG(newSeed)
val n = (newSeed >>> 16).toInt
(n, nextRNG)
}
length(List (5,6,7))
foldRight(5 :: 6 :: 7 :: Nil, 0) ((_, acc) => acc + 1)
f(5, foldRight(6 :: 7 :: Nil, 0) ((_, acc) => acc + 1))
(foldRight(6 :: 7 :: Nil, 0) ((_, acc) => acc + 1)) + 1
(f(6, foldRight(7 :: Nil, 0)) ((_, acc) => acc + 1))) + 1
((foldRight(7 :: Nil, 0) ((_, acc) => acc + 1)) + 1) + 1
(((0 + 1) + 1) + 1)
foldRight(Cons(1, Cons(2, Cons(3, Nil))), Nil:List[Int]) (Cons(_,_))
Cons(1, foldRight(Cons(2, Cons(3, Nil)), Nil:List[Int]) (Cons(_,_)))
Cons(1, Cons(2, foldRight(Cons(3, Nil), Nil:List[Int]) (Cons(_,_))))
Cons(1, Cons(2, Cons(3, foldRight(Nil, Nil:List[Int]) (Cons(_,_)))))
Cons(1, Cons(2, Cons(3, Nil)))
List(1,2,3,Nil)
foldLeft(List(10.0,23.0,96.0), 1.0) (_ * _)
foldLeft(10.0 :: 23.0 :: 96.0 :: Nil, 1.0) (_ * _)
foldLeft(23.0 :: 96.0 :: Nil, (1.0 * 10.0)) (_ * _)
foldLeft(96.0 :: Nil, ((1.0 * 10.0) * 23.0)) (_ * _)
foldLeft(Nil, (((1.0 * 10.0) * 23.0) * 96.0)) (_ * _)
(((1.0 * 10.0) * 23.0) * 96.0)
foldLeft(List(1,2,3), 0) (_ + _)
foldLeft(1 :: 2 :: 3 :: Nil, 0) (_ + _)
foldLeft(2 :: 3 :: Nil, (0 + 1)) (_ + _)
foldLeft(3 :: Nil, ((0 + 1) + 2)) (_ + _)
foldLeft(Nil, (((0 + 1) + 2) + 3)) (_ + _)
(((0 + 1) + 2) + 3)
foldLeft(List(1,2,3), 0) (_ + _)
foldLeft(1 :: 2 :: 3 :: Nil, 0) (_ + _)
foldLeft(2 :: 3 :: Nil, (0 + 1)) (_ + _)
foldLeft(3 :: Nil, ((0 + 1) + 2)) (_ + _)
foldLeft(Nil, (((0 + 1) + 2) + 3)) (_ + _)
foldLeft(List(1,2,3), 0) (_ + _)
foldLeft(1 :: 2 :: 3 :: Nil, 0) (_ + _)
foldLeft(2 :: 3 :: Nil, f(0, 1)) (_ + _)
foldLeft(2 :: 3 :: Nil, (0 + 1)) (_ + _)
foldLeft(3 :: Nil, ((0 + 1) + 2)) (_ + _)
foldLeft(2 :: 3 :: Nil, (0 + 1)) (_ + _)
foldLeft(List(1,2,3), 0) (_ + _) // step 1 - where we start
foldLeft(1 :: 2 :: 3 :: Nil, 0) (_ + _) // micro-step 1(a)
foldLeft(2 :: 3 :: Nil, f(0, 1)) (_ + _) // micro-step 1(b)
foldLeft(2 :: 3 :: Nil, (0 + 1)) (_ + _) // step 2 - where we end up