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.

0
votes
0answers
25 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 ...
0
votes
0answers
23 views

Http4s custom header with JSON payload

This is a custom http4s header based on the headers that are already provided which takes a io.circe.Json as its value. I'm ...
1
vote
1answer
58 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
45 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
48 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
42 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
27 views

Add an attribute to specific XML Elements in Scala

supposed I have an XML like ...
0
votes
0answers
41 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
70 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
87 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 ...
3
votes
1answer
89 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
47 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
59 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
56 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
165 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
47 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
219 views

Replace Every JSON Key's Instance with Value

Given the following using the Ammonite REPL: ...
3
votes
0answers
47 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
389 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
234 views

WebSocket example with akka-http

Akka-http supports WS connections like that: ...
1
vote
0answers
38 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
54 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
131 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
185 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
56 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
88 views
0
votes
1answer
798 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
42 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
75 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
110 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
284 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
633 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
164 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
296 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
1k views

Removing stop words from a Spark Dataframe

I am trying to apply a function to two Spark Dataframes (in Zeppelin): ...
5
votes
2answers
159 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
74 views

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

I have a function that looks like this: ...
2
votes
1answer
68 views

Project Euler 23 in Scala (non-abundant sums)

I recently overhauled my solution to Project Euler Problem 23 (which asks for the sum of all positive integers which cannot be written as the sum of two abundant numbers) to move it from an imperative ...
0
votes
1answer
61 views

Report a status based on a list of results

In one of my services, I have the function as presented below: ...
-1
votes
2answers
377 views

Implementation of linked list in Scala

Here is my implementation of a linked list in Scala.It is a very basic implementation to understand generics and use of Option in Scala. Please let me how I can ...
2
votes
0answers
55 views

Scala code that loops through Java annotations decorating Scala classes

I have code written in Scala that reads annotations decorating other Scala classes. These annotations are written in Java. The sticking point is that annotations don't allow advanced data structures, ...
1
vote
0answers
49 views

Purely functional octree and octree priority queue

I'm programming a game in Scala, and a lot of tasks are able to be done in the background. I've created a system in which each Future can be assigned a position in 3d space, and they are stored in an ...
3
votes
1answer
129 views

Grab first successful pair from a list of maps

I have a function that parses geographic coordinates through a list of different providers. In this case I want to look up two addresses. The main problem of this question is - given a List of of ...
6
votes
0answers
174 views

Functional K-way merge (Merge k sorted sequences)

I've implemented a functional k way-merge algorithm to merge k sorted sequences of any type T. I used scalaz's Heap since the Scala standard library does not provide an immutable heap. Any suggestions ...
3
votes
2answers
130 views

Using a radix tree to find case-insensitive matches in large files

I'm using a radix tree library to find case-insensitive matches from several relatively large files (~24 files each on the order ~150MB). My current algorithm requires me to read the chars of an ...
2
votes
1answer
238 views

HackerRank Equal with Scala

For studying Scala, I wrote some code to solve the Equal problem on HackerRank. But I am stuck on test case #15 due to timeout. Christy is interning at HackerRank. One day she has to distribute ...
3
votes
2answers
53 views

Naming a not-quite-flatMap method

I recently created Argyle, a small library for parsing command-line arguments in Scala. I'd be happy for anyone to take a look and provide general feedback on it. But there is also one specific issue ...
0
votes
2answers
61 views

Extract value from scala collect case

I have the following code which reads data from a key/value data storage. At some stage I want to go through everything in the storage and check expiry times and remove values - as done in the code ...