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
25
votes
7answers
5k views

First Scala FizzBuzz implementation

Would love some feedback on this. I'm coming from a Java background and kinda feel like I've just done exactly what I would do in Java. Is there a better way? ...
19
votes
4answers
409 views

Ising Model - Atom Vibration Simulation in Scala

I am an experienced Java developer (12+ years) and have recently switched to Scala and I love it. However I feel not comfy yet and I have a feeling that I might use to many paradigms from the good old ...
19
votes
2answers
1k 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, ...
18
votes
1answer
5k views

Code with many “early returns (exits)” into the functional style

I have some Scala code that uses Internet to authorize a user. Therefore, it can throw Exceptions like IOException in the method. The original code was written in ...
16
votes
1answer
2k views

Simple anagram finder using Scala and some Scalaz

I'd like to hear some thoughts on my simple anagram finder, written in Scala. I mainly wrote this as a first go at writing some serious Scala, and I wanted to try out the Scalaz library. I'm coming ...
16
votes
1answer
578 views

TDD and function injection

I'm trying to come up with a scalable way of TDD'ing my Scala code, in particular managing dependencies. In this example, I've TDD'd part of the classic river crossing problem where we have people on ...
15
votes
1answer
230 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 ...
14
votes
1answer
303 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 ...
13
votes
1answer
277 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. ...
12
votes
3answers
380 views

Kosaraju in Scala

I started coding in Scala some time ago, and also learning some fundamental algorithms in CS. Here's a terribly slow implementation of Kosaraju algorithm to find strongly connected components in a ...
12
votes
1answer
788 views

Implementing a generic and covariant Set in Scala

I'm struggling with manually implementing a simple purely functional Set that's also generic and covariant on its type parameter: ...
12
votes
1answer
4k views

Simple wrapper around Java's File & FileInputStream

In an attempt to begin learning Scala, I've taken a stab at creating a simple wrapper around Java's File & FileInputStream in order to read lines from a file. This functionality already exists in ...
12
votes
0answers
492 views

Binary Search Tree Monad Implementation

I've written a Binary Search Tree Monad in Scala. I would like to hear your thoughts on how to improve it (e.g. making insertion/deletion/search faster and more scalable). Also, is there a better way ...
11
votes
2answers
4k views

Sqrt (square root) function

As an exercise in learning Scala, I implemented a square root function like this: ...
11
votes
1answer
1k views

Scanner in Scala

I wanted to implement Java's Scanner in Scala. The goal for this class is to: Implement a Scala collection interface (probably ...
11
votes
3answers
697 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 ...
11
votes
4answers
898 views

Alternate form of BufferedInputStream

I was having a problem with the TCP/IP socket communications in a web app where the server that the app was talking to would occasionally send bursts of data that would overflow the low level stream ...
11
votes
2answers
180 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 ...
11
votes
2answers
155 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 ...
11
votes
2answers
2k views

Disjoint sets implementation

I would like to get some feedback on the following implementation of disjoint sets, based on disjoint-sets forests (Cormen, et.al., Introduction to Algorithms, 2nd ed., p.505ff). It should have the ...
10
votes
4answers
4k views

Scala inspired classes in Python

I have to define a lot of values for a Python library, each of which will represent a statistical distribution (like the Normal distribution or Uniform distribution). They will contain describing ...
10
votes
5answers
14k views

Pair matching elements from two lists

Here is a method that takes two lists (l1 and l2) and pairs up elements that "match". As you can see at the end of the code ...
10
votes
2answers
895 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 ...
10
votes
1answer
312 views

OSGi-like infrastructure

I am a moderately new Scala developer working mostly by myself, so I don't have anyone to tell me what I'm doing wrong (except that the system mostly does work, so it can't be too awful). I would ...
10
votes
1answer
657 views

BFS and DFS tree traversal

I posted some code as part of an answer to another question and I thought it would be a good idea to get that code reviewed also. Any comments are welcomed, but I am mostly annoyed by ...
10
votes
2answers
726 views

Sending SMS through twilio from play framework

I am trying to send SMS using Twilio with Scala 2.10 in Playframework 2.2, but this code is looking odd to me, maybe due to lots of conditions. I'm sure it needs some important reviews. ...
9
votes
1answer
7k 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? <...
9
votes
1answer
11k 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. ...
8
votes
1answer
4k views

Merge sort in Scala

I've implemented merge sort in Scala: ...
8
votes
1answer
277 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 ...
8
votes
1answer
1k views

Basic (Logical Only) Sudoku Solver

I'm (very) new to Scala, and decided I'd try to get a handle on some of the syntax and general language constructs. This currently doesn't implement any backtracking and will only solve things if ...
8
votes
2answers
9k views

Binary tree implementation in Scala

This is my implementation of a binary tree in Scala. How can this be made better? ...
8
votes
2answers
749 views

Idiomatic Scala try option code block

Basically, this function returns a list of flights for an airline. But if the airline doesn't exist, I want to throw some custom runtime exception. If that airline does exist, I want to return a list ...
8
votes
1answer
250 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 ...
8
votes
1answer
264 views

Basic XML template

I am currently exploring the play framework. I'm about to replace the proposed templating system, using the powerful XML processing of the Scala library. Here is what I have come with: ...
8
votes
1answer
374 views

Event dispatcher

In the last 2 years or so, I've learned a great deal about how to write better Scala code, but I know that I've barely scratched the surface. This is part of a library that I've been using. It has ...
8
votes
1answer
167 views

Dungeon generation in Scala

I've recently begun learning Scala, and while I've run into its concepts before (immutability, tuples, first-class functions) I'm not sure whether I'm using the language how it's supposed to be used. ...
8
votes
1answer
165 views

Scala implementation of @rolfl's integer-partitioning algorithm

While reviewing @Martijn's Distinct sums of integers (finding all combinations of positive integers that sum to a given number), I proposed the following solution: ...
7
votes
4answers
348 views

Multiple SQL queries, one per ID

ids contains 100 ids that are comma delimited. Is it fine or does it need improvement? ...
7
votes
3answers
4k 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 ...
7
votes
5answers
7k views

More functional way of writing this palindrome extractor?

I wrote this palindrome extractor. And even though it works, and I can solve the challenge with it, it feels very Java-like. I was wondering what adjustments I could make in order for it to be more ...
7
votes
3answers
2k 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 ...
7
votes
1answer
6k views

Case class design in Scala

In the Java world while writing the data access layer (for CRUD) and the model layer, I have done something like this: ...
7
votes
2answers
535 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 ...
7
votes
2answers
7k views

Whether or not to name Boolean parameters in Scala [closed]

def execute(parameterName : scala.Boolean = { /* compiled code */ }) When calling this method, you can go with ...
7
votes
1answer
6k views

Handling parsing failure in Scala without exceptions

I have a Scala (Play!) application that must get and parse some data in JSON from an external service. I want to be able to gently handle failure in the response format, but it is becoming messy. What ...
7
votes
3answers
160 views

Distinct sums of integers

I need to get the distinct sums of naturals of a natural What I came up with is: ...
7
votes
1answer
243 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 ...
7
votes
4answers
6k views

Reducing boilerplate when validating parameters and using a tuple for the parameter list of a case class

I had designed a simple case class that looks like this: case class StreetSecondary(designator: String, value: Option[String]) I needed to add validation (to ...
7
votes
1answer
188 views

Validation macro

I would like a review of a Scala validation library I am writing. For now we can focus on the regular expression component. The usage is described in RegexExample.scala There is an outline of the ...