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
1
vote
0answers
27 views

Spark Scala: SQL rlike vs Custom UDF

I've a scenario where 10K+ regular expressions are stored in a table along with various other columns and this needs to be joined against an incoming dataset. Initially I was using "spark sql rlike" ...
6
votes
1answer
102 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
137 views

Return all anagrams for a word matched against a list of words in a file

The exercise description: Given a ‘words.txt’ file (attached) containing a newline-delimited list of dictionary words, please implement a small command line application capable to return all ...
4
votes
4answers
8k views

Realizing a SQL ResultSet into a Map in Scala

I am trying to realize a java.sql.ResultSet into a map, in Scala. ...
2
votes
1answer
30 views

Compressing string in Scala, how to do this immutably?

I have a string "abbbbccdddd" and the function should return "a1b4c2d4". This is what I have written in Scala. Iterative-version ...
1
vote
0answers
17 views

Scala code to insert a row into Hbase

Below is a case class and a companion object with a method putRecord which adds a new row into Hbase using Put API. This code has some repetitive parts. Is there a ...
2
votes
1answer
301 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 ...
1
vote
0answers
29 views

How to make it more Scala compatible?

When you write a code in python code people always ask to make it more pythonic. I am not sure if there is a term for scala or not. However, I am pretty sure my following code use one or two scala ...
3
votes
1answer
40 views

Scala: Cumulative String Tokenisation

I'm trying to split an incoming stream of strings into cumulative tokens per line item using a function below, ...
-1
votes
2answers
43 views

A better OOP way for solution

I am implementing a functionality in Scala to Copy files from one FileSystem to another ...
0
votes
0answers
29 views

how should I optimise the scala code and make it more readable

I have this ugly massive piece of code. It does many things, mostly around querying databases. Each query depends on the result of the previous query or on the parameters of the incoming request body. ...
4
votes
1answer
77 views

A simple data class based on an existing collection (Scala)

I'm fairly new to Scala and I would like to learn it properly. My current task is to create a type to represent a heap, as seen in interpreters: a mapping from addresses in memory to values stored ...
4
votes
1answer
66 views

Clean up tags from a text

I need to generate random text for tests. I do that using alphanumeric characters and some special characters as well. For some special cases, I cannot generate the random text with tags. I have the ...
2
votes
1answer
59 views

What is better to use for mapping value to Map of functions: tail recursion or foreach?

I want to map functions from my functions library (Map called as chain) to input string str. ...
1
vote
2answers
82 views

Count Substrings for a binary string

Problem is taken from leet code. Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings ...
2
votes
1answer
61 views

Integrating caliban, ZIO and Akka

I created a small sample project using caliban, akka-persistence and ZIO. I'm not really happy with the wiring code: ...
0
votes
1answer
44 views

implementation of atoi in scala

I've implemented atoi function as described in leetcode problem string-to-integer-atoi. I am looking for some feedback on writing this in a more scala native way ...
1
vote
1answer
70 views

Create JIRA ticket using REST API

Objective: The objective of the program is to create a JIRA ticket using a REST API. It's working perfectly fine without any issue. However I am not sure if it's up to good Scala standards. I would ...
2
votes
0answers
145 views

generic implementation approaches for a recursive function

Let's take the famous fibonacci problem as an example but this is a generic problem. Also, taking scala as the language as it's rather feature-rich. I want to know which solution you'd prefer. we all ...
6
votes
2answers
6k views

Construct date sequence in Scala

I want to have a continuous date sequence like ['2014-01-01','2014-01-02', ...] Then I define a stream to do that. ...
1
vote
1answer
51 views

Scala graph coloring

i have solved this problem https://leetcode.com/problems/find-eventual-safe-states/submissions/ ...
1
vote
1answer
207 views

Perfect Number usage in higher order function

I am new to Scala. I have been trying to complete an exercise where the ask was as follows: // Write a function isPerfectNumber which takes integer input and returns String output. // It finds if a ...
2
votes
0answers
37 views

Finite automaton library in Scala including subset construction and minimization

I built a scanner generator like flex or jflex in Scala for a class last year. Part of this project is a small library for handling and manipulating finite automata. It works completely as intended, ...
-1
votes
1answer
54 views

Functional filter unique elements in sequence is not as good as imperative [closed]

Can you please help me fulfill the following? Requirements: Create an iterable with unique elements. Their order should be preserved: a | b | a | c → a | b | c ...
2
votes
0answers
65 views

Bellman-Ford Implementation in Scala/Java

I think the part to get shortest path from the cost table got pretty messy. Also can use some tips on how to avoid the extra O(V+E) work checking all edges from source to dest after getting the cost ...
2
votes
0answers
61 views

