Scala is a general purpose programming language principally targeting the Java Virtual Machine. Designed to express common programming patterns in a concise, elegant, and type-safe way, it fuses both imperative and functional programming styles.
3
votes
2answers
92 views
Using actor model, how can one program concurrent portion (critical section) of code as self contained nuggets?
Von neumann architecture allows sequential processing of instructions. So, a single core within a CPU executes instructions sequentially.
Consider, OS providing 1-1 threading model(here) in a multi-...
-1
votes
0answers
44 views
Building Scala “fatjars” with Gradle
JVM (Java + Groovy) developer here, making a foray into Scala.
Historically, for web apps and REST services, I use Spring Boot which has a Gradle plugin that conveniently shades 3rd party ...
2
votes
0answers
36 views
Placing case classes
If the supervisor and its child actors are sharing the same messages, which is the right place to keep those case classes? Should it be in both Supervisor and the individual actors or should it be in ...
4
votes
1answer
96 views
Scala Callback Pyramid of Doom
I would like to solicit some general design principles and best practices to avoid creating a callback pyramid of doom particularly in the Scala language.
Consider the following rudimentary and ...
5
votes
1answer
139 views
How to make a webdriver run reliably in Selenium?
I have been having quite a time getting this to work reliably for 100s of thousands of terms and potentially millions of pages per source and ETL the resulting data into a database in an automated ...
5
votes
3answers
218 views
Why use Either over (checked) Exception?
Not too long ago I started using Scala instead of Java. Part of the "conversion" process between the languages for me was learning to use Eithers instead of (checked) Exceptions. I've been coding this ...
0
votes
1answer
68 views
In scala is it acceptable to have id variables written as `_id` instead of the regular camel case notation?
In Scala is it acceptable to have id variables written as _id instead of the regular camel case notation?
In the code most variables respect the camel case notation. However for variables referring ...
0
votes
1answer
97 views
Future and Try duality [closed]
I was looking at this lecture, https://class.coursera.org/reactive-002/lecture/135 at 2:15.
The lecturer talks about the duality of Future and Try in Scala. For his demonstration he changes the type:
...
1
vote
2answers
75 views
Type of map for Try[T]
I was looking at the type of map for Try[T] in Scala, which is:
def map[S](f: T=>S): Try[S]
From Haskell, I am used to the type of map being:
map :: (a->b)->[a]->[b]
This seems very ...
3
votes
1answer
62 views
Code style for parse-methods in Scala?
Say I have a class with multiple parameters:
case class Foo(a: String, b: String, c: String, d: String, ... )
Now say I want to parse an instance of Foo from a Map[String, String]:
// returns ...
3
votes
2answers
139 views
Designing a centralised command api
I'm working on a personal project that has a fairly complex UI. As much as possible I would like to remove mouse interaction. To help with that I am working on a kind of in app command line, where you ...
4
votes
1answer
200 views
Right way to read from token list to implement parse tree
I'm trying to tackle the While Language problem on HackerRank using Scala. I've been given a set of grammar rules and have to implement the actual interpreter underlying it.
I've decided to approach ...
0
votes
1answer
69 views
Determine execution order based on declarative definition?
We want to create a DSL in Scala where you can declaratively list the ingredients that a product consist of. These ingredients can consist of for example "Create product a", "Create product b", "Send ...
2
votes
1answer
99 views
When to use Future[T] vs. Future[Try[T]] when the call might fail
I am slightly confused when to use which because futures are allowed to fail. If futures are allowed to fail, then to me, that sounds like a Try[] in itself.
And if the Try[] is wrapped inside a ...
4
votes
1answer
59 views
What is the best pattern to define own type with definitive collection of values?
I want to define own data type, say a Digit, which would have definite type of values ( 0 to 9 ) and I'm struggling with defining this in a way it's practical. What's the best way (design pattern) to ...
0
votes
0answers
220 views
SQL/HQL in a Play framework controller and Service Layer pattern
I started using the Play Framework a couple of months ago. It "redefines" many conventions from the traditional Java world (like the way of using static methods among other things). I was browsing the ...
5
votes
3answers
358 views
Should databases be viewed as Monads?
Because any kind of persistence updates/inserts/deletes represents in some sense a kind of state change in a database, it makes me wonder whether databases can be considered monads. We say the same ...
57
votes
4answers
7k views
Is functional programming faster in multithreading because I write things differently or because things are compiled differently?
I'm diving into the world of functional programming and I keep reading everywhere that functional languages are better for multithreading/multicore programs. I understand how functional languages do a ...
1
vote
1answer
253 views
Why use tuples as function parameters in languages that support currying?
In languages that support currying, I can't think of many cases where using a tuple as function input parameters would be better than breaking the tuple apart into multiple parameters, which then ...
1
vote
2answers
453 views
Why does Scala name monadic composition as “for comprehension”?
Not sure if it's an appropriate question, but here it goes.
I know Haskell's do notation pretty well. And I realized that Scala's "for comprehension" really is just mostly the same as do notation in ...
2
votes
1answer
102 views
How to represent data objects gradually getting augmented in a pipeline [closed]
I'm working on a Scala data-processing program. Essentially we start with a collection of many small data objects, say, (eventId: String, basicInfo: Basic) and gradually filter out some objects and ...
2
votes
2answers
247 views
Why does an immutable object in Scala need less memory than a mutable one?
In Programming in Scala, there is this statement:
For instance an empty mutable map in its default representation of HashMap takes up about 80 bytes and about 16 more are added for each entry that’...
1
vote
1answer
147 views
Scala, checking for type and then cast to it
I have sometimes the usecase that I need to check if a variable is of a specific type and if so cast to it to access a property/function of it.
Example:
if (state.isInstanceOf[StateA] && ...
24
votes
8answers
4k views
Using a “strong” type system in the real world, say, for large-scale web-apps?
I know this is a very broad, ambiguous, and possibly philosophical question. To an extent, that the most important keyword in the question - "strong" type system - itself, is ill-defined. So, let me ...
5
votes
1answer
82 views
Why would you document a return type using scaladoc?
The following is snippet of an example from page 240 of the book Programming Scala (Wampler & Payne, 2009; O'Reilly Media):
/** @return Parser[Money] */
def deductItem = deductKind ~> ...
22
votes
3answers
8k views
Why would I not need an ORM in a functional language like Scala?
I'm wondering if I can switch from Java to Scala in a Spring + Hibernate project to take advantage of some Scala features such as pattern matching, Option and what it seems to me a cleaner syntax in ...
1
vote
1answer
2k views
akka and futures, which ExecutionContext should be used
Following this blog by @Tomasz Nurkiewicz we can see that 4 different approaches are available for picking the ExecutionContext, when interacting with akka actors, for scala futures to run on.
import ...
25
votes
5answers
2k views
Could an instance be equal to some other instance of a more specific type?
I've read this article: How to Write an Equality Method in Java.
Basically, it provides a solution for an equals() method that supports inheritance:
Point2D twoD = new Point2D(10, 20);
Point3D ...
1
vote
1answer
104 views
Scala: DSL for repetitive algroritms
So, very often in your code you have statements/algorithms like this one:
if (!myCollection.contains(unit))
throw new IllegalArgumentException(s"$unit is not available")
The issue which I have ...
3
votes
1answer
110 views
Including a library to use one function / utility classes
At my place of employment I have noticed a weird thing where developers will include largish libraries to do simple things. To be clear we're a Scala shop. Here are two examples that occured :
1) In ...
1
vote
1answer
332 views
Best way to separate public API from internal implementation [closed]
I'm developing a little framework (in Scala) where I want to define clean and simple interface for the users of the framework. Some of theses interfaces have to be implemented by the framework itself, ...
1
vote
0answers
231 views
When to use trait components, objects, package objects in Scala
When I am adding dependencies in Scala, I tend to use mixin trait components for those that are inherit to the class itself. For example, DaoGenerator extends Generator
However, for trait components ...
4
votes
2answers
2k views
Why Java does not have a framework like Rails? [closed]
I am a Ruby on Rails developer and I like its simplicity and rapidness. On the other hand we need to use Java for some projects that we develop for our clients.
My question is why (pure) Java does ...
2
votes
1answer
68 views
Transform Either types in Scala
I have an Either type e.g. Either[A, B] but I want an Either[X, Y] by using two functions A => X and B => Y.
I can do that with fold:
val myEither: Either[A, B] = ...
myEither.fold(
a => ...
1
vote
1answer
98 views
Send signal to a scala process
I have a cron job running that is a Scala application. I want another job, that runs forever, to do something when this one finishes. It seems to me some kind of signal (Unix/Linux signal?) has to be ...
1
vote
3answers
273 views
Why does scala require parenthesis around the condition of an if statement
Just curious, from a syntax parsing perspective, why does Scala actually require parentheses around the condition of an if statement, given that the only thing allowed after if is (?
1
vote
1answer
718 views
Polyglot Node.js with Typesafe Akka for Actor Model
I have a case that needs to manipulate a large stream of JSON and inject it into Apache HBase. Our system works on Node.js with Mongo then, since we need to enhance performance, so HBase is choosen to ...
5
votes
1answer
205 views
Use StringLike instead of String in Scala?
Scala has the StringLike trait. Let's say I want to create a case class Name and internally it should save the name with some characters. Should I use case class Name(name: StringLike) or case class ...
2
votes
1answer
78 views
Is there a way to make the default way a class is accessed a field within the class?
I'm trying to write a wrapper for the following bit of scala code:
val vertBuffer: FloatBuffer = createFloatBuffer(vertex_positions.length)
vertBuffer.put(vertex_positions)
vertBuffer.flip()
However,...
4
votes
2answers
2k views
Name of data structure that's tree-like with multiple root nodes
I'm attempting to implement a data structure, and using a more traditional tree data structure, but I'm not using the root node as it holds no real value in the context I'm using it in.
Ideally, I ...
22
votes
5answers
5k views
If Scala runs on the JVM, how can Scala do things that Java seemingly cannot? [duplicate]
I just learned about Scala yesterday, and I'd like to learn more about it. One thing that came to mind, however, from reading the Scala website is that if Scala runs on the JVM, then how is it ...
1
vote
2answers
637 views
best practices for packaging in Scala projects?
Scala runs on the JVM, but that doesn't mean we have to write it like Java. Eclipse discourages use of the default package. What are some considerations (e.g. size of project) that determine how ...
0
votes
1answer
84 views
FP: Capturing the characteristics of a process which blocks, causes side-effects, and may fail
I have a driver function modifyFile that interacts with many sources in the outside world (e.g. HTTP, filesystem). Let's say the code is as such:
def downloadFile(from: String, to: String): Try[Unit]
...
7
votes
1answer
258 views
Migrating a Grails application to Scala Play/Spray
We have a moderately sized Grails web application using GORM/Hibernate over PostgreSQL and GSPs serving HTML, and also a few REST APIs. We are standardising on Scala, and would like to migrate this ...
0
votes
1answer
157 views
How can I condense a case class with ~20 constructor arguments into 5 or 6 arguments in Scala?
Let's say I have a case class with ~20 constructor arguments? This is obviously very clunky to type. What would be the best way to condense these arguments into maybe 5 or 6 arguments? There are some ...
2
votes
1answer
76 views
Are Serverlet Containers' Characteristics Against Scalability and Fault-Tolerance of a Web Application?
We most likely deploy a JVM-based Web application (e.g., Scala) on a container (e.g., TOMCAT, Jetty, etc.). Does the following points about a serverlet container goes against us to create a scalable ...
3
votes
2answers
266 views
How to use scala case classes when delegation is needed
Let's assume in our application we want to model cars. We also want to model a car repository where we store some registered cars. How should that be modeled in scala?
Here comes my approach: First, ...
1
vote
2answers
69 views
What Scala type to use for a list of meeting participants
I want to store a number of participants for a meeting. The order of them is not important. The only thing I want to make sure is, that I will be able to add and remove participants, use filter ...
1
vote
2answers
156 views
The reason for scala not supporting Multiple Inheritance [closed]
I've searched around for a long time now and haven't come up with any official reason why scala doesn't support multiple inheritance. I know traits solve a lot of things, but the developers of C# for ...
1
vote
1answer
361 views
Generic delegates (C#) in Scala?
I wanted to know if there is an equivalent of the C# generic delegates in Scala.
The reference website is: http://msdn.microsoft.com/en-us/library/dd233060.aspx
I'm trying to make a complete ...