-
Notifications
You must be signed in to change notification settings - Fork 105
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The method |
||
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 { | ||
|
||
} |
There was a problem hiding this comment.
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))
.