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 ...
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 ...
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 (...
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. ...
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 ...
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 ...
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).
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 ...
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 ...
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 ...
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" ! ...
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 ...
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, ...
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 = {...
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.
....
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 "_=" ...
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 ...
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 ...
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,...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
scala × 239java × 49
functional-programming × 37
programming-languages × 13
haskell × 13
design × 12
language-design × 11
clojure × 10
akka × 10
c# × 9
architecture × 9
jvm × 8
design-patterns × 7
object-oriented-design × 7
exceptions × 7
actor-model × 7
concurrency × 6
scalability × 6
type-systems × 6
immutability × 6
trait × 6
coding-style × 5
functions × 5
playframework × 5
database × 4