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.
497
questions
2
votes
1
answer
228
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
3
answers
2k
views
Find next prime
I wrote small app finding next prime number starting from number passed as argument:
...
3
votes
0
answers
39
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
1
answer
324
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
1
answer
434
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
1
answer
723
views
Heap Sort in Scala
Trying to implement Heap Sort in Scala
...
2
votes
0
answers
170
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
1
answer
391
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
1
answer
295
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.
...
0
votes
1
answer
2k
views
Cartesian product in Scala
I'm using this code to compute a cartesian product:
...
2
votes
0
answers
230
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
1
answer
740
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
1
answer
76
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
1
answer
631
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
0
answers
898
views
Akka Streams Flow through a cache
Assume I have a cache:
...
1
vote
1
answer
112
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
2
answers
1k
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
2
answers
1k
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
3
answers
1k
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
1
answer
465
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
1
answer
2k
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
1
answer
173
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
1
answer
570
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
1
answer
536
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 was ...
5
votes
1
answer
753
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
4
answers
191
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
1
answer
1k
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
1
answer
434
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
2
answers
1k
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
1
answer
562
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
1
answer
104
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
0
answers
200
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
2
answers
136
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
2
answers
589
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
1
answer
100
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
2
answers
175
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
0
answers
108
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
1
answer
276
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
1
answer
203
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
1
answer
105
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:
...
3
votes
1
answer
425
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
2
answers
1k
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, ...
0
votes
1
answer
172
views
Generic Int conversion to scalar
I'm new to Scala and I'm trying to write a generic function to convert from Int to any scalar.
For example:
...
4
votes
1
answer
194
views
Solving a digitwise addition equation in Scala
I've written the following codes in Scala and Java to solve cryptarithmatic problems.
E.g. For SEND+SOME=MONEY, assign the digits 0 to 9 such that the equation holds true.
The algorithm I've used is ...
1
vote
2
answers
257
views
1
vote
1
answer
2k
views
Building a query URL in Scala
I'm working on a webapp in ScalaJS and want to create a query url for requesting some JSON.
Right now I'm using a method called urlBuilder to take the query options from an Options case class and ...
0
votes
1
answer
79
views
Unifying trait for tabular data in Scala
This trait is meant to be a simple interface for tabular data from different file types, such as csv, excel, open office etc. Right now I'm only asking about some of the top level code for now and ...
0
votes
1
answer
6k
views
Unit testing Spark transformation on DataFrame
Looking for suggestions on how to unit test a Spark transformation with ScalaTest. The test class generates a DataFrame from static data and passes it to a transformation, then makes assertion on the ...
8
votes
1
answer
363
views
Game of Life in Scala with a sparse representation
While reviewing another Scala implementation of the Game of Life, I ended up drastically rewriting it. The board is represented as a Set[(Int, Int)] containing the ...
1
vote
1
answer
145
views
Parsing latitude and longitude, using Try[T] to ignore errors
I wrote this question asking about how to implement my error handling while still maintaining functional code and based on 200_success's very interesting note on the spaces, I decided to ...