How to design state which is used in a Map

How to design state which is used in a Map

Here is a dumbed down example of what I am trying to achieve. Let's say we have some sort of application coming from a user. This application can be in different states.

Let's say that our Application can be in these different states:

Submitted | Accepted | Confirmed | Processed | Finished 

We can think of Application as a sealed trait and the states are all case classes which have some different information (compared to each other)

Let's say that we have some sort of state machine which describes the workflow of our application processing. There is some initial state which has Map[UserId, Submitted]. Here we don't use Application but restrict ourselves to Submitted type only, because well in our initial state there are no other possible types of application.

And now for the actual question. Let's say that we validate submitted applications and change our state to ConfirmingApplications where we have Map[UserId, Accepted], but the problem here is that we start our state with applications being Accepted but we don't move to the next (ProcessingApplications) until all applications are Confirmed.

How can I approach the design of this case? First thing that I thought was about having two different maps in ConfirmingApplications state where one map is our input map Map[UserId, Accepted] and the other one is Map[UserId, Confirmed]. It seems ok in this small example but what I have is a bigger amount of application types and in one particular state the type of application can change 4 times, should I create four different maps for that?

The other approach is to bring in some other traits which will somehow mark those application types which are allowed to be in a particular state. For example in our ConfirmingApplications state we could have Map[UserId, AcceptedOrConfrimed] where trait AcceptedOrConfirmed is extended by both Accepting and Confirmed application types.

What do you think about these design approaches. Is there any other way to design it? Something better? Something on the same level? Something more type safe?

Submitted December 16, 2017 at 06:44PM by faorien
via reddit


Is there much difference between 2nd and 3rd edition of “Programming in Scala” book from Martin Odersky?

Is there much difference between 2nd and 3rd edition of “Programming in Scala” book from Martin Odersky?

I have the second edition, but I don't know if the new version has new and important features of the language. I'm a javascript dev and I want to move to a static typed language. Thanks!

Submitted December 16, 2017 at 05:11AM by ezeed87
via reddit

Anyone familiar with Scalamock who could possibly explain this error to me?

Anyone familiar with Scalamock who could possibly explain this error to me?

Hey all. So I was working on a personal project earlier today and writing some unit tests for a Service class that I have. Essentially the Service calls a Connector, which then calls an API, and information is passed back along the chain. As I was just unit testing the service, I mocked the connector out, and my code compiles and seems fine. I wrote one test just to test the happy path but I'm getting the following error:

java.lang.ClassCastException: org.scalamock.function.MockFunction2 cannot be cast to org.scalamock.function.MockFunction1 

The only thing I am mocking in my test is this:

val connector: TheConnector = mock[TheConnector] 

And the only function I am calling is:

(connector.myFunction(_: Int)).expects(*).returns(Future.successful("hello")) 

Where myFunction takes in one parameter of type Int and returns a Future[String].

I realise I could have provided a lot more information detailing how the rest of my code looks, but as I am fairly new to Scala I'm trying to learn and troubleshoot as much as I can myself without getting other people to write my code for me, so if anyone could tell me what this type of error pertains to then that would be really helpful as I've spent three hours trying to get to the bottom of it with no luck.

Thanks a lot and I hope I haven't annoyed anyone by posting such a small amount of code. I'm not expecting a concrete solution or anything like that, but maybe just a little guidance and explanation of the error would be helpful. 🙂

Submitted December 16, 2017 at 01:53AM by Panichord
via reddit

Simple Projects to Learn Scala.

Simple Projects to Learn Scala.

Does anyone know here simple projects to learn Scala and programming in general?

I mostly see libraries as example to learn Scala as first language.

Can anyone provide links (ex. simple management systems, web apps that may use both OOPS and FP ) to learn more about Scala before jumping to libraries.
Github Search couldn't help me with my requirement.

They are either too simple (like play templates) or too complex libraries for a learner. Need something in between.Thanks!

Submitted December 15, 2017 at 07:47PM by sanketpatil
via reddit