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.

Filter by
Sorted by
Tagged with
0
votes
2answers
58 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 ...
5
votes
1answer
80 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
862 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
758 views

WebSocket example with akka-http

Akka-http supports WS connections like that: ...
1
vote
1answer
58 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
257 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
392 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 ...
2
votes
1answer
456 views

Reading a certain number of lines from a file

...
1
vote
1answer
3k 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
1answer
70 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
103 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
132 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
668 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 ...
3
votes
0answers
1k 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-100 MB. I would to compact them so as to have files at least say 100 MB or 200 MB. The logic of my code is to: * find a ...
1
vote
1answer
307 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 ...
2
votes
3answers
573 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
3k views

Removing stop words from a Spark Dataframe

I am trying to apply a function to two Spark Dataframes (in Zeppelin): ...
5
votes
2answers
206 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
95 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
82 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
63 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
1k 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
65 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
85 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
136 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 ...
7
votes
1answer
253 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 ...
4
votes
2answers
168 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
302 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
63 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
84 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 ...
4
votes
3answers
102 views

Compare ZIP codes, disregarding leading zeros

I have a function that compares ZIP codes. It also has to handle cases where the input ZIP code be missing leading zeros, have extraneous leading zeros. This is what I've come up with. I don't have ...
5
votes
1answer
130 views

Scala Docx generator from CSV input, using the docx4j library and Scala Swing

I am working on an application which takes as input a CSV file containing a list of attributes and a .docx template containing variable names equal to the columns ...
1
vote
0answers
68 views

Choosing names for “logic” and “implementation” traits in Stackable trait pattern

I have a trait that is to be mixed-in with existing types of Actor classes in Akka and offer a certain functionality. To offer better maintenance, I am using the stackable trait pattern. The ...
4
votes
1answer
133 views

List replication problem from Hacker rank

Problem: Given a list, repeat each element in the list n amount of times. Solution: ...
4
votes
2answers
89 views

Find the minimum number of operations (concat, add and subtract) for the numbers 1-9 to equal 100

I am beginning to learn Scala and functional programming, and I came across this puzzle. This looked like good practice and I have a solution but just want some hints in terms of functional approach ...
1
vote
0answers
91 views

Purely functional minecraft-like mesh compiler

I'm creating a minecraft clone (for practice), in scala, using largely functional programming. When a chunk doesn't have a mesh loaded into VRAM, it create a Future for the vertex and index arrays, ...
6
votes
1answer
497 views

Convert time 24hr format into time as words

I'm currently writing a Scala Object that converts time 24hr format (i.e. 00:00 - 23:59) into Time As Words (i.e. Eight o'clock, Half past eight, Quarter to nine etc). How can I refactor the current ...
4
votes
0answers
82 views

Checking membership of object in set without pattern matching

I'm curious in how to best structure code that checks for membership of an instance of an ADT in a set of elements without using pattern matching. Given the following ADT, methods and functions, ...
1
vote
2answers
118 views

Find the most frequent character in a string

I'm trying to solve this problem https://community.topcoder.com/stat?c=problem_statement&pm=40 Given a string made up of ONLY letters and digits, determine which character is repeated the ...
1
vote
0answers
60 views

Sample Banking System Deposit Function

I am working on a sample banking system and have the following for deposit functionality, I was wondering if this is idiomatic, and how it can be improved. I have separated the algebra of the service ...
3
votes
0answers
2k views

Adding columns in Spark dataframe based on rules

I have a dataframe df, which contains below data: ...
2
votes
0answers
215 views

Scala Performance in Solving Sudoku

I translated Peter Norvig Python Sudoku solver to Scala and my Scala implementation was slower in the order of few magnitudes e.g. instantaneous, in Python, vs 4 to 5 seconds. I suspected I didn't ...
0
votes
1answer
68 views

Translate boolean flags to collection of numbers in scala

I'm having trouble to implement the following requirement in a functional way in scala. Given a predefined number of boolean flags, I want to build up a collection of numbers. For each (true) flag, a ...
3
votes
1answer
146 views

Scala Codility GenomicRangeQuery performance

I solved Codility's GenomicRangeQuery challenge in Scala. We are given a long DNA sequence (up to 100000 bases), where A, C, G, T are assigned values 1, 2, 3, 4, respectively. There are up to 50000 ...
2
votes
1answer
88 views

Generate a sequence of coin flips until pattern is hit

A book on probability says that a sequence of heads and tails is generated by a fair coin asking what is the probability that the pattern T, H, H, H occurs before the pattern H, H, H, H. I have made a ...
3
votes
1answer
642 views

Producer - multiple consumer implementation using futures

I've tried to implement a producer-multiple consumer pattern using futures and I was wondering should it ever deadlock, and if it should, why? I have this method ...
1
vote
0answers
87 views

Scala Functional Programming ODE Solver

I'm new to both Scala and Functional programming. I've been doing the excellent course Functional Programming Principles in Scala on Coursera and I want to test out what I've learned. I've written a ...
5
votes
1answer
155 views

Bowling game Kata in Scala - pattern match

This is a functional approach to problem described by standard Bowling Game Kata - to implement the rules of calculating a score in a bowling game. The inspiration was the implementation by Uncle Bob: ...
0
votes
1answer
971 views

Re-inventing the wheel: Binary search in Scala

I am trying to implement the code described in this post about how Reddit uses binary search to identify duplicate user names, in Scala. Just to clarify, this is mainly for me figuring out how these ...
1
vote
0answers
69 views

Extending a simple Tree parser and lexer

Yesterday, I decided to learn about scala's Parser-Combinator library. After a while, I was able to write a simple Tree parser and lexer which understands trees written like this: ...