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

0
votes
1answer
22 views

Pipeline operator in scala

Please, review my implementation of the F# pipeline operator in Scala class PipelineContainer[F](value: F) { def |>[G] (f: F => G) = f(value) } implicit def pipelineEnrichment[T](xs: T) = ...
1
vote
1answer
32 views

Is this a correct recursive Scala iterator creation?

I am diving into scala (using 2.10) headfirst and am trying out a simple task of taking bulk data and then doing something with that stream. The first part is generating that stream. I have seem to be ...
3
votes
1answer
54 views

is there a better and more functional way to write this

I'm learning Scala and functional programming. Can I make it more functional? Thanks class Student(val firstname: String, val lastname: String) { override def toString: String = firstname + " " + ...
3
votes
1answer
47 views

How to make this code more functional?

I have a simple task: Given a sequence of real numbers. Replace all of its members larger than Z by Z. And count the number of replacements. I solved this task using Scala. But my code looks ...
6
votes
0answers
176 views

Attempt to port Free Applicative to scala [closed]

I'm trying to port Edward Kmett's free package (in particular, Free Applicative) to scala. I was able to get this far. I'm particularly having problem with implementing the ap function for the Ap ...
1
vote
1answer
39 views

Need help figuring out the performance bottle neck

I'm working on my scala chops and implemented a small graph Api to track vertices and edges added to graph. I have basic GraphLike Trait, and have an Undirected Graph class ( UnDiGraph) and a Directed ...
1
vote
2answers
43 views

Recursively print in scala

I have just picked up Scala like 2 hours ago, and I am thinking of printing a series like 1 to 10 but in a recursive fashion. I do not understand what is wrong with this: def printSeries(x:Int):Int = ...
3
votes
1answer
94 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
64 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
83 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
165 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
81 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
135 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
72 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
42 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 ...
2
votes
2answers
132 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
51 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
173 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 ...
4
votes
1answer
61 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
214 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
170 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
81 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
55 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
131 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
160 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
299 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
141 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
285 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 ...
6
votes
1answer
616 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 ...
9
votes
1answer
1k 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
426 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
257 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
144 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
135 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
107 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
178 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
361 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
259 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
580 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
65 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
477 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
127 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 ...
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
115 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
83 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
500 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
152 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
239 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
152 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
254 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 ::= ...

1 2