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

1
vote
2answers
28 views

Range Minimum Query Implementation in Scala

I would like to heat some feedback on this. I'm coming from a Java background and this is my first program in Scala. I solved Range Minimum Query problem. object Solution { def main(args: ...
10
votes
3answers
561 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? for (i <- 1 to 100) { if ( i % 3 ...
4
votes
3answers
79 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 block these lists consist of matching elements but in different indexes. The ...
1
vote
1answer
32 views

Implementing Option#map2

Working on an example from Functional Programming in Scala, I'm working on Option#map2: override def map2[A, B, C](fa: Option[A],fb: Option[B])(f: (A, B) => C): Option[C] = { (fa, fb) match { ...
3
votes
1answer
34 views

Writing Traverse Instance for Option

Working on an exercise from Functional Programming in Scala, I implemented a Traverse instance for Option: override def traverse[G[_],A,B](oa: Option[A])(f: A => G[B])(implicit G: Applicative[G]): ...
3
votes
2answers
31 views

Partial octree implementation

I am just now learning Scala a bit on my own time. I wrote some code that works, but was wondering if you could eyeball it to see if its structure can be improved. It is a partial octree ...
3
votes
1answer
53 views

Is this base 62 encoding algorithm okay?

This Scala code snippet is supposed to encode a SHA1 hash in base 62. Can you find any issues? I'm asking since I might not be able to change the algorithm and, for example, fix issues in the future. ...
1
vote
1answer
61 views

How to ensure file exists, and get the absolutePath? [closed]

I need to: create an object with the absolute path of a file ensure the file exists My approach is to create a new Path and call toFile to make sure it exists - or fail. Some(new VideoSource(new ...
1
vote
1answer
53 views

Functional Re-Write in Scala: Rows of Strings with a Max Width

I came across an issue the other day where I really just could not think of a functional solution. So I fell back on my imperative programming skills. Since a functional solution still eludes me, I ...
4
votes
1answer
43 views

Singly Linked List Implementation in Scala

My implementation for single linked list is as follow class Node(itemValue: Object, nextItem:Node){ val value = itemValue val next = nextItem } class MyList() { var start: Node =null def ...
5
votes
1answer
111 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 Java. So, yes, it uses variables, ...
3
votes
1answer
99 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: public abstract class AbstractDao<N extends AbstractModel>{ ...
3
votes
1answer
118 views

Why does the LR on spark run so slowly?

Because the MLlib does not support the sparse input, I ran the following code, which supports the sparse input format, on spark clusters. The settings are: 5 nodes, each node with 8 cores (all the ...
2
votes
2answers
157 views

Given an array, find any three numbers which sum to zero

Given an array, find any three numbers which sum to zero. This problem is a continuation of a code kata in Python . I'm learning Scala and would like feedback on more efficient, cleaner, and ...
2
votes
1answer
57 views

Interface for Spotify API

I'm trying to make a scala interface to the spotify API. I'm using it to learn Scala. I come from mostly a python background. import dispatch._, Defaults._ trait Request { val name: String val ...
6
votes
1answer
62 views

Idiomatic Scala try option code block

Trying to learn scala. Wanted to know if this is the standard way of doing things in Scala - or is there a better way to write this code ? Basically, this function returns a list of flights for an ...
1
vote
1answer
36 views

Scala for processing files in a directory tree

I'm new to Scala, and would appreciate any notes about a better approach, correctness, or style. /** * Call proc(f) for each file in the directory tree rooted at dir. * * proc will not be called ...
4
votes
1answer
41 views

What is a better approach for operating on an arbitrary map value in Scala?

Coming from an imperative background, I have written the following code in Scala. I need to attempt to find a value in a map. If the value exists, I need to pass the value to another function and ...
2
votes
1answer
77 views

Is this a reasonable Scala monad?

I found myself writing code of the form... val foo : Foo = ??? val step1 : Foo = { if ( someCharacteristic( foo ) ) someTransformation( foo ) else foo } val step2 : Foo = { if ( ...
2
votes
1answer
73 views

Is there any way to simplify this code

I'd like to simplify this Scala code: private def getToken() = { val token = getTokenFromDb() token match { case Some(x) => token case None => { val remoteToken = ...
1
vote
1answer
104 views

Better way of calculating Project Euler #2 (Fibonacci sequence)

Even Fibonacci numbers Problem 2 Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, ...
3
votes
1answer
61 views

is this future map scala syntax good?

I'm new to scala and have been working to understand the syntax so that I can be more efficient. How does this look in terms of functional syntax and scala idioms? In particular, I'd like to know if ...
1
vote
0answers
82 views

Scalaz state monad exercise

I can't tag this with scalaz and state-monad yet since I don't have sufficient reputation to add tags. Below is a self imposed exercise and solution to learn Scalaz state monad. How can I ...
1
vote
2answers
104 views

More functional approach

I wonder if there is a more "functional" way of implementing something like this: @months.zipWithIndex.foreach { case (month, index) => if (index%3 == 0) { <tr> } ...
4
votes
2answers
145 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
76 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
186 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
102 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
250 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
287 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
138 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
361 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
93 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
2answers
123 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
682 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
533 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, ...
3
votes
2answers
149 views

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
61 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
82 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
61 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
78 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
158 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 ...
5
votes
1answer
338 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
117 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
122 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
103 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
80 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
45 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
53 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 = ...
6
votes
1answer
388 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 ...