scala-2.8

How do I implement a collection in Scala 2.8?

末鹿安然 提交于 2019-12-03 06:26:01
In trying to write an API I'm struggling with Scala's collections in 2.8(.0-beta1). Basically what I need is to write something that: adds functionality to immutable sets of a certain type where all methods like filter and map return a collection of the same type without having to override everything (which is why I went for 2.8 in the first place) where all collections you gain through those methods are constructed with the same parameters the original collection had (similar to how SortedSet hands through an ordering via implicits) which is still a trait in itself, independent of any set

Scala immutable objects and traits with val fields

孤街醉人 提交于 2019-12-03 06:07:36
I would like to construct my domain model using immutable objects only. But I also want to use traits with val fields and move some functionality to traits. Please look at the following example: trait Versionable { val version = 0 def incrementVersion = copy(version=version+1) } Unfortunatelly such code doesn't work - copy method is unknown for trait Versionable. I think that it would be nice to have copy method generated for every trait and class. Such method should create shallow copy of object and return it using the same type as for original object with given field modified accoring to

What is the most straightforward way to parse JSON in Scala?

故事扮演 提交于 2019-12-03 05:03:05
问题 I'm working on a simple web application with Scala. The plan is to obtain JSON data from an external API, and insert it into a template (unfortunately, obtaining the data in XML is not an option). I've tried working with Twitter's scala-json library, but I can't get it to compile properly (the code on github fails to update in sbt, saying standard-project 7.10 is not available and I haven't worked that out yet). lift-json looks impressive, but appears to be a lot more elaborate than I need

Using scala.util.control.Exception

試著忘記壹切 提交于 2019-12-03 04:46:16
问题 Does anybody have good examples of using scala.util.control.Exception version 2.12.0 (version 2.8.0), ? I am struggling to figure it out from the types. 回答1: Indeed - I also find it pretty confusing! Here's a problem where I have some property which may be a parseable date: def parse(s: String) : Date = new SimpleDateFormat("yyyy-MM-dd").parse(s) def parseDate = parse(System.getProperty("foo.bar")) type PE = ParseException import scala.util.control.Exception._ val d1 = try { parseDate } catch

How to convert a Seq[A] to a Map[Int, A] using a value of A as the key in the map?

心不动则不痛 提交于 2019-12-03 03:34:10
问题 I have a Seq containing objects of a class that looks like this: class A (val key: Int, ...) Now I want to convert this Seq to a Map , using the key value of each object as the key, and the object itself as the value. So: val seq: Seq[A] = ... val map: Map[Int, A] = ... // How to convert seq to map? How can I does this efficiently and in an elegant way in Scala 2.8? 回答1: Map over your Seq and produce a sequence of tuples. Then use those tuples to create a Map . Works in all versions of Scala.

How to find same-value rectangular areas of a given size in a matrix most efficiently?

狂风中的少年 提交于 2019-12-03 03:01:28
My problem is very simple but I haven't found an efficient implementation yet. Suppose there is a matrix A like this: 0 0 0 0 0 0 0 4 4 2 2 2 0 0 4 4 2 2 2 0 0 0 0 2 2 2 1 1 0 0 0 0 0 1 1 Now I want to find all starting positions of rectangular areas in this matrix which have a given size. An area is a subset of A where all numbers are the same. Let's say width=2 and height=3. There are 3 areas which have this size: 2 2 2 2 0 0 2 2 2 2 0 0 2 2 2 2 0 0 The result of the function call would be a list of starting positions (x,y starting with 0) of those areas. List((2,1),(3,1),(5,0)) The

Mixing Scala and Java files in an Eclipse project

随声附和 提交于 2019-12-03 02:49:48
I'm probably doing something stupid, but I can't spot it. I've installed Eclipse Helios (Helios because I couldn't get Glassfish support to work correctly using Gallileo) and the nightly build of the Scala Eclipse plugin for Helios I've created a Scala project and added some files - a mix of Java and Scala. They all seem syntactically correct - the Eclipse editor at least seems to know what language each file is, and reports correctly on syntax errors when I make them - but the Java files cannot find classes created in Scala. The IDE site seems to suggest this should all just work. There are

How to use Scala in IntelliJ IDEA (or: why is it so difficult to get a working IDE for Scala)?

╄→гoц情女王★ 提交于 2019-12-03 01:35:58
问题 I recently gave up trying to use Scala in Eclipse (basic stuff like completion doesn't work). So now I'm trying IntelliJ. I'm not getting very far. I've been able to edit programs (within syntax highlighting and completion... yay!). But I'm unable to run even the simplest "Hello World". This was the original error: Scala signature Predef has wrong version Expected 5.0 found: 4.1 in .... scala-library.jar But that was yesterday with IDEA 9.0.1. See below... UPDATE Today I uninstalled IntelliJ

How to find a matching element in a list and map it in as an Scala API method?

百般思念 提交于 2019-12-03 01:30:48
Is there a method to do the following without doing both methods: find and map ? val l = 0 to 3 l.find(_ * 33 % 2 == 0).map(_ * 33) // returns Some(66) Wilfred Springer How about using collect? // Returns List(66) List(1, 2, 3) collect { case i if (i * 33 % 2 == 0) => i * 33 } However that will return all matches and not just the first one. The better answer would have been, based on Scala 2.9: // Returns Some(66) List(1, 2, 3) collectFirst { case i if (i * 33 % 2 == 0) => i * 33 } The solution suggested in the comments to append a head to get a Scala 2.8 version of that is not very efficient,

How to write a zipWith method that returns the same type of collection as those passed to it?

隐身守侯 提交于 2019-12-03 01:03:01
I have reached this far: implicit def collectionExtras[A](xs: Iterable[A]) = new { def zipWith[B, C, That](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: CanBuildFrom[Iterable[A], C, That]) = { val builder = cbf(xs.repr) val (i, j) = (xs.iterator, ys.iterator) while(i.hasNext && j.hasNext) { builder += f(i.next, j.next) } builder.result } } // collectionExtras: [A](xs: Iterable[A])java.lang.Object{def zipWith[B,C,That](ys: Iterable[B])(f: (A, B) => C)(implicit cbf: scala.collection.generic.CanBuildFrom[Iterable[A],C,That]): That} Vector(2, 2, 2).zipWith(Vector(4, 4, 4))(_ * _) // res3: