RN
RN

News

Robbie Nohra
December 12, 2018

I am currently working on a book. Will post updates soon.

Read More

Understanding traits

A brief introduction to traits

Robbie Nohra
November 12, 2018

Consider the following example in Java. Suppose we have an interface Base and two interfaces A and B that extend Base, each with a method m().

public interface Base {

 default void m() {
 System.out.println("Hello from Base");
 }
}

interface A extends Base {

 default void m() {
 System. . . .

Read More

Closures

A brief intro to closures

Robbie Nohra
November 06, 2018

Recall the curry of the addition function

def add(x: Int) = {
 def addX(y: Int) = {
 x + y
 }
 addX _
}

Suppose I call add with the arguments 1 and 2, respectively.

val add1 = add(1)
val add2 = add(2)

If I call each with argument 1 then,

add1(1)
res0: Int = 2
add2(1)
res1: Int . . .

Read More

Weighted interval scheduling

Robbie Nohra
November 05, 2018

Weighted interval scheduling

Read More

The flatMap operation

A brief intro to the flatMap operation

Robbie Nohra
November 04, 2018

The use case for flatMap is when one wishes to compose the map and flatten operations, in that order.

val a = List(List(1, 2), List(3, 4))
val b = a.map((x: List[Int]) => x.map(_ * 2))
val c = b.flatten

c will have the value List(2,4,6,8).

Due to its frequency of use, there is a built-in function in Scala that . . .

Read More

The flatten operation

A brief intro to the flatten operation

Robbie Nohra
November 04, 2018

We have a list of lists.

val a = List(List(1,2),List(3,4),List(5,6))

We want to concatenate those lists into a single list.

We can accomplish this using a foldLeft pattern.

def f(a: List[List[Int]]): List[Int] = a.foldLeft(List[Int]())((flat,x)=> flat ++ x)

Note that List[Int]() represents an empty list. . . .

Read More

Mapping

A brief intro to the map operation

Robbie Nohra
November 04, 2018

We have some collection. A list, say.

val a = List(1,2,3)

We have a function, f.

def f(a: Int): Int = 2*a

We would like to evaluate f at each element of a and assign the return value to the corresponding position of a new list. map accomplishes this.

val b = a.map(f)

The value of b is List(2,4,6).

. . .

Read More

All Categories: probability(1) home

Archive
   Subscribe by email and never miss a post.