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.
1
vote
1answer
45 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 ...
2
votes
0answers
31 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 ...
5
votes
1answer
50 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 ...
7
votes
0answers
133 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 ...
4
votes
1answer
113 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 ...
7
votes
1answer
147 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
148 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
124 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
85 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
92 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
84 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 ...
4
votes
2answers
154 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
303 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
184 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
288 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 ...