Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [scala]

Scala is a general purpose programming language principally targeting the Java Virtual Machine. Designed to express common programming patterns in a concise, elegant, and type-safe way, it fuses both imperative and functional programming styles.

2
votes
1answer
29 views

Game of Life with Scala and Functional Programming

I have been doing OOP and imperative programming my whole career but I wanted to dabble in Scala along with functional programming. Rather than writing Java like code in Scala I wanted to try a more ...
1
vote
1answer
42 views

Calculate simple arithmetic string in Scala

Problem Given an arithmetic string like 2+2*3 calculate result 8. Assumptions you can make string will be always well formed (no error checking needs to be done) only operators passed in string ...
0
votes
0answers
14 views

Creating an abstract collection of items using cats

I have some code that uses Monix Observable for stream processing of a file. To test this code, I'd like for the operations I perform on the ...
0
votes
1answer
24 views

find if one string is a valid anagram of another , in scala

Question is taken from leet code. Problem Given two strings s and t , write a function to determine if t is an anagram of s. Examples ...
1
vote
1answer
26 views

Longest Substring Without Repeating Characters in Scala

Question is taken from Leetcode and solution works for their test cases. I do see a scope of improvement and make it more functional (getting rid of vars and mutables data structures). Please suggest ...
0
votes
0answers
12 views

Efficiency of grouping a List of case classes by more than one field

I have the following input: ...
-1
votes
0answers
23 views

Calculate number of possible messages from a coded string

Question Alphabets are assigned numbers like - a = 1 b = 2 c = 3 . . z = 26 Given a number like 1234 as input calculate count ...
1
vote
1answer
52 views

Check if list contains a pair which adds up to a given sum

Kindly review this scala code for given problem and suggest improvements. Problem - Given an array of integers and a target sum, check if array contains a pair which adds up to sum. Example - <...
1
vote
0answers
25 views

NQueen solution in scala

This is my solution for NQueen problem in scala. I just want to know how to make it more easy to read, modular and areas i need to improve. ...
0
votes
1answer
41 views

Find the combination of matches which are closest to each other

Problem - Given three sorted arrays find combinations which are closest to each other. example - ...
3
votes
1answer
40 views

Mark positions attacked by a queen on a chess board

Problem - Given a chess board and the position of only one queen piece in it, mark all the position where the queen can move with X. Kindly review this Scala code and suggest improvements. ...
0
votes
2answers
43 views
0
votes
0answers
40 views

Scala function to summarize sales metrics

I have a Java background and have recently started learning Scala. I've implemented a method that still looks to me more like a Java style. How can it be improved using various Scala features to be ...
1
vote
1answer
71 views

Run two computations in parallel, and return the results together

I am reading the "Learning Concurrent Programming in Scala" and there are exercises in the end of each chapter. One of exercises is Implement a parallel method which takes two computation blocks a ...
1
vote
2answers
53 views

Project Euler - multiples of numbers

I'm trying to learn functional programming and Scala, following through Project Euler problems. I'm solving get the sum of multiples x & y in a list. So for a list ...
1
vote
1answer
56 views

Longest Collatz sequence in Scala

I am trying to find the longest Collatz Sequence: Which starting number, ≤ N, produces the longest chain? If many possible such numbers are there print the maximum one. (1 ≤ N ≤ 5×106) I tried ...
0
votes
1answer
44 views

Count lines of scala code (omitting empty lines) in a directory, traversing any subdirectories

I need to simply count lines of Scala code in a project, which includes some package(i.e. directory) hierarchy. I'm not sure about the performance of the code below. Can you help improve it, if ...
1
vote
0answers
30 views

Add an attribute to specific XML Elements in Scala

supposed I have an XML like ...
0
votes
0answers
72 views

Generating test Spark dataframe with Scala

I need to create test dataframe of lat/lon points. I make it with scalanlp and it looks suitable for me. Next stage is creating ...
1
vote
1answer
83 views

Binary search in Scala

Another exercise in Scala in which the goal is to find the target position as fast as possible. The initial input provides the grid size and the initial position. Each turn, this code provide a new ...
0
votes
0answers
190 views

Spark Dataframe aggregate with limited random samples

If I want to aggregate by one column and sample a fixed number of samples for each key, one way to do it is first use collect_list(). With this approach, the job can run out of memory when some keys ...
4
votes
1answer
100 views

Find an ideal speed to cross all traffic lights when they are green

As a second exercise with Scala I tried to solve another puzzle. In this puzzle, we have to find the highest speed that allows a driver to cross all the traffic lights when they are green, without ...
2
votes
1answer
51 views

Finding the nearest defibrillator exercise from CodingGames

For my first attempt at Scala I decided to solve a simple exercise on codingame.com. Based on a string input containing the user location, the defibrillators count and a list of defibrillators ...
0
votes
1answer
75 views

Finding the number of inversions in an Array

I am doing a college assignment where I have to count the number of inversions in an array/list. An inversion is defined as a tuple where i<j and a[i]>a[j]. ...
1
vote
1answer
76 views

