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
3
votes
1answer
2k views

All combinations of 2-value variables

Introduction This code was part of one of my projects where it was used to generate boolean input values for truth tables. I have since generalized it for combinations of variables with only 2 values,...
8
votes
1answer
254 views

Sieve of Eratosthenes performance; Scala very slow compared to Node.js

I am new to Scala so it might show. I am learning it for one of my classes and have to write a performance benchmark. I have written the Sieve of Eratosthenes in both Node.js and Scala, and my Node.js ...
6
votes
0answers
311 views

Kattis problem Amanda Lounges

I wrote this solution to Kattis problem Amanda Lounges in Scala. TL;DR: It's a graph theory problem where I read in a list of edges from stdin and try to compute the minimum number of nodes that ...
2
votes
1answer
884 views

Find maximum distance between elements of an array

I have written a program in scala which actually does the below For every number between the maximum and minimum value of the array calculate the distance for each and every element in the array ...
2
votes
2answers
61 views

Alternate Tree implementation

I am trying to implement algorithms related to trees in Scala. I have dabbled with the following implementations of a tree. Are there any pros and cons of preferring the non-...
5
votes
0answers
64 views

HListSum - Sum of HList's Nat's

I wrote HListSum, whose type parameter is an input HList. Its Out type member represents the ...
2
votes
1answer
174 views