Synchronous use of Mongo-Scala Driver

The Mongo-Scala Driver (v2.6) currently only supports asynchronous operations, although my use cases often seem to lend themselves well to synchronous reads. This may be to block downstream code ...
10
votes
1answer
12k views

BFS and DFS in Scala

I would love it if someone could give me some suggestions for these 2 graph search functions. I am new to scala and would love to get some insight into making these more idiomatic. ...
3
votes
1answer
373 views

Dotty 0.19 (Scala 3) features: contextual abstractions

tl;dr I tried to write some as general and as feasible as possible code where entities are able to communicate with other entities of the same type or of different types, allowing for the recipient ...
3
votes
2answers
102 views

Solve membership binary mask problem in Scala

Imagine you have two string sequences: val s1 = Seq("c", "a", "b", "z", "a", "b") val s2 = Seq("a", "b") You need to come up with an algorithm that generates a ...
2
votes
2answers
233 views

Scala app to transpose columns into rows

This is the first application or really any Scala I have every written. So far it functions as I would hope it would. I just found this community and would love some peer review of possible ...
3
votes
1answer
60 views

Counting bigrams in XML files in a directory tree

I've written this code in Scala that extracts bigram statistics from the Reuters dataset. It puts the statistics in a Map[String, SortedMap[String, Int]]. For ...
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 ...
0
votes
1answer
82 views

Provide functional-programming style solution for 2D Array - DS challenge

Problem Statement: Given a 6×6 2D Array, arr: 1 1 1 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 We define an hourglass in A to be ...
0
votes
1answer
149 views

Imperative and functional-programming solutions for Jumping on the Cloud challenge

Problem Statement Emma is playing a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud ...
3
votes
2answers
175 views

Count character 'a' in first n characters of indefinitely repeating string s

Problem statement: Lilah has a string, s, of lowercase English letters that she repeated infinitely many times. Given an integer, n, find and print the number of letter a's in the first n ...
4
votes
2answers
154 views

Counting valleys traversed below sea level, given elevation changes

Problem Statement: Gary is an avid hiker. He tracks his hikes meticulously, paying close attention to small details like topography. During his last hike he took exactly steps. For every step ...
3
votes
0answers
2k views

Adding columns in Spark dataframe based on rules

I have a dataframe df, which contains below data: ...
4
votes
1answer
71 views

Print a Connect 4 grid based on some rules for each cell

In a game of Connect4: we start with an empty grid two players place pieces X and O on the grid the first player to achieve 4 pieces in a line wins! this is a text based console game Here is the ...
-1
votes
1answer
48 views

Perform KS-test on Random Number Generator [closed]

I've just started practicing Functional Programming in Scala in some days. This is the code I used to perform KS-test on a random number generator. ...
0
votes
1answer
64 views

Is it possible to DRY these 2 methods of a tailrec Node Tree?

I have a Tree structure composed by Node class. I have to @tailrec method that are visiting up to the root. Those 2 methods ...
1
vote
1answer
57 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 - ...
2
votes
1answer
341 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 ...
3
votes
3answers
148 views

Computing average duration per user from a large CSV file that doesn't fit in memory

We have a large log file which stores user interactions with an application. The entries in the log file follow the following schema: {userId, timestamp, actionType} where actionType is one of two ...
5
votes
1answer
598 views

Extracting numeric values from a string containing key:value pairs

I am writing function numericValues(text: String): List[Int] to extract patterns """([a-z]+)\s*:\s*(\d+)""" and return the list ...
1
vote
1answer
42 views

Page download with reattempts in Scala

I wrote the following code to deal with possible network problems. ...
1
vote
0answers
68 views

Java classes to load configuration data from a JSON file, with Scala tests

I want to implement classes in a project that will support the loading of configuration data from a JSON file. The JSON file is in the following form. Please be as critical as you can be. I'm a junior ...
1
vote
1answer
97 views

Minimum steps to reach target by a Knight In Scala

Given a chessboard of N size (square matrix), the position of Knight and position of a target, find out minimum steps (both count and exact steps) from start to target for a Knight. If it is not ...
1
vote
0answers
126 views

KMP algorithm in scala

I implemented KMP pattern matching algorithm in Scala. My code works but it looks more imperative and a scala translation of C/C++ implementation. I am not able to figure out how to manage multiple ...
4
votes
2answers
93 views

Replace array element with multiplication of neighbors in Scala

Given an array of integers, update the index with multiplication of previous and next integers, Input: 2 , 3, 4, 5, 6 Output: 2*3, 2*4, 3*5, 4*6, 5*6 Following ...

1
2 3 4 5
10