scala

Scala: What is the right way to build HashMap variant without linked lists ?

How mouch Scala standard library can be reused to create variant of HashMap that does not handle collisions at all? In HashMap implementation in Scala I can see that traits HashEntry, DefaultEntry and LinkedEntry are related, but I'm not sure whether I have any control over them. ...

What is the standard way to optimise mutual recursion in F#/Scala?

These languages do not support mutually recursive functions optimization 'natively', so I guess it must be trampoline or.. heh.. rewriting as a loop) Do I miss something? UPDATE: It seems that I did lie about FSharp, but I just didn't see an example of mutual tail-calls while googling ...

How to get Ponter/Reference semantics in Scala.

In C++ I would just take a pointer (or reference) to arr[idx]. In Scala I find myself creating this class to emulate a pointer semantic. class SetTo (val arr : Array[Double], val idx : Int) { def apply (d : Double) { arr(idx) = d } } Isn't there a simpler way? Doesn't Array class have a method to return some kind of reference to a p...

Internal scala compilation. Working with interactive.Global

I am trying to retrieve the AST from scala souce file. I have simplified the code (only relevant code) to following. trait GetAST { val settings = new Settings val global = new Global(settings, new ConsoleReporter(settings)) def getSt = "hello" //global.typedTree(src, true) } object Tre extends GetAST { def main(args:...

How do you code up a pattern matching code block in scala?

How do you code a function that takes in a block of code as a parameter that contains case statements? For instance, in my block of code, I don't want to do a match or a default case explicitly. I am looking something like this myApi { case Whatever() => // code for case 1 case SomethingElse() => // code for case 2 } And insid...

Iterators for mutable collections in Scala?

I just found out that there are such iterators in Java. Does Scala have iterators with 'set' and 'remove' methods for iterating (and modifying) mutable collections like array? If there is no such iterator then is there a good reason for that? ...

Scala XML retrieving from optional paths

I'd like to process a document to retrieve a value that could have more than one path. The ideal signature would look something like: def value(doc: Elem, potential_paths: List[something]): String Where it would simply process the doc looking at the head of the potential_paths, if found, return it, otherwise continue with potential_p...

How can I get a Node adjacent to a unique Node using Scala?

I'm trying to parse an Apple plist file and I need to get an array Node within it. Unfortunately its only unique identifier is sibling Node right before it, <key>ProvisionedDevices</key>. Right now my best thoughts are to use Java's XPATH querying or Node.indexOf. Here is an example: <plist version="1.0"> <dict> <...

Passing enum parameter to a case class does not work

Can someone tell me why this does not work? case class XY(enum: MyEnum) object MyEnum extends Enumeration { val OP1, OP2 = Value } Error: not found: type MyEnum ...

Discovering a functional algorithm from a mutable one

This isn't necessarily a Scala question, it's a design question that has to do with avoiding mutable state, functional thinking and that sort. It just happens that I'm using Scala. Given this set of requirements: Input comes from an essentially infinite stream of random numbers between 1 and 10 Final output is either SUCCEED or FAIL T...

Can't find method in the activity

I'm starting with Scala + Android (and using the sbt android plugin). I'm trying to wire a button action to a button without the activity implementing View.OnClickListener. The button click fails at runtime because the method cannot be found. The document I'm working through says that I need only declare a public void method taking a Vi...

Scala simple dummy project.

Currently my whole work cycle is: edit foo.scala fsc foo.scala && scala -cp . FooMain But my project is getting bigger and I would like to split files, make unit tests, etc. But I'm too lazy for reading sbt documentation and doing whatever needs to be done to get a sbt's "Makefile". Similarly for unit tests (there are so many framewo...

How to get list of traits that were mixed in the specified class?

And more specific example: abstract trait A trait B extends A trait C extends A How to check what traits that extend trait A (it can be from 0 to many) were mixed in specified class? ...

Assign method in Scala.

When this code is executed: var a = 24 var b = Array (1, 2, 3) a = 42 b = Array (3, 4, 5) b (1) = 42 I see three (five?) assignments here. What is the name of the method call that is called in such circumstances? Is it operator overloading? Update: Can I create a class and overload assignment? ( x = y not x(1) = y ) ...

CPS/Continuations StackOverflowError on (tail-)recursive functions

Hello, is there any way to have a tail-recursive function inside CPS not throwing a StackOverflow? import scala.util.continuations._ object CPSStackOverflow { def main(args: Array[String]) = { reset { def recurse(i: Int): Unit @suspendable = { println(i) shift { k: (Unit => Unit) => k( () ) // just ...

Self-type mismatch in Scala

Given this: abstract class ViewPresenterPair { type V <: View type P <: Presenter trait View {self: V => val presenter: P } trait Presenter {self: P => var view: V } } I am trying to define an implementation in this way: case class SensorViewPresenter[T] extends ViewPresenterPair { type V = SensorView[T] ty...

How to call a generic method with an anonymous type involving generics?

I've got this code that works: def testTypeSpecialization: String = { class Foo[T] def add[T](obj: Foo[T]): Foo[T] = obj def addInt[X <% Foo[Int]](obj: X): X = { add(obj) obj } val foo = addInt(new Foo[Int] { def someMethod: String = "Hello world" }) foo.someMethod } But, I'd lik...

Converting mutable to immutable map

private[this]object MMMap extends HashMap[A, Set[B]] with MultiMap[A, B] How convert it to immutable? ...

Advanced control of recursive parser in scala

val uninterestingthings = ".".r val parser = "(?ui)(regexvalue)".r | (uninterestingthings~>parser) This recursive parser will try to parse "(?ui)(regexvalue)".r until the end of input. Is in scala a way to prohibit parsing when some defined number of characters were consumed by "uninterestingthings" ? UPD: I have one poor solution: ...

Scala: getting the name of the class the trait is mixed in

Given an instance of a class, we can obviously return its name: trait MixedInClassDiscovery { val className = this.getClass.getName } class AClass extends MixedInClassDiscovery { ... this.className // returns "AClass" ... } But this way uses reflection, once for every instance of AClass. Can the same be done once for every cl...