Sum of HList (where all elements are Nat's)

I wrote the following to output, at the type level, the sum, Nat, of an input HList: ...
3
votes
3answers
1k views

Find next prime

I wrote small app finding next prime number starting from number passed as argument: ...
3
votes
0answers
38 views

Simplified Kata of BankOCR using Shapeless

I attempted to solve a simplification of User Story 2 using shapeless: Namely, define the implementation of: ...
3
votes
1answer
262 views

Haskell Parsec library implementation in Scala

I've been developing a compiler for a programming language I'm working on in Scala, and after having used Haskell's Parsec a lot for this sort of thing in the past I decided to reimplement the library ...
2
votes
1answer
189 views

Functional API wrapper written in Scala

I've been working on an small API wrapper for the GitHub API using Scala (full source on GitHub). I'm still very new to the language, so I was hoping to get some feedback about a couple of design ...
4
votes
1answer
432 views

Heap Sort in Scala

Trying to implement Heap Sort in Scala ...
2
votes
0answers
134 views

Reduce sample rate of GPS data based on distance between points

The algorithm needs to reduce an RDD[GPSRecord] based on the distance between several points, e.g. "give me only GPS records when the distance between them exceeds ...
0
votes
1answer
161 views

Scala implicit conversion to add methods to Int

I started learning Scala, and like most functional languages, things get messy pretty quickly for a beginner. Here I have built a wrapper class for Ints so that I ...
4
votes
1answer
268 views

Implement `Penultimate` in Shapeless

I attempted to define Penultimate, i.e. a type class for shapeless.HList's that have a second to last element. ...
1
vote
0answers
46 views

cake pattern object assembly - using a builder function in combination with the cake pattern

I am new to the cake pattern, this is my first attempt at using it. Am I doing something wrong if I use the apply method in ...
1
vote
0answers
45 views

Cake Pattern with implicitly added functionality

I have a scenario where I want to implement a variant of a Cake Pattern, but adding implicit functionality to a class (a Spark DataFrame). So, basically, I want to be able to run a code like the ...
-1
votes
1answer
844 views

Cartesian product in Scala

I'm using this code to compute a cartesian product: ...
2
votes
0answers
142 views

Calculating Pi to a tolerance

I'm taking my first steps in the Scala world, though I'm not wholly unfamiliar with functional style. Apparently one way of calculating Pi is to sum the terms of the infinite series: $$4 * (\frac{1}{...
1
vote
1answer
479 views

Singly linked list in Scala

I've written a basic implementation of a singly linked list in Scala. I would appreciate any feedback and improvements (especially the remove() method, I feel like ...
1
vote
1answer
71 views

Extends Scala iterator's drop method to allow for drop(x: Long)

I have a need to drop quantities of elements from an iterator beyond Int.MaxValue so I wrote a helper function that does just that. However, I can't help but feel ...
0
votes
1answer
481 views

Sort Stack using just an additional stack

The problem is from the cracking coding Interview programming book. The problem is that: Sort Stack: Write a program to sort a stack such that the smallest items are on the top. You can use an ...
5
votes
0answers
662 views

Akka Streams Flow through a cache

Assume I have a cache: ...
1
vote
1answer
105 views

Finding a passcode by comparing MD5 result

This function finds the 8 char passcode from a given "doorId". Is there a way to find the passcode in an undetermined number of steps without using a while? Maybe ...
2
votes
2answers
680 views

Intersection function in Scala

The problem is: Intersection: Given two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. Note that the intersection is defined based on reference, not ...
2
votes
2answers
492 views

Check if a linked list is a palindrome in Scala

The problem: Palindrome: Implement a function to check if a linked list is a palindrome If it's a palindrome, the list must be the same backwards and forwards Below is the code I wrote: ...
1
vote
3answers
904 views

Sum Lists function in Scala

Sum Lists: You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1 's digit is at the head of the list. ...
0
votes
1answer
297 views

Check Permutation in Scala

The problem is: Given two strings, write a method to decide if one is a permutation of the other. I wrote the following code in scala, may I know any other optimization one? ...
4
votes
1answer
913 views

Merge sort in one Single Function in Scala

I have implemented a sortmerge in Scala in one single function. I would like to have it reviewed, so that I can have it well organized, and it is then doing what ...
3
votes
1answer
118 views

MarsRover in Scala

This is my first attempt at Scala programming. I tried to be functional but I'm not sure if I have achieved that. DataTypes.scala ...
1
vote
1answer
451 views

Print Connected Components Scala

Given a file containing adjacent IDs: i1, i2, i5 i3, i4 i2, i6, i7 i4, i8 i9, i3 I would like to print each of the connected components: ...
2
votes
1answer
285 views

FizzBuzz in Scala

As the title suggests, this is my FizzBuzz implementation in Scala. I started learning Scala (and functional programming in general) in small steps about two months ago, and one recent assignment ...
4
votes
1answer
470 views

A general-use For-comprehensible Loan Pattern

I use Scala's loan pattern in order to ensure and automate the resource aquisition and release after its use. When multiple resources have to be acquired at once, however, the function calls must be ...
3
votes
4answers
151 views

Displaying nested arithmetic expressions as a string using Scala pattern matching

I am learning the concepts of pattern matching in Scala. Following is an exercise for the same. The task is to define a show function that outputs an expression as ...
2
votes
1answer
859 views

Generating Fibonacci sequences in Scala

Would you consider this to be idiomatic Scala? If not, what would you do to improve it? ...
14
votes
1answer
305 views

All RGB colors in one image

This is a program to solve: Images with all Colors from Code Golf: Make images where each pixel is a unique color (no color is used twice and no color is missing). Create the image purely ...
2
votes
2answers
590 views

Sum of multiples of 3 or 5 below 1000 in Scala

I'm in the process of learning Scala, and started working through a series of coding puzzles that I've done previously in other languages. For example, this solution, coded in Erlang: ...
1
vote
1answer
483 views

Convert a List of graph edges to a Map of neighboring nodes

What would be best way achieving this? Is it possible to use Immutable map? ...
3
votes
1answer
86 views

Project Euler #12 in Scala — Highly divisible triangular numbers

I generally work in Python for programming competitions, but I'm teaching myself Scala. As an exercise, I've attempted Project Euler problem #12 in Scala. I hope that somebody familiar with Scala can ...
3
votes
0answers
187 views

Simple picture app for storing and viewing

Update: git repo: https://github.com/muhsinali/picture-example-minified This is my first project that I've written using Scala and the Play framework. It's a simple application that allows the user ...
1
vote
2answers
107 views

Taking some JSON data, converting the date format and calculating a user average

I'm originally a Java developer taking a stab at some Scala development. One of the main reasons I'm posting is because I know that in Scala you can write your code in a Java (OOP) way and it will run ...
2
votes
2answers
332 views

A Scala Maze Generator in Functional Style

I'm wondering if there is more I can do to incorporate more idiomatic scala and functional programming principles. I know the maze itself is mutable but i didn't see an easy solution to making it ...
2
votes
1answer
83 views

Building and querying an immutable phone book

I wanted to get your opinion on this immutable implementation of a Phone Book using recursion to process a stream. I am especially interested in ways to speed up the code execution. Here is an ...
1
vote
2answers
105 views

Record parsing to Scala object with some unknown fields

I am getting some customer data but don't know the complete schema beforehand. I want to parse this data and create a Scala object from it. But due to unknown fields in the record, I fill only the ...
4
votes
0answers
80 views

Distributing work in scala

Below is a script to distribute jobs amongst workers while trying to maintain an even distribution of work and considering that not every worker can do every task. The solution I used is to find ...
1
vote
1answer
112 views

Triangle numbers finder in Scala

I'm new to Scala and learn it by solving some Project Euler problems. Here is the solution to problem 42 from Project Euler. The task is to read words from file, then sum ordinal numbers of each ...
0
votes
1answer
155 views

Extract Element and its Followers from List

I wrote the following function to convert List[Int] => List[List[Int]]. Its intended purpose is to extract 1, followed by ...
3
votes
1answer
86 views

Classifying and counting database entries using Scala map and flatMap

I am new to Spark and Scala and I have solved the following problem. I have a table in database with following structure: ...
2
votes
1answer
276 views

List-manipulation exercises in Scala

I have been using Scala as a means to learn Functional Programming and writing better code. However, I have some questions regarding some exercises or if there's a better way to approach them. In the ...
6
votes
2answers
535 views

Monte-Carlo method to estimate Pi runs slower in multiple threads, than in single thread

I'm learning Scala, and I wrote a program to estimate the value of Pi. When I'm using multiple threads, it takes 5-6 times longer to calculate the same iterations. I wrote a similar program in Java, ...