Tagged Questions
0
votes
1answer
32 views
Consolidating a data table in Scala
I am working on a small data analysis tool, and practicing/learning Scala in the process. However I got stuck at a small problem.
Assume data of type:
X Gr1 x_11 ... x_1n
X Gr2 x_21 ...
2
votes
1answer
133 views
Scala “update” immutable object best practices
With muttable object I can write smth like
var user = DAO.getUser(id)
user.name = "John"
user.email ="[email protected]"
// logic on user
If user is immutable and I need to clone\copy it on every change ...
2
votes
3answers
105 views
What is a good way of reusing function result in Scala
Let me clarify my question by example. This is a standard exponentiation algorithm written with tail recursion in Scala:
def power(x: Double, y: Int): Double = {
def sqr(z: Double): Double = z * z
...
1
vote
0answers
56 views
Whats the diference between def f1[A](x: => A) and def f1[A](x: A)? [duplicate]
I was reading the definition of getOrElse in Option object:
def getOrElse[B >: A](default: => B): B
Why putting the arrow and not just writing?:
def getOrElse[B >: A](default: B): B
...
1
vote
2answers
105 views
Could I avoid retyping two very similar functions using scala?
My sample is as simple as this:
def func(arg1: Long, arg2: Long, arg3: String) {
privateFunc1(arg1);
privateFunc2(arg1, arg2, arg3);
}
and this is an overloaded function which also works
def ...
2
votes
1answer
55 views
Lazy foldRight early termination confusion
While going though Functional Programming in Scala, I came across the following code snippet:
def foldRight[A](z: => B)(f: (A,=>B) => B):B = uncons match {
case Some((h,t)) => ...
2
votes
1answer
123 views
Scala comonads; Comonad laws?
So given this encoding of a comonad (see below) are the comonad laws above it correct? for some reason I don't think they are from looking at them, and I know that heading off wrong from there will ...
4
votes
4answers
98 views
Partially applied functions with all arguments missing in Scala
From my understanding, a partially applied function are functions, which we can invoke without
passing all/some of the required arguments.
def add(x:Int, y:Int) = x + y
val paf = add(_ :Int, 3)
val ...
1
vote
2answers
99 views
Implementing CSP (Communicating Sequential Processes) in Scala
I am trying to implement a parser and semantic for a 'CSP' in Scala . I have already implemented the parser , and now I am busy working on a Semantic part of the language. I am completely new to the ...
0
votes
3answers
110 views
How come I can't flatMap from Option to List in Scala? [duplicate]
When I try
Some(1).flatMap(_ => List(2))
I get
error: type mismatch;
found : List[Int]
required: Option[?]
But doing Some(1).map(_ => List(2)).flatten works. How come I get a compile ...
3
votes
4answers
254 views
Is there a name for this kind of lifting a function?
I wrote a Scala function:
def liftOrIdentity[T](f: (T, T) => T) = (a: Option[T], b: Option[T]) =>
(a, b) match {
case (Some(a), None) => Some(a)
case (None, Some(b)) => ...
1
vote
4answers
97 views
Scala: exception handling in anonymous function
If I pass an anonymous function as an argument, like e.g. in this code sample:
val someMap = someData.map(line => (line.split("\\|")(0), // key
...
0
votes
2answers
80 views
How do I filter a list with functional programming in scala?
I have two lists:
val list1 = List("asdf", "fdas", "afswd", "dsf", "twea", "rewgds", "werwe", "dsadfs");
val list2 = List();
I want to filter all items from list1 and setup list2 so that it only ...
0
votes
2answers
110 views
Initializing a val lately
Is it possible to do that in Scala using only val:
class MyClass {
private val myVal1: MyClass2 //.....????? what should be here?
def myMethod1(param1: Int) = {
myVal1 = new MyClass2(param1)
...
3
votes
2answers
132 views
FoldLeft using FoldRight in scala
While going through Functional Programming in Scala, I came across this question:
Can you right foldLeft in terms of foldRight? How about the other way
around?
In solution provided by the ...