How can I define a ScalaCheck generator that produces a subset of a sequence's elements? - scala

How can I pick a subset of the elements of a sequence?
For instance, if I had the sequence Seq(1,2,3,4,5), I'd like each call to my generator to produce something like
Seq(1,4)
or
Seq(1,2,3,5)
or
Seq()
How can I define such a generator?

org.scalacheck.Gen.someOf is a generator that picks a random number of elements from an iterable:
scala> import org.scalacheck.Gen
import org.scalacheck.Gen
scala> val baseSeq = Seq(1, 2, 3, 4, 5)
baseSeq: Seq[Int] = List(1, 2, 3, 4, 5)
scala> val myGen = Gen.someOf(baseSeq).map(_.toSeq)
myGen: org.scalacheck.Gen[Seq[Int]] = org.scalacheck.Gen$$anon$6#ff6a218
scala> myGen.sample.head
res0: Seq[Int] = List(3, 4, 5)
scala> myGen.sample.head
res1: Seq[Int] = List(1, 2, 3, 4)
scala> myGen.sample.head
res2: Seq[Int] = List()

This depends on the probability for each element to show up in your result. For example, for any element to show up with 50% chance, you can use the following. Hope this helps.
import scala.util.Random
val s = Seq(1,2,3,4,5)
s filter { i => Random.nextInt(2) == 1 }

Related

Why does List.toString and Vector.toString return a nice representation but not Array.toString?

scala> Array(1, 2, 3).toString
res1: String = [I#11cf437c
scala> List(1, 2, 3).toString
res2: String = List(1, 2, 3)
scala> Vector(1, 2, 3).toString
res3: String = Vector(1, 2, 3)
Logically, one would expect Array(1, 2, 3).toString to return "Array(1, 2, 3)".
Update: seems to me like Array maps to the built in Java array type—is this correct? and if yes, is this the reason Array.toString has to behave like this?
It is because Array is a Java object. You can however use runtime.ScalaRunTime.stringOf if it suits your needs.
scala> runtime.ScalaRunTime.stringOf(Array(1, 2, 3))
res3: String = Array(1, 2, 3)
scala> List(1,2,3).getClass()
res0: Class[_ <: List[Int]] = class scala.collection.immutable.$colon$colon
scala> Vector(1,2,3).getClass()
res1: Class[_ <: scala.collection.immutable.Vector[Int]] = class scala.collection.immutable.Vector
scala> Array(1,2,3).getClass()
res2: Class[_ <: Array[Int]] = class [I
List and Vector are Scala classes, so they have a nice representation. Array comes from Java and practices Java's ugliness.

How can I sort List[Int] objects?

What I want to do is sort List objects in Scala, not sort the elements in the list. For example If I have two lists of Ints:
val l1 = List(1, 2, 3, 7)
val l2 = List(1, 2, 3, 4, 10)
I want to be able to put them in order where l1 > l2.
I have created a case class that does what I need it to but the problem is that when I use it none of my other methods work. Do I need to implement all the other methods in the class i.e. flatten, sortWith etc.?
My class code looks like this:
class ItemSet(itemSet: List[Int]) extends Ordered[ItemSet] {
val iSet: List[Int] = itemSet
def compare(that: ItemSet) = {
val thisSize = this.iSet.size
val thatSize = that.iSet.size
val hint = List(thisSize, thatSize).min
var result = 0
var loop = 0
val ths = this.iSet.toArray
val tht = that.iSet.toArray
while (loop < hint && result == 0) {
result = ths(loop).compare(tht(loop))
loop += 1
}
if (loop == hint && result == 0 && thisSize != thatSize) {
thisSize.compare(thatSize)
} else
result
}
}
Now if I create an Array of ItemSets I can sort it:
val is1 = new ItemSet(List(1, 2, 5, 8))
val is2 = new ItemSet(List(1, 2, 5, 6))
val is3 = new ItemSet(List(1, 2, 3, 7, 10))
Array(is1, is2, is3).sorted.foreach(i => println(i.iSet))
scala> List(1, 2, 3, 7, 10)
List(1, 2, 5, 6)
List(1, 2, 5, 8)
The two methods that are giving me problems are:
def itemFrequencies(transDB: Array[ItemSet]): Map[Int, Int] = transDB.flatten.groupBy(x => x).mapValues(_.size)
The error I get is:
Expression of type Map[Nothing, Int] doesn't conform to expected type Map[Int, Int]
And for this one:
def sortListAscFreq(transDB: Array[ItemSet], itemFreq: Map[Int, Int]): Array[List[Int]] = {
for (l <- transDB) yield
l.sortWith(itemFreq(_) < itemFreq(_))
}
I get:
Cannot resolve symbol sortWith.
Is there a way I can just extend List[Int] so that I can sort a collection of lists without loosing the functionality of other methods?
The standard library provides a lexicographic ordering for collections of ordered things. You can put it into scope and you're done:
scala> import scala.math.Ordering.Implicits._
import scala.math.Ordering.Implicits._
scala> val is1 = List(1, 2, 5, 8)
is1: List[Int] = List(1, 2, 5, 8)
scala> val is2 = List(1, 2, 5, 6)
is2: List[Int] = List(1, 2, 5, 6)
scala> val is3 = List(1, 2, 3, 7, 10)
is3: List[Int] = List(1, 2, 3, 7, 10)
scala> Array(is1, is2, is3).sorted foreach println
List(1, 2, 3, 7, 10)
List(1, 2, 5, 6)
List(1, 2, 5, 8)
The Ordering type class is often more convenient than Ordered in Scala—it allows you to specify how some existing type should be ordered without having to change its code or create a proxy class that extends Ordered[Whatever], which as you've seen can get messy very quickly.

scala: how to convert ArrayBuffer to a Set?

I've been looking for a bit of time on how to convert an ArrayBuffer to a Set, an HashSet I guess to be precise. Any hint?
There is a toSet function implemented in ArrayBuffer
Example:
scala> import collection.mutable.ArrayBuffer
import collection.mutable.ArrayBuffer
scala> import collection.immutable.HashSet
import collection.immutable.HashSet
scala> val a = new ArrayBuffer(2)
a: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()
scala> val b = a.toSet
b: scala.collection.immutable.Set[Nothing] = Set()
To Set:
scala> val bf = ArrayBuffer(1,2,3,4)
bf: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
scala> bf.toSet
res0: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
To HashSet:
scala> val hs = new HashSet[Int]++ bf.toSet
hs: scala.collection.immutable.HashSet[Int] = Set(1, 2, 3, 4)
For completeness' sake, one more way:
scala> import collection.mutable.ArrayBuffer
import collection.mutable.ArrayBuffer
scala> val as = ArrayBuffer(34, 89, 11)
as: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(34, 89, 11)
scala> Set(as: _*)
res13: scala.collection.immutable.Set[Int] = Set(34, 89, 11)

Convert Seq to ArrayBuffer

Is there any concise way to convert a Seq into ArrayBuffer in Scala?
scala> val seq = 1::2::3::Nil
seq: List[Int] = List(1, 2, 3)
scala> seq.toBuffer
res2: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3)
EDIT After Scala 2.1x, there is a method .to[Coll] defined in TraversableLike, which can be used as follow:
scala> import collection.mutable
import collection.mutable
scala> seq.to[mutable.ArrayBuffer]
res1: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3)
scala> seq.to[mutable.Set]
res2: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
This will work:
ArrayBuffer(mySeq : _*)
Some explanations: this uses the apply method in the ArrayBuffer companion object. The signature of that method is
def apply [A] (elems: A*): ArrayBuffer[A]
meaning that it takes a variable number of arguments. For instance:
ArrayBuffer(1, 2, 3, 4, 5, 6, 7, 8)
is also a valid call. The ascription : _* indicates to the compiler that a Seq should be used in place of that variable number of arguments (see Section 4.6.2 in the Scala Reference).

