Questions tagged [scala]

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.

Filter by
Sorted by
Tagged with
1
vote
1answer
22 views

Preferable design for overriding sealed trait method in Scala case classes/objects

Say I have a sealed trait Person that requires the definition of a method work :: Unit -> Unit. I then have three case objects Accountant, Doctor, and Lawyer. I can think of two ways to implement ...
0
votes
1answer
101 views

Why is this “more efficient” version of a Scala Collatz solution slower than tail recursion?

I implememted two versions of the collatz problem and felt an icy terror in the pit of my stomach as an optimized solution was slower than tail. The tail recursion is simple: // calculate the next ...
3
votes
3answers
483 views

Does variance make sense in a fully immutable language?

In many OOP programming languages, types can be made co-, contra- or in- variant. Most (if not all of these languages) are able to let variables be mutated in place, i.e. they are not fully immutable ...
0
votes
2answers
199 views

Scala Option apply method when passed a None

Not sure if this is an appropriate question for here, please let me know! In Scala, the ever so useful Option class has an apply method in its companion object that allows us to quickly wrap any ...
1
vote
2answers
196 views

Is it a good idea to use “lazy val” for correctness?

In Scala, declaring a val as lazy means that its value won't be evaluated until it's used for the first time. This is often explained/demonstrated as being useful for optimization, in case a value ...
2
votes
1answer
92 views

Extends for feature (aka mix-in) in Scala is bad practice, isn't it?

