Skip to content

Added solutions for chapter 1, exercise 1 to 5 and chapter 2 exercise 1 to 3 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/main/scala/org/learningconcurrency/exercises/ch1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.learningconcurrency.exercises

object CH1Solutions extends App{

def compose[A, B, C](g: B => C, f: A => B): A => C = g compose f
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice.
We should also add the alternative solution g(f(x)).

// Alternative solutioin: = x => g(f(x))

def fuse[A, B](a: Option[A], b: Option[B]): Option[(A, B)] = for {
aVal <- a
bVal <- b
} yield (aVal, bVal)

def check2[T](xs: Seq[T])(pred: T => Boolean): Boolean = xs.forall {x =>
try {
pred(x)
} catch {
case _: Exception => false
}
)

def permutations(x: String): Seq[String] = x.permutations.toList
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a correct solution.

However, we would like to encourage the readers to use recursive functions along with for-comprehensions here, so we should add it as an alternative solution.


}

case class Pair[P, Q](first: P, second: Q)
42 changes: 42 additions & 0 deletions src/main/scala/org/learningconcurrency/exercises/ch2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.learningconcurrency.exercises

import org.learningconcurrency.ch2.thread


object CH2Solutions extends App{

def parallel[A, B](a: =>A, b: =>B): (A, B) = {
var aVal: A = null.asInstanceOf[A]
var bVal: B = null.asInstanceOf[B]

val t1 = thread {
aVal = a
log(aVal.toString())
}

val t2 = thread {
bVal = b
log(bVal.toString())
}

t1.join()
t2.join()

(aVal, bVal)
}

def periodically(duration: Long)(f: () => Unit): Unit = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method periodically must start a thread that executes a computation block every duration milliseconds, but the method itself should not block. It should immediately return. Also, the thread that gets started should invoked f indefinitely, not just 3 times.

val worker = new Thread {
while(true){
f()
Thread.sleep(duration)
}
}

worker.setName("Worker")
worker.setDaemon(true)
worker.start()
}
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package org.learningconcurrency






package object exercises {

}