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.

learn more… | top users | synonyms

5
votes
3answers
129 views

Replacing all occurrences in a String

I have a string like this : val content = "some_text{macro1}another_text{macro2}text" I want to replace the {macro1} and ...
0
votes
0answers
31 views

Idiomatic exception handling for socket connection [migrated]

I'm trying to understand how I can elegantly use scala.util.control.Exception package. To be more specific I want to convert this piece of Java code to functional ...
3
votes
1answer
40 views

Safe cracker string with all combinations

Imagine a safe with a 4-digit code, and accepting a continuous stream of code entries, such that when the 4 digits are seen in the right sequence, the safe opens. Generate a short string that ...
1
vote
0answers
25 views

Find minimum number of coins (count and list of coins too)

For a given set of denominations, you are asked to find the minimum number of coins with which a given amount of money can be paid. Assume that you can use as many coins of a particular ...
4
votes
0answers
24 views

Selecting subset of size k using recursion

Sometimes I need to implement recursive algorithms that pass a certain state from one recursive call to another. For example, a greedy subset selection: we have a set of candidate objects, and we ...
7
votes
3answers
294 views

n-digit Verification Code Generator

This is a verification code generator, which generates n-digit numeric strings. (code can be 0000, so I chose String as a return ...
4
votes
2answers
51 views

Simple load balancer model in Scala

For one of my cases in algorithms I am required to create a model of a load balancer model for multiple processors. Lately I became interested in Scala so I decided that it would be great to create ...
2
votes
2answers
31 views

Tail-Recursion to get a Map of word counts

I want to read a file and store the number of occurrences of each word in a Map, using tail recursion. I came up with the following; it seems to work; does it look like it's right? ...
1
vote
1answer
44 views

Graphs processing in Scala

I've just started to learn Scala and decided to implement small graph library to train. Here is the basic version, that describes graph and nodes structure and also provides implementation of ...
2
votes
1answer
48 views

Playframework (Scala) custom exception handling

I am using play framework with scala and here is a scenario on which I would like to have your opinion. Let's say I have 2 ...
2
votes
0answers
26 views

Functional, Loaner-Like Pattern For Consuming SQS Queues, Deleting Queue Items On Success

My program fetches input from a RabbitMQ or Amazaon SQS-like queue. There is not always input available in the queue. If a message is available, it triggers work. If that work is successful, ...
2
votes
1answer
66 views

Memoization of Fibonacci using generic Int => Int helper

I'm trying to understand memoization using Scala. I took the Fibonacci sequence as an example because of the high cost of recursively computing numbers in the sequence without memoization. ...
3
votes
1answer
52 views

Random Plinko path using functional programming style

In order to learn both scala and functional programming, I've been rewriting the projects from my introductory CS class in scala. One of the projects was a plinko game, where the user picks a slot to ...
11
votes
2answers
116 views

Converting imperative style user input menu to functional programming style

I am currently trying to teach myself scala by rewriting projects from my intro CS class, which was taught in C++. In addition to just learning the syntax I am trying to gain understanding of how ...
3
votes
1answer
48 views

Returning preferences

I have a function that returns some preferences, but first I have to do some checks to see whether the user is authorised to read them. I'm still not happy with the result (the match is a bit ugly). ...
1
vote
1answer
55 views

File-to-fully qualified class name function

My instincts are telling me a seasoned Scala programmer will find my code less than optimal. As will no doubt be obvious, I'm a Java guy who has just started in with Scala. Is there a more ...
1
vote
0answers
36 views

NonEmptySortedSet implementation

I'm looking for feedback specifically about what I might add to this to make it more usable or general, especially with regard to scala collection traits to implement. ...
1
vote
1answer
102 views

Extracting tuples from Excel sheet

My code have too much nested option processing which makes it ugly. But it has an advantage it has no side effect and typesafe. ...
10
votes
2answers
81 views

Test runner for Scala problems

I'm learning Scala and could use another set of eyes on the code below. I'm creating a test runner for the 99 scala problems set. I figure between actually solving the problems and working through the ...
1
vote
0answers
39 views

Using Java's Fork/Join library from Scala

I am a newbie trying to learn some Java and I am reading prof. Dan Grossman's introductory lecture notes on parallel and concurrent algorithms. I wrote a version of a divide-and-conquer algorithm for ...
3
votes
2answers
311 views

BFS Maze Solver in Scala

I recently got a Scala developer position, so I'd like to get more comfortable with Scala on my own. Here is a maze solver that I wrote (previously in other languages) that I translated to Scala. Any ...
3
votes
1answer
20 views

Gathering response from a pagable service

I'm wondering how this code could be improved. I especially don't like the use of mutable Buffer but not sure what the best way to get it out cleanly. ...
1
vote
0answers
205 views

Scala TryWith that closes resources automatically

I'm quite new to Scala and was surprised that there doesn't seem to be anything like Java's try-with-resources block. So of course I wrote my own little construct. Is there anything I could improve? ...
2
votes
1answer
258 views

Async Controller Play 2.3

Is this example the correct way of writing an Async controller end point in Scala using Play 2.3? ...
2
votes
2answers
90 views

Tail Recursive Fibonacci

Please critique my implementation of a tail-recursive method for generating the Fibonacci sequence: ...
1
vote
0answers
45 views

A Timer Construct to Simplify Timing Blocks of Code

This is fairly simple, so I'm just looking for ideas on anything that can be cleaned up, places it can be extended, and suggestions regarding style/naming (I'm terrible at naming). It allows you to ...
16
votes
2answers
264 views

