Skip to main content

All Questions

2 votes
3 answers
76 views

List pattern matching add filtering based on case object

I have a list of Male and Female inhabitants to be iterated over. How to add filtering based on gender to the list pattern matching? (such that countOldManFloor would return 1 only if inhabitants ...
techkuz's user avatar
  • 3,981
4 votes
2 answers
2k views

Why use a helper function inside a recursive function?

In the book Functional Programming in Scala, in the context of explaining how recursion is often used in functional programming over imperative iteration, the authors show recursion via a factorial ...
Aaron's user avatar
  • 3,459
0 votes
2 answers
123 views

Convert Option based linked list to a `List`

What is a nicest way to implement such function case class Node(prev: Option[Node]) def flatIt(n: Node): List[Node] = ??? val n1 = Node(None) val n2 = Node(Some(n1)) val n3 = Node(Some(n2)) flatIt(...
kan's user avatar
  • 29.1k
1 vote
1 answer
95 views

Conversion of Looping to Recursive Solution

I have written a method pythagoreanTriplets in scala using nested loops. As a newbie in scala, I am struggling with how can we do the same thing using recursion and use Lazy Evaluation for the ...
petereg157's user avatar
13 votes
1 answer
1k views

How to reason about stack safety in Scala Cats / fs2?

Here is a piece of code from the documentation for fs2. The function go is recursive. The question is how do we know if it is stack safe and how to reason if any function is stack safe? import fs2._ /...
Lev Denisov's user avatar
  • 2,191
-2 votes
1 answer
120 views

Why does the absence of an else block translate to Unit type return for a function?

I noticed there is a type mismatch caused in the line else if(r1 == 0 || divisors.tail.isEmpty || !divisors.tail.contains(r1)){newAcc}. Because there is no else clause to my if ... else if ... def ...
Imad's user avatar
  • 2,743
3 votes
1 answer
166 views

What is a better way to solve this exercise in more Immutable way?

I am trying to solve a problem on HackerRank. I am trying to solve this problem in more functional way (using immutability). I have attempted a solution but I am not fully confident about it. Here’s ...
Sandio's user avatar
  • 113
4 votes
2 answers
4k views

Is this Depth First Search implementation tail recursive now?

I had this function for functionally traversing a graph: private def dfs(current: RCell, rCellsMovedWithEdges: Vector[RCell], acc: Vector[RCell] = Vector()): Vector[RCell] = { current.edges....
COOLBEANS's user avatar
  • 767
12 votes
2 answers
2k views

How to make tree mapping tail-recursive?

Suppose I have a tree data structure like this: trait Node { val name: String } case class BranchNode(name: String, children: List[Node]) extends Node case class LeafNode(name: String) extends Node ...
Michael's user avatar
  • 42.2k
-1 votes
1 answer
71 views

How to identify whether a problem can be solved with Tail-recursion or not in Scala

How can I identify whether a problem statement can be solved with tail recursion or not. Is there any characteristics of a problem by which one can identify that?
Abhirup Bose's user avatar
0 votes
1 answer
234 views

Scala Polymorphic Function Type Mismatch

When trying to run a polymorphic function that abstracts over the type of the array, I get a type mismatch with the following error message: Type mismatch: expected: (Int) => Boolean, actual: Int ...
mberrett's user avatar
7 votes
5 answers
7k views

Writing a factorial tail recursive function in Scala

I'm trying to write a tail recursive function in the below way, but the compiler is throwing an error: Too many arguments for method apply: (v1: Int)Int in trait Function1 else factorial(x-1, ...
Vasu's user avatar
  • 93
1 vote
2 answers
875 views

Tail-recursive Implementation of Scala's List partition method

For exercise purposes I've been trying to implement a couple of Scala's List methods in a functional manner, one of them being partition. Assume the following signature: def partition[T](l: List[T], ...
BenScape's user avatar
  • 159
5 votes
4 answers
345 views

Rewriting imperative for loop to declarative style in Scala

How do I rewrite the following loop (pattern) into Scala, either using built-in higher order functions or tail recursion? This the example of an iteration pattern where you do a computation (...
ioreskovic's user avatar
  • 5,709
3 votes
1 answer
277 views

Why nested FlatMaps could blow up the stack in Scala?

I am learning the Trampoline trick in Scala by reading this paper Stackless Scala with Free Monad, by Rúnar Bjarnason. But I got stuck in section 4.3 "An easy thing to get wrong". One thing makes me ...
SanCoder's user avatar

15 30 50 per page