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

Conversation

Mironor
Copy link
Contributor

@Mironor Mironor commented Jan 18, 2015

Given #5 here are some solutions to the exercises solved so far (for ch1 and some of ch2) just to show that there is some progress in the 'exercise' package.
I will submit solutions to following chapters as soon as they are solved.

@Mironor
Copy link
Contributor Author

Mironor commented Jan 18, 2015

Actually, blocked by this line (ch. 2 ex. 4):

Then, implement a producer thread that transfers a range of numbers 0 until 15 to the consumer thread that prints them.

It looks like it's something simple, but I only found Producer/Consumer implementations with Akka so far. Can you give some hint to the problem, please?

@axel22 axel22 mentioned this pull request Jan 19, 2015

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)).

@axel22
Copy link
Member

axel22 commented Jan 19, 2015

I recommend that we keep the exercises for each chapter in separate packages, just like the chapter examples:

package org.learningconcurrency
package exercises
package ch1

The exercises themselves should be in separate files, for example Ex1.scala, Ex2.scala, and so on. This will allow easily finding them, and will unclutter the code, as Ex1.scala can contain multiple solutions for that exercise.

@axel22
Copy link
Member

axel22 commented Jan 19, 2015

(I can also do the refactoring to separate files for you, if you want)

@axel22
Copy link
Member

axel22 commented Jan 19, 2015

Then, implement a producer thread that transfers a range of numbers 0 until 15 to the consumer thread that prints them.

It looks like it's something simple, but I only found Producer/Consumer implementations with Akka so far. Can you give some hint to the problem, please?

Sure! The producer/consumer pattern is not specific to Akka and the actor model. It is a more general pattern in which one computation produces some values, and another computation takes those values and does something with them. Here, the consumer depends on the values created by the producer, and cannot proceed until they become available.

In this exercise, you need to:

  • create a SyncVar
  • create a thread that repetitively puts values into the SyncVar after getting true from isEmpty
  • create a thread that repetitively does the opposite

Which one is the consumer, and which is the producer here?

@Mironor
Copy link
Contributor Author

Mironor commented Jan 19, 2015

Thanks for taking time reviewing the solutions!

I just updated the PR with regards to your comments. If you could refactor the code with separate files, it would be great, this way I could see what exact file structure you have in mind.

axel22 added a commit that referenced this pull request Jan 19, 2015
Added solutions for chapter 1, exercise 1 to 5 and chapter 2 exercise 1 to 3
@axel22 axel22 merged commit c07216a into concurrent-programming-in-scala:master Jan 19, 2015
@Mironor
Copy link
Contributor Author

Mironor commented Jan 19, 2015

Just saw the hint on the exercise 4, that's exactly what I was looking for, thanks!

@axel22
Copy link
Member

axel22 commented Jan 19, 2015

You're welcome, and thank you for contributing! :)

Here's the structure I meant:

c0a4981

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants