Skip to main content
15 votes

Why use Either over (checked) Exception?

If you only use an Either exactly like an imperative try-catch block, of course it's going to look like different syntax to do exactly the same thing. Eithers are a value. They don't have the same ...
Karl Bielefeldt's user avatar
11 votes

Does variance make sense in a fully immutable language?

Variance has nothing to do with mutability. It appears at the intersection of parametric polymorphism and subtyping. The oldest example of variance is actually an example that comes out of functional ...
Jörg W Mittag's user avatar
9 votes
Accepted

Does variance make sense in a fully immutable language?

does even the concept of variance make sense in an immutable environment? Yes; presence of subtyping is independent of immutability. could every type of a speculative fully immutable OOP language (...
Alexey Romanov's user avatar
8 votes

Why use Either over (checked) Exception?

The two are actually very different. Either's semantics are much more general than just to represent potentially failing computations. Either allows you to return either one of two different types. ...
Jörg W Mittag's user avatar
8 votes
Accepted

Is using Option#get really a bad idea here?

Instead of pattern matching explicitly, you can write .getOrElse { throw new IllegalStateException("Failed to load answer after creation") } More generally, when dealing with Option/Either/Try you ...
Alexey Romanov's user avatar
8 votes

What is the benefit of Java collection streams over C# or Scala collections?

we already had well-established mechanisms for manipulating collections in several other languages [...]. Since the creators of java decided that java collection streams are worthy of including in ...
Filip Milovanović's user avatar
7 votes
Accepted

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

Scala inherited this behavior from Java. The key is that Scala's == is equivalent to Java's equals, not == (which checks referential equality).
Morgen's user avatar
  • 1,091
7 votes

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

It's fine to use constants (including references to other functions) from the outer scope. If you copy your function to another file and those symbols aren't defined, you'll get a compiler error, the ...
Karl Bielefeldt's user avatar
6 votes
Accepted

How do you encode Algebraic Data Types in a C#- or Java-like language?

All of the other answers here are outdated (as of me writing this answer). Java has first class support for Abstract Data Types (ADT's) now. The easiest way to create an ADT is by using Records and ...
davidalayachew's user avatar
6 votes
Accepted

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

I strongly prefer the first version: addIntTuples does exactly what it says. It is a generic method that could even exist outside of this scope. This means that when I reason about the code, I can can ...
V-Lamp's user avatar
  • 176
6 votes
Accepted

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

Capture results via a variable and capture exceptions via STDERR as follows: in Scala: import sys.process._ def callPython(): Unit = { val result = "python /fullpath/mypythonprogram.py" ! ...
tale852150's user avatar
6 votes
Accepted

Does dependency injection fly in the face of functional programming?

Functional programming is about minimizing states and all that comes with that. Any and all information that would normally be provided by class members is instead provided through parameters. It ...
Neil's user avatar
  • 22.8k
6 votes
Accepted

Trying to understand how this class representation truly represents Natural numbers in Scala

when I create a val two = new Succ(2) why would I set the two.predecessor = 2 when 2's predecessor is actually 1? You can't call new Succ(2), as Succ needs to have an object of type Nat as argument, ...
Bart van Ingen Schenau's user avatar
5 votes

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

If I were going to go to those kinds of lengths to improve the readability of my code, I would just create a SumLen class case class SumLen(sum: Int, len: Int) { def add(that: SumLen): SumLen = {...
Donald.McLean's user avatar
5 votes
Accepted

Export huge excel file

My question is ... how I can map db tables in excel files to give it to users instantly, when they want to download a table? Simple answer: you can't, unless you have the files prepared in advance. ....
Hans-Martin Mosner's user avatar
4 votes

In scala is it acceptable to have id variables written as `_id` instead of the regular camel case notation?

Underscores in names (_) are not actually forbidden by the compiler, but are strongly discouraged as they have special meaning within the Scala syntax. Mutators for example use the "_=" ...
dunck's user avatar
  • 167
4 votes
Accepted

Using actor model, how can one program concurrent portion (critical section) of code as self contained nuggets?

It's probably best to think of the actor model as the ancestor of object oriented programming; and compare it to OOP. However, because (I hope) you're already familiar with OOP I'm going to do the ...
Brendan's user avatar
  • 3,975
4 votes

Operate on data that doesn't fit into JVM

As this wonderful bloomfilter tutorial by llimllib will show you, when you test an element you have to recalculate its hash. This will tell you what bits it sets in the bloom filter. If your ...
candied_orange's user avatar
4 votes

Trying to understand how this class representation truly represents Natural numbers in Scala

The encoding shown here is based on the Peano Axioms for natural numbers: Z ∈ ℕ. (Existence of zero.) ∀x ∈ ℕ: x = x. (Reflexivity of equality.) ∀x, y ∈ ℕ: x = y ⇒ y = x. (Symmetry of equality.) ∀x,...
Jörg W Mittag's user avatar
3 votes
Accepted

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

One downside I can see is that the function signatures are "lying" This is actually fairly significant. Type signatures are a free and guaranteed up to date documentation on your system. Having ...
walpen's user avatar
  • 3,241
3 votes

Scala Callback Pyramid of Doom

First of all, I encourage you to give it a try without the Eithers if possible. Futures already encode an error state, and that's sufficient in the vast majority of real world cases. That gets you ...
Karl Bielefeldt's user avatar
3 votes

Do non-pure interpreters still make the guarantees of functional programming?

The guarantees provided by a functional programming language apply to the behavior of programs written in that language, not the implementation itself. "No side effects" means that there are no side ...
Sean Lynch's user avatar
3 votes

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

There is not going to be a single answer to this because each case is different and all approaches has been used successfully by different teams. In general there are three approaches: If both ...
Lie Ryan's user avatar
  • 12.4k
3 votes
Accepted

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

Yes, it is considered okay in general. A relatively common use is to initialize fields of a supertype when they have to depend on the state of a subtype (but even there you need to be careful). See ...
Alexey Romanov's user avatar
3 votes

Scala Option apply method when passed a None

Well, the apply method was designed to create an option from a nullable value, and if you already have an Option, there's not really a need to create one. Also a Some(None) is valid in certain ...
Karl Bielefeldt's user avatar
3 votes
Accepted

Scala Option vs. conditional branches

Don't get me wrong, optional is great. Use it when it makes sense. However, if you want to compare it honestly to conditional code then you need better conditional code. I think De Morgan can help ...
candied_orange's user avatar
2 votes

Transform Either types in Scala

So you want to map left and right of either monad right? Instead of writing fancy utility methods I'd rather stick with vanilla monad transformers since Scala already provides tools to achieve what ...
Arda Güney's user avatar
2 votes

What are the biggest differences between F# and Scala?

this is also a good article on the topic: comparing-scala-to-fsharp as always, best way is to try for yourself! //dotnet fsi --> starts F# interactive //or open vscode and a .fsx file, install ...
jkone27's user avatar
  • 39
2 votes

Using actor model, how can one program concurrent portion (critical section) of code as self contained nuggets?

Eliminate the critical section Divide the work into individual parts in such a way that they no longer require concurrent access to a shared memory pool. This is inevitable, since actors can and ...
Robert Harvey's user avatar
2 votes

Placing case classes

If child actors are short-lived workers, while supervisor is some static service - better put all messages it accepts in the supervisor's companion object along with the props factory. All clients ...
iTollu's user avatar
  • 315

Only top scored, non community-wiki answers of a minimum length are eligible