In Scala, sometimes class extends a trait to provide some methods to extending class inside. ScalaTest import org.scalatest._ class ExampleSpec extends FlatSpec with Matchers { ... Matchers trait ...
3
votes
3answers
338 views

Define multiple DDD events or just one XXXChanged event when using CQRS?

Is it preferable to define a single 'container event' like below: trait UserStatus case object Active extends UserStatus case object Inactive extends UserStatus case class UserStatusChanged(...
-1
votes
1answer
65 views

In which order functions should be defined scala methods

I have some values and methods to define in a scala object. I do not know in which order I should define them. Is there a "good way" to do it? Such as private methods first then public methods? Only ...
1
vote
2answers
166 views

Scala: what has been the desired design consideration for the null instantiation in the example

Or simply asked WHY ? It simply does not seems logical ... if ( null.asInstanceOf[String] == null ) println ("null.asIstanceOf[String] is null") println ("BUT !!!") if ( null.asInstanceOf[...
1
vote
2answers
120 views

How can an iterative algorithm be controlled dynamically?

Suppose we need an iterative algorithm for mathematical optimisation. Each iteration takes a long and random time. After each iteration, a stopping condition is checked for the iterate x, based on ...
2
votes
1answer
57 views

Architecture and Database re-design for migration and support of multiple clients

I'm evaluating the migration of the following application's architecture: - Nginx + PHP + MySQL - Currently the infrastructure is scalable and redundant in the AWS cloud and It was designed to support ...
8
votes
2answers
1k views

What's the difference between a Mixin and a Trait?

From what I can tell from Scala and Hack- Mixins: Can have state (ie. instance properties) Can only provide concrete methods Can have constructors, that are called in the same order that their ...
3
votes
0answers
115 views

Should I pass presenter to use case when implement clean archeiture using scala

This question came to me when I was trying implement Clean Architecture using Scala, and come across this post. In the accepted answer, @candiedorange emphasis on the separation of responsibility, ...
1
vote
1answer
166 views

Building a real-time API on a Kafka/Kinesis-centric structure

I am a beginner to the event-driven data architecture using Kafka / Kinesis as the centrepiece (currently Kinesis) and I have some questions regarding how to build a WebSocket API on such a structure. ...
2
votes
2answers
117 views

Trait and Implementation vs just Implementation

In my previous Java job, we tended to avoid defining interfaces for everything unless we genuinely had multiple implementations for it. This may just be my opinion, but if there is only one ...
1
vote
1answer
50 views

Java/Maven and Scala/sbt projects share immutable thing

Project 1: a Java/Maven project Project 2: a Scala/sbt project Thing: generally, an immutable object instantiated in from 3rd party Java library. For example, ThingBuilder.foo("bar").build() ...
0
votes
2answers
87 views

Do Scala worksheet (or REPL logs) belong to the SCM?

The Eclipse Scala IDE (and Intellij Idea, too) has, together with a standard REPL CLI, an artifact named Worksheet, that works like a persistent REPL log: the whole file is compiled and executed at ...
5
votes
1answer
188 views

Is using Option#get really a bad idea here?

I'm working on a Scala project and Wartremover shows an error about Option#get usage in my code: Option#get is disabled - use Option#fold instead While I do understand how get should often be ...
3
votes
1answer
207 views

Dependencies between functions-only modules: hardcoding vs alternatives

In switching from a procedural background to "FP in the small, OO in the large" I'm grappling with the following problem. Suppose there're modules, each only containing numerical math functions ...
1
vote
1answer
491 views

Does dependency injection fly in the face of functional programming?

I have the following pure function (f2 and f3 are pure too): class X { def f1(arg: Type1): Type2 = { val x = f2(arg) val y = f3(x) y } def f2... def f3... } Now, I would like ...
5
votes
2answers
5k views

Which approach for integrating Python code into a mainly Scala application did you use a second time because it was successful? [closed]

I have a mainly Scala application and I am interested in approaches to integrating Python code into this application in a way that is proven by you personally to be successful. In this context ...
0
votes
1answer
141 views

Unit testing certain tasks are done in a process

I have some Scala code that I wish to unit test using ScalaMock: class CancellationManagerSpec extends FlatSpec with MockFactory { "Cancelling a shipment" should "check shipment is not already ...
5
votes
0answers
484 views

Class table inheritance using Slick: always an anti-pattern?

My application's model includes a number of entities that share many common properties, and are operated on by many of the same classes. If I was using an ORM, I'd define an abstract class with the ...
0
votes
1answer
126 views

Can a database wrapper be implemented in Scala using Traits? Is there a better way?

I've decided to implement a database wrapper to provide data operations of a web service using postgresql dbms. Models in my database are somelike complex and frameworks like slick do provide more ...
5
votes
2answers
691 views

Avoiding instanceof for recursive data types

I have written a simple class hierarchy to represent terms in Scala. Terms are recursive data types, e.g., a Sum and a Multiplication consist of the left-hand-side (lhs), which is a Term, and the ...
3
votes
2answers
584 views

Reverse engineering an ETL pipeline

I have a one-way large ETL pipeline in Scala where I start with protobuf schema A and end up with protobuf schema B. I want to create a many-to-many mapping between the fields of schema A and schema B,...
1
vote
1answer
92 views

Simplified API one case class vs robust and multi ADT case class? [duplicate]

Below are oversimplified examples, but I wonder which route to take what is the best practice when designing API, the simple or the more complex and robust which is better? This looks good and goes ...
0
votes
1answer
664 views

RESTful API with Scala using Jersey?

Thanks Maven archetypes the barrier to start developing of services for new RESTful APIs is very straightforward - you can get more or less a project template and start filling in your code. Now as I ...
1
vote
1answer
299 views

Modelling relational database entities in a functional language

I'm working on a Scala project that uses DynamoDB for persistence, and does this by modelling the records as case classes. This is becoming increasingly more relational, which means we have classes ...
1
vote
2answers
52 views

In my use case, does it make sense to make every major method return an async result?

I have a Playframework application written in Scala. As you might expect, it's primary function is to respond to user requests. Typical request flow: Parse input and figure out what the user wants ...
1
vote
2answers
400 views

Why doesn't scala do type checking when checking for equality

For example: scala> val a: String = "5" a: String = 5 scala> val b: Int = 5 b: Int = 5 scala> a == b res5: Boolean = false I expect a TypeError, like in Java: class MyClass { public ...
3
votes
2answers
183 views

functional programming: impact of typedef-ing datatypes on code readability and maintenance

In functional programming languages, such as Scala, data types and structures, are really important. I am in two minds about the use of type-defs in helping with the readability of the code ...
1
vote
2answers
300 views

Designing Scala library which wraps a Java library

I'm designing a simple scala wrapper library over a java library in order to make it more idiomatic scala. The problem I faced with is that all operations from the java library could throw an ...
3
votes
1answer
598 views

Should I pass all arguments to a method explicitly in functional programming?

I wonder whether a method in a functional programming language should receive all variables from the argument list, or whether it is ok to use variables from the outer scope? But let me explain the ...
2
votes
2answers
538 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-...
2
votes
1answer
126 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
530 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 ...
4
votes
2answers
1k 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 ...
17
votes
3answers
4k 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
113 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 ...
2
votes
1answer
298 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
123 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 ...
4
votes
1answer
90 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
175 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
951 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
139 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 ...
4
votes
1answer
3k 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 ...
3
votes
1answer
67 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 ...
7
votes
3answers
2k 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 ...
63
votes
4answers
10k 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 ...