API for looking up Japanese postal code

Most of my experience is in Python, Java and embedded C, and I need to learn Scala and Play for a new job. As an exercise, I decided to create a very simple API for looking up area by postal code, ...
1
vote
0answers
73 views

A Simple Thread Pool in Scala

I wrote the following that spawns n threads, uses them to process a queue of jobs, then returns a result. As well as any general suggestions, I'd like feedback on the following: How safe is ...
0
votes
0answers
23 views

Gathering emails, phones, URIs, and tag related to a room

I've made a strange method with multiples conditionals, creating a lot of Lists and I was wondering if I could write it in a better and more legible way. ...
4
votes
2answers
92 views

“Functionalizing” this tree walking method and cleaning it up

I have a MessageTree class that represents a binary tree of negative/positive. I'm looking to improve my walkTree method and ...
2
votes
2answers
72 views

List.containsSlice

In an effort to strengthen my functional programming, I'm reading Functional Programming in Scala. One of the assignments is to implement a function that does the same operation as ...
2
votes
3answers
95 views

Refactor jaccard similarity the “Scala way”

I'm trying to pick Scala up. This is a simple heuristic that checks a similarity value between two sets. I've done this a million times in Java or Python. The function works, but I'm certain I am not ...
3
votes
1answer
86 views

A simple “Tamagotchi” like game that uses math questions as food

I wrote a Tamagotchi-like game where to feed the pet, you do math questions instead of just pressing a button. It's persistent, in that on loading, it figures out long it's been since the last save, ...
6
votes
3answers
295 views

Checking for balanced parentheses

I wrote a method to check if each parenthesis in a string is closed. It should take into account the order in which a parenthesis is presented, e.g. "hello)goodbye(" should return false even though ...
2
votes
0answers
104 views

Scala BitSet implemented with Java BitSet, for use in Scala Enumerations to replace ValueSet

Due to a performance profiling hotspot detailed here, I implemented my own BitSet using Java's BitSet. This is intended to ...
14
votes
1answer
117 views

Oh ye beloved bejeweled

Before @bazola asked his recent question we chatted about it for a bit. I figured that I could try implementing something similar myself. As I've heard that Scala has all the advantages of Java but ...
1
vote
1answer
85 views

Scala Case Classes

Please take a look at the following Scala program and give me suggestions for improvement. I'm sure there would be plenty. This is my very first Scala code, so please don't be frustrated because of ...
2
votes
3answers
197 views

A Cipher with 2 methods for encryption/decryption

I'd like advice on how this could be written more efficiently, idiomatically, or just anything that could be improved upon. I'd also like to know if there's an alternative to my ...
1
vote
0answers
152 views

Idiomatic Scala for nested map/flatmap

Does anybody have an idea on how to make this code more idiomatic? I tried to use scalaz Traverse, but it didn't really help. Given the types and method signatures: ...
11
votes
1answer
120 views

First four Project Euler in Scala

I was working on teaching myself some Scala and decided to tackle some Project Euler problems. The first four of these turned out to be one liners. ...
5
votes
3answers
74 views

Generating a game board with squares in a rectangle

I am trying to generate a game board (similar to a Monopoly board). I am new to Scala and am thinking that there may be a better way to do this. But improvements to the code are also welcome. ...
3
votes
2answers
175 views

zipWith function in Scala

My implementation works, but it looks very ugly. Curious to see how it's better implemented. The zipWith function takes a list of lists (...
2
votes
3answers
74 views

Removing nested blocks from a string

I wrote this function in scala that uses tail recursion to remove nested blocks from a text. Usage examples: ...
2
votes
1answer
64 views

Dealing with state

I am working on something that involves the observer pattern. A simple version of that looks like this: ...
10
votes
2answers
213 views

“Pure” functional simple Scala battle game simulation

I am a long time imperative programmer (mostly C++) taking my first forays into functional programming with Scala. I am following some online tutorials about functional programming and Scala and I'm ...
3
votes
1answer
88 views

Ported C# scheduling to Scala

I'm taking a design workshop and we've received a short task: To parse an input file of processor jobs. To simulate scheduling of those jobs on a parallel computer (each job needs X processors) The ...
2
votes
1answer
42 views

Access case class parameter inside a map with default [closed]

I have a map whose values are case classes, and I want to access one parameter of that case class with a default value if they map doesn't contain the provided key. ...
5
votes
2answers
210 views

Simple search engine using TF-IDF (200 LOC)

I wrote simple search engine based on TF-IDF statistic and cosine similarity. I am still learning Scala, so would be glad to hear comments (here or at GitHub) regarding my code from Scala ...
2
votes
2answers
241 views

Comparing equivalent Scala and C# string-manipulation code - with Scala running much slower

I am very early in my journey of learning Scala, and I was doing challenges on hackerrank.com to get used to the syntax. As I finished one (very trivial) challenge, I noticed that the run times seem ...
10
votes
2answers
114 views

Value Mapper service

I have some data I'm getting from an external source that I don't control and have been told that I have to map some of it to "user friendly" display values. So I wrote a little utility service to do ...