Fixed size double linked list in Scala

I have written a double linked list in Scala. The list should be fixed in size and items are always added to the head. if the list reaches max size then the tail is dropped. This is my code. I want ...
3
votes
0answers
51 views

Finite State Machine to segment Thai syllables

I am developing a FSM to segment Thai syllables with the following rules: Here are my util functions: ...
6
votes
2answers
171 views

Program to calculate some stats from Facebook messages

I've written a program that parses JSON downloaded from Facebook to calculate some stats from a conversation between two parties. It attemps to count these things: Messages grouped by day of week ...
0
votes
2answers
50 views

Retrieve value from item wrapped in a Future[Option[Item]] [closed]

I have a function like this def findById(id: UUID): Future[Option[Item]] case class Item(subject: String) Now I want to retrieve the ...
0
votes
0answers
282 views

Replace Every JSON Key's Instance with Value

Given the following using the Ammonite REPL: ...
3
votes
1answer
56 views

Readable unit test - lists of complex objects

Goal: Writing more readable tests. I have a couple of functions, which basically merge and converts two lists of Datasets together, written using Scala and Spark. Each of these Datasets has a lot of ...
2
votes
1answer
496 views

Solution to a simple Hackerrank problem: Compare the Triplets

Given two equally sized arrays of integers A and B, compute the number of times ...
1
vote
0answers
334 views

WebSocket example with akka-http

Akka-http supports WS connections like that: ...
1
vote
0answers
39 views

short circuit nesting multiple cats.effect.IO [closed]

is there better way of nesting multiple IO? readfromSources should not look for soure2 if data is available in source1 ...
1
vote
1answer
56 views

Create a collection of functions tupled with a collection of parameters, and execute the collection in order in Scala

I am trying to create a collection of Unit functions that have any arity and type, and will execute them in order. This is the working implementation that I have: ...
1
vote
1answer
164 views

Check Is Anagram Scala HashMap based implementation

Here is the HashMap based implementation of the function checking whether two strings are anagrams: Example: ...
1
vote
1answer
214 views

Scala tic-tac-toe no mutable state

The following was an exercise for me to try to understand how state can be passed down the stack without immutability. I'm used to writing java code and this is just a way for me to understand. I've ...
0
votes
0answers
59 views

Transforming websocket data though Akka framework for Scala

I am using Akka to listen to a websocket that handles data from the GDAX trading platform. I haven't used Akka or Scala before and it seems like there are a lot of different ways to approach problems....
1
vote
1answer
107 views
0
votes
1answer
1k views

Joining Apache Spark data frames, with many conditional substitutions

I am joining two data frame in spark using scala . My code looks very ugly because of the multiple when condition . Can somebody please help me simplify my code? Here is my existing code . ...
0
votes
0answers
29 views

select index to keep from candidate list by external function

Given a shape (a closed LineString, i.e. sequence of Points, where the first and the last points are equal) that contains other duplicates than the first and last point. The algorithm finds all ...
0
votes
1answer
43 views

Compute max population given a list of people's birth and death year

I recently came across a brief programming puzzle prompt (say that three times fast) which asked to return the year that population peaked. The actual algorithm to compute it can be done several ways ...
1
vote
1answer
86 views

Reduce function on custom data structure using Cats Semigroup

This is for purely pedagogical purposes only. So, within the contrived data structure and its constraints, I'm interested in knowing how might one improve that reduce function and whether I've ...
1
vote
4answers
117 views

Project Euler problem 92 in Scala, square digit chains

Here is a summary of the problem: "A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before... How many starting ...
0
votes
1answer
386 views

Scala type matching map

I was working with some complicated generics system, and I found the need to make an abstraction for a typesafe mapping between K[T] and V[T] where T is the same for individual pairs. In the case ...
1
vote
0answers
717 views

Apache spark compaction script to handle small files in hdfs

I have some use cases where I have small parquet files in hadoop say 10-100mb and I would to compact them so as to have files at least say 100mb or 200mb. The logic of my code is find a partition to ...
1
vote
1answer
180 views

Take a number and sum its digits raised to the consecutive powers

I am new to Scala have to solve this problem: If you have number "xy" should equal sum of consecutive powers of its digits xy = x^1 + y^2 For example, take the number 89 and you should ...
1
vote
3answers
350 views

Shell script to parse variable definitions from Scala build file

The build.sbt text file contains versions like this: name := "happy" scalaVersion := "2.11.8" sparkVersion := "2.2.0" I wrote ...
2
votes
1answer
2k views

Removing stop words from a Spark Dataframe

I am trying to apply a function to two Spark Dataframes (in Zeppelin): ...
5
votes
2answers
170 views

General FizzBuzz in Scala

I wanted to create a FizzBuzz implementation that both showcases the strengths of Scala (Functional paradigm and collections) and is general enough to anticipate the typical interview follow-up ...
1
vote
2answers
84 views

Get the highest role of a user across all the organizations he is member of

I have a function that looks like this: ...