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

4
votes
2answers
61 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 someobject.execute(true) or someobject.execute(parameterName = true) If you use ...
1
vote
1answer
55 views

Counting inversions

I'm trying to implement an inversion counting routine in Scala in a functional way (I don't want to port Java solution) but have real troubles with it. The sorting routine works fine but adding the ...
2
votes
1answer
101 views

How to write better an sqrt (square root) function in Scala

As an exercise in learning Scala, I implemented a square root function like this: def sqrt(x: Double): Double = { if (x < 0) throw new IllegalArgumentException("negative numbers not ...
5
votes
1answer
75 views

Maximum path problem using Scala

I am very new to Scala. I have done an exercise using Scala to solve the maximum path problem. Basically, I have a triangle of integers, I want to find the path from the top to bottom which the ...
3
votes
2answers
216 views

How to rewrite this code in functional way?

My code looks like imperative style code. I want to make my code more functional. How I can rewrite my program so it become functional style code? val _map = getMap(); if(_map.contains(x) ...
3
votes
1answer
134 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. type Vertex=Int ...
3
votes
2answers
98 views

Improve scala functional string transformation

How could this scala code be made more concise and/or idiomatic? Do I need to define each functions as a val in order to pass them as values, or is there a shorthand for this? Also looking for ...
0
votes
0answers
55 views

rxjava observables and scala futures

In a simple webapp I have been using as a playground, I am playing with using rxjava to perform a number of operations in a reactive manner. I have been using reactivemongo to connect to my Mongo ...
0
votes
0answers
58 views

Scala: Pointillism

I'm relatively new to Scala and tried to copy one of the language examples of Processing: Pointillism. The original image can be retrieved here. I've used the Standard Widget Toolkit for drawing. ...
2
votes
3answers
108 views

Refactor to reduce code duplication

I have four methods like these (here are only two of them): def checkLeft(clickedIndex: Int): Option[Int] = { val leftIndex = clickedIndex - 1 if (leftIndex >= 0 && clickedIndex ...
3
votes
1answer
96 views

Scala Binary Search Tree Implementation?

I have written a sample immutable binary search tree using Scala. However, I am not sure whether or not I was doing it the proper way since I am not an expert at functional programming. Is there a ...
3
votes
1answer
132 views

Recursive branch and bound for knapsack problem

I've got the imperative styled solution working perfectly. What I'm wondering is how to make a recursive branch and bound. This is my code below, Evaluate function returns the optimistic estimate, ...
1
vote
2answers
112 views

Scala: What takes precedence: Readability or Elimination of Side Effects

I have a method: def removeExpiredCookies(jar: CookieJar): CookieJar = { val currentDate = System.currentTimeMillis / 1000L jar.filter(_._2._2 > currentDate) } You use said method ...
1
vote
1answer
55 views

Is this class name for my commenting system confusing?

In my project (which is a discussion system) there are approved and unapproved comments. There's a class that tells if unapproved comments should be shown. But I cannot think of a good name for this ...
2
votes
1answer
60 views

Printing out a tree set using pattern matching in Scala

I'm working on some exercises Scala by Example and I've implemented an excl() method on sets which removes an element from a set, and a toString method that converts a set to a string. I'm practicing ...
1
vote
1answer
45 views

Simple Comonads in Scala?

trait Comonad[M[_]] { // map def >>[A,B](a: M[A])(f: A => B): M[B] // extract | coeta def counit[A](a:M[A]): A // coflatten | comu def cojoin[A](a: M[A]): M[M[A]] } ...
0
votes
1answer
64 views

Scala: Are assertions acceptable outside of test context?

In the case that Type is not an option, data must exist so I just use an assert to clarify and throw an exception if data is missing. Is there a better way to do this? I think in Java this would be ...
1
vote
1answer
104 views

Subclass of AtomicReference with atomic function application

Update: Now that https://github.com/bionicspirit/scala-atomic is available, I have less of a need for this. Am I doing anything bad? I wrote this class to allow modifying an atomic reference with the ...
4
votes
0answers
166 views

First Scala Program - CSV Parsing - is it idiomatic?

This is my first real attempt at a scala program. I come from a predominantly java background, so I'd like to know if the program sticks to scala conventions well. Is it well readable or should it ...
0
votes
1answer
62 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
2answers
82 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
3answers
94 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
65 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 ...
1
vote
1answer
43 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
52 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
171 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
86 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
164 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
184 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 ...
4
votes
1answer
126 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 ...
6
votes
3answers
366 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
94 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 ...
1
vote
0answers
67 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
174 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
91 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 ...
5
votes
3answers
231 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
72 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
261 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
180 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
96 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
2answers
92 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
145 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
333 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 ...
9
votes
2answers
461 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
153 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 ...
10
votes
0answers
332 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 ...
8
votes
1answer
945 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 ...
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
731 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
345 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 ...