What is the difference between val b=a (a is an Array) and val b=a.clone()?

I am reading scaladocs and just wondering difference between direct assignment and .clone method.
val a=Array(1,2,3,4,5)
case 1:
val b=a
case 2 :
val b=a.clone()
Consider this:
scala> val a=Array(1,2,3,4,5)
a: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val b = a
b: Array[Int] = Array(1, 2, 3, 4, 5)
scala> val c = a.clone()
c: Array[Int] = Array(1, 2, 3, 4, 5)
scala> b(0) = 0
scala> c(1) = 1
scala> a
res2: Array[Int] = Array(0, 2, 3, 4, 5)
scala> b
res3: Array[Int] = Array(0, 2, 3, 4, 5)
scala> c
res4: Array[Int] = Array(1, 1, 3, 4, 5)
As you can see, when you do val b = a, then a and b point to the same object. When the object is changed, the change will be seen by both.
On the other hand, when you clone the array, you produce a new array with the same content. Changing this new array does not change the old one.
I believe case 1 just sets the reference of a to b while case 2 creates an entirely new array that is a copy of a and putting the value in b.
In other words if you in case a edit the a array the b array will also be edited this is not the case in case 2
Here is an answer in code:
scala> val a = Array(1,2,3,4,5)
scala> a.hashCode()
res12: Int = 1382155266
scala> val b = a
scala> b.hashCode()
res13: Int = 1382155266
scala> val c = a.clone()
scala> c.hashCode()
res14: Int = 2062756135
scala> a eq b
res15: Boolean = true
scala> a eq c
res16: Boolean = false
scala> b eq c
res17: Boolean = false
In case 1, both reference leads to the same object while in the second case, a new object is created and a and b do not reference the same object.