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

2
votes
1answer
51 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 ...
3
votes
1answer
44 views

How idiomatic is this Scala 36 Cube Solver

I've written a short program to find solutions to the 36 Cube puzzle. http://en.wikipedia.org/wiki/36_cube I'm trying to break away from my normal Java / imperative style, and would like some ...
1
vote
0answers
39 views

How to configure JMS Component in akka-camel

I'm trying to write JMS Consumer using Akka-Camel. For now, I'm using FFMQ as JMS server. I want to listen on JMS queue myqueue. Creating JMS consumer actor is quite straightforward: import ...
2
votes
3answers
142 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 ...
3
votes
0answers
58 views

Scala Play Controller - Refactoring out Duplication for Restful Web Services

I'm new to scala and I'm noticing I have some repeating code in my controllers. I'm wondering if anyone can give some hints for traits and parameterization for eliminating duplicate code for basic ...
4
votes
2answers
74 views

Merge sort in scala

I've implemented merge sort in scala: object Lunch { def doMergeSortExample() = { val values:Array[Int] = List(5,11,8,4,2).toArray sort(values) printArray(values) } def ...
2
votes
0answers
61 views

Is this Scala/Akka idiomatic and correct for Akka 2.1?

I'm wondering if there is anything i'm overlooking in the code below. I've taken the Pi calculation example, simplified it and converted it to Akka 2.1. More specifically, i use the ask pattern to ...
0
votes
0answers
33 views

Type-safe user preferences in scala

I was thinking about a solution to store preferences and access to them in a most natural way. I came up with the solution below. My questions are: what do you think about the solution? Can it be ...
1
vote
2answers
112 views

Option type in C# and implicit conversions

Good morning everyone, I have decided to create a simple Option type for C#. I have essentially based it on the Scala's Option. It was mostly a fun exercise, but it might come in handy at work or ...
0
votes
0answers
39 views

Testing scala code which depends on objects

I am writing a small oauth2 library for Play! 2.1 scala. As I am still learning I got stuck trying to TDD and ended up writing the code first, then refactoring for testability. By testability I mean ...
4
votes
3answers
165 views

A scala implementation of java trim method

I'm a scala beginner and looking at the trim() method of the java api I noticed side effects, so I attempted to implement a functional version in scala. Here is the java version : public String ...
3
votes
1answer
40 views

Terser syntax for first-class functions needing a context bound

I have the following function that looks up a mathematical operation that can be applied to a Numeric sequence based on a String def getAction[T : Fractional]( op : String ) : Seq[T] => String = ...
5
votes
2answers
187 views

Typesafe Tic-Tac-Toe API

So I've been working on Tony Morris' Tic-Tac-Toe challenge. The rules are as follows: Write an API for playing tic-tac-toe. There should be no side-effects (or variables), at all, for real. The ...
1
vote
4answers
165 views

How to break this curse by a pure functional recursive function?

I have written two programs to find out a loopy path in a directed graphs. The first version is a pure functional recursive solution, but its complexity is exponential. The second version following ...
4
votes
1answer
70 views

Scala: how to find and return a loopy path in a directed graph

I wrote a function in Scala to find out and return a loopy path in a directed graph. The program is as followed, one of the arguments is a graph presented in an adjacent list, and the other is a start ...
3
votes
0answers
53 views

GroupBy over a clustered iterator in Scala

Here's a solution for a lazy groupBy when the iterator contents are assumed to be clustered (repeated elements next to each other) over the key: def clusteredGroupBy[B](h: Iterator[B])(f: B => _): ...
2
votes
3answers
128 views

Cleaner way for coding a repetitive application of a function

I have this function definition which takes an r and applies the function f n times: r => (1 to n).foldLeft(r)((rx, _) => f(rx)) So for n=3 this is equivalent to f(f(f(r))) I don't like this ...
2
votes
1answer
137 views

Scala: tail-recursive factorial

Is there anything what could be improved on this code? def factorial(n: Int, offset: Int = 1): Int = { if(n == 0) offset else factorial(n - 1, (offset * n)) } The idea is to have tail-recursive ...
7
votes
0answers
226 views

Scala: Disjoint-Sets

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 ...
6
votes
2answers
133 views

Solving Project Euler problem #48 in Scala

I'm trying to learn some Scala and decided to try to tackle some Project Euler problems. For problem #48, coming from a Python background, my solution is the following one-liner: print ( (1 to ...
9
votes
0answers
260 views

Scala 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 ...
5
votes
1answer
458 views

Scala Best Practices

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 ...
8
votes
1answer
639 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 ...
5
votes
1answer
366 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 ...
5
votes
3answers
218 views

Scala: a safer way to cut string

I want to get just the first line of a big string. Currently, here's how I do it: def getFirstParagraph(txt: String) = { val newLineIdx = txt.indexOf("\n") match { case i: Int if i > 0 ...
5
votes
1answer
122 views

Matching BigInts in Scala

I'm currently in the process of learning scala, and I'm looking for some best practices/proper idioms for the use of pattern matching with BigInts. I'm writing some algorithmic code (for Project ...
2
votes
1answer
132 views

First steps in Scala: does this look idiomatic?

I an a newcomer in the Scala world, and I would like some advice from more experienced people to find out whether what I am writing goes in the direction of idiomatic code. In particular, I have to ...
4
votes
1answer
102 views

