103 votes
Accepted

Is functional programming faster in multithreading because I write things differently or because things are compiled differently?

The reason people say functional languages are better for parallel processing is due to the fact that they usually avoid mutable state. Mutable state is the "root of all evil" in the context of ...
MichelHenrich's user avatar
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
10 votes
Accepted

When to use Future[T] vs. Future[Try[T]] when the call might fail

There's really no reason to ever use a Future[Try[T]]. You add no benefit over a plain Future, and a good deal of complexity. Trys are sort of the synchronous version of Futures. You use them to ...
Karl Bielefeldt's user avatar
9 votes
Accepted

Future and Try duality

Note: I am not an expert in Category Theory. If you want a precise explanation what Duality means in Category Theory, you should ask a mathematician. The concept of duality in Category Theory can be ...
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

Is functional programming faster in multithreading because I write things differently or because things are compiled differently?

Sort of both. It's faster because it's easier to write your code in a way that's easier to compile faster. You won't necessarily get a speed difference by switching languages, but if you had started ...
Karl Bielefeldt'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

Is functional programming faster in multithreading because I write things differently or because things are compiled differently?

Functional programming doesn't make for faster programs, as a general rule. What it makes is for easier parallel and concurrent programming. There are two main keys to this: The avoidance of ...
sacundim's user avatar
  • 4,786
7 votes
Accepted

Type of map for Try[T]

Scala is an object-oriented language. In object-oriented languages, methods have special access to a designated "receiver" argument (typically called this or self). This parameter is usually not made ...
Jörg W Mittag's user avatar
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
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
6 votes
Accepted

Code style for parse-methods in Scala?

You could create your custom method for extracting Map values as an Either instance and use them in a for comprehension: implicit final class MapOps[A, B](self: Map[A, B]) { def getAsEither(key: A):...
Paweł Jurczenko'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
Accepted

Should databases be viewed as Monads?

Not really. A database is a facility for the structured and organized storage and retrieval of data. A monad is a way to communicate with that database in a manner that preserves functional purity. ...
Robert Harvey's user avatar
5 votes
Accepted

Designing a centralised command api

One way to accomplish what you're looking for is to implement the Observer pattern. For example, instead of having a single, centralized "commands" object that defines all possible commands, you'd ...
Evan's user avatar
  • 1,265
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,925
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

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
Accepted

Right way to read from token list to implement parse tree

I would suggest you use your Lexer construct as more of a generator, rather than identifying all the tokens at once and sending an entire list to the parser. Your lexer could simply declare an ...
Chris's user avatar
  • 98

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