Tagged Questions
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 ...
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 ...
5
votes
2answers
215 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 ...
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 ...
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
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 ...
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 ...
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 ...
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
255 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
2answers
248 views
Multiple if conditons in scala using funcional programming
The following code has a lot of conditionals. I am trying to write
it in a functional programming way.
val basePrice = {
var b = 0.0
if (runtime > 120)
b += 1.5
if ((day == Sat) || (day ...
1
vote
2answers
321 views
Scala mastermind solver, which is the most scala-ish
I am trying to express the classical TDD kata of mastermind in the most idiomatic scala I can. Here is a scalatest, test suite :
package eu.byjean.mastermind
import org.junit.runner.RunWith
import ...