Code improvements

I'm doing exercise 11.8 from Scala for impatient, asking to write class for matrix: Provide a class Matrix—you can choose whether you want to implement 2 × 2 matrices, square matrices of ...
5
votes
2answers
171 views

Change code to use functional style

I tried to solve a programming contest problem in Scala, but I have a feeling, that it could be done in a more functional way. I compared it with the solution written in imperative style and it is not ...
6
votes
4answers
348 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 ...
5
votes
1answer
244 views

Connect Four In Idiomatic Scala

Learning Scala I'm trying to learn Scala and I'm interested in criticism about how to make my code more idiomatic. I've programmed a simple Connect-four game. Can I get some feedback? ...
2
votes
4answers
491 views

Is my Scala Option conversion to Java correct?

Summary: I've implemented what I think is a complete Java implementation of the Scala Option class. Could you please review and let me know if I have correctly and completely implemented it? And if ...
1
vote
0answers
60 views

Defining transpose on a collection of irregular collections

I was asked to submit my request for code review on http://stackoverflow.com/questions/10672046/defining-transpose-on-a-collection-of-irregular-collections here. This is a follow-up to the post I ...
2
votes
2answers
457 views

Code review of Pouring water from code chef using Scala

I had posted this question on Stackoverflow and it seems like code review might be a better venue for my query. My question is as follows: I am trying to solve the pouring water problem from codechef ...
2
votes
2answers
118 views

Scala: Josephus problem with Actors

Another stab at the Josephus problem, this time using Actors. N people stand in a circle and every stepth person is eliminated until one is remaining. The way I solve it here is to give each actor a ...
0
votes
0answers
67 views

Josephus problem in Scala without mutable state

The Josephus problem is a game involving a circle of people who take turns to kill themselves (being removed from the circle in the process), with the next person to go being a fixed number of places ...
1
vote
1answer
36 views

Which name should I choose for private abstract functionality with a public proxy?

I have an abstract class implementing some concrete functionality for its child classes: abstract class Function { def eval: Double = some_concrete_functionality_which_calls_fEval def simplify: ...
4
votes
2answers
114 views

Attempting to eliminate var (and imperative style) from my Piece class

I've been cooking with gas since I got Daniel C Sobral's help on my last question. I am now re-reading Odersky's "Programming in Scala, 2nd Edition" (finished my first reading about this time last ...
2
votes
1answer
82 views

Ideal FP/Scala way to vaildate rectangular list

I am learning Scala and FP (Functional Programming), coming from Java, OO and a strong imperative pardigm. I am now trying to implement a small puzzle solving project so I can get some deeper hands on ...
3
votes
1answer
464 views

Pouring Water = My first scala code

This is my first time in CodeReview, I'd hope like something like that existed and voila, a fellow StackExchange site has already been done. I just began to study Scala (coming from Python I had ...
3
votes
1answer
144 views

Help new Scala developer

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 ...
4
votes
4answers
231 views

Finding the first stream of non-repeating elements in Scala (without recursion or side-effects)

Here are some examples: [1, 2, 3, 4, 5] => [1, 2, 3, 4, 5] [10, 15, 10, 15, 30] => [10, 15] [1, 2, 3, 4, 1, 5, 6, 7] => [1, 2, 3, 4] Here's my best (and deeply ugly) non-recursive, ...
2
votes
1answer
139 views

Scala object-function balance

I am learning Scala, coming from Java background. Here I have written a small prototype proof of concept program that is a part of concept base for a simple upcoming tutorial game. There are two game ...
3
votes
1answer
236 views

Functionally retrieving rows from a database in Scala

Consider the following piece of code: var result = List[Measurement]() val dbResult = conn.createStatement.executeQuery(unsentMeasurements) while(dbResult.next) { result ::= ...
2
votes
1answer
112 views

Scala Direction enum with Enumeration and collection usage

I've just implement direction enum object Direction extends Enumeration { type Direction = Value val Up, Right, Down, Left = Value val pairs = HashSet() ++ List((HashSet() ++ List(Up, Down)), ...
2
votes
1answer
284 views

Simple factory pattern in scala (attempting to restrict use of new)

I am a Scala newbie and attempting to improve my skills. I have decided to be playful about it and work to do a conversion of the spaceinvaders game supplied with the LWJGL. It consists of 10 Java ...
3
votes
2answers
221 views

How to improve this scala code which is checking if two byte arrays are same?

I have a method to compare two byte arrays. The code is java-style, and there are many "if-else"s. def assertArray(b1: Array[Byte], b2: Array[Byte]) { if (b1 == null && b2 == null) return; ...
1
vote
0answers
100 views

How would you improve this scala basic xml template?

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 : import scala.xml._ ...
2
votes
4answers
202 views

Correct use of Option in scala?

I'm somewhat new to scala and am not sure if I'm doing things in a very scalaesque way. In particular, I'm not sure if I'm using Option correctly, since by calling .isDefined I'm basically just doing ...
3
votes
5answers
914 views

More functional way of writing this palindrome extractor?

I wrote this palindrome extractor for the greplin challenges: import collection.mutable._ object Level1 { def palindrome(input:String) = input.reverse == input def ...

1 2