How to generate a list of n unique elements picked from a collection?


beetle

How can I use ScalaCheck to generate a list of n unique values ​​( Gen[List[T]]) from a set of values ​​(not a generator) ? This post is not using Gen[T]*a set of values ​​and I can't seem to override it to make it work.

edit

At @Jubobs' request, I now shamelessly show what I've tried so far, showing my newbie status when using ScalaCheck :-)

I just tried replacing the duplicate gs: Gen[T]parameters with the solution Set@Eric wrote here :

def permute[T](n: Int, gs: Set[T]): Gen[Seq[T]] = {
    val perm = Random.shuffle(gs.toList)
    for {
        is <- Gen.pick(n, 1 until gs.size)
        xs <- Gen.sequence[List[T], T](is.toList.map(perm(_)))
    } yield xs
}

But is.toList.map(perm(_))with a red underline, IntelliJ IDEA tells me "You should read the ScalaCheck API first before blind (albeit intuitive) trial and error" , or maybe "Type mismatch, should be expected: Traversable[Gen[T]], Actual List[T]" , I don't remember.

I've also tried several other approaches, most of which are absurd in hindsight (and thus not worth posting), the most naive of which is to use @Eric's (otherwise useful and neat) solution as-is:

val g = for (i1 <- Gen.choose(0, myList1.length - 1); 
  i2 <- Gen.choose(0, myList2.length - 1)) 
  yield new MyObject(myList1(i1), myList2(i2))
val pleaseNoDuplicatesPlease = permute(4, g, g, g, g, g)

After some testing I found that pleaseNoDuplicatesPleaseactually contains duplicates, at which point I weighed having to read the ScalaCheck API and have more knowledge than I do now (inevitably and gradually), or post my question on StackOverflow (after searching carefully for similar issues).

jub0bs

Gen.pickRight up your alley:

scala> import org.scalacheck.Gen
import org.scalacheck.Gen

scala> val set = Set(1,2,3,4,5,6)
set: scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 3, 4)

scala> val myGen = Gen.pick(5, set).map { _.toList }
myGen: org.scalacheck.Gen[scala.collection.immutable.List[Int]] = org.scalacheck.Gen$$anon$3@78693eee

scala> myGen.sample
res0: Option[scala.collection.immutable.List[Int]] = Some(List(5, 6, 2, 3, 4))

scala> myGen.sample
res1: Option[scala.collection.immutable.List[Int] = Some(List(1, 6, 2, 3, 4))

Related


How to get only unique elements from a list?

Ordostamasa I have a list, say [1,2,3,4,3,2,4]. For example, I want to output once per element [1,2,3,4]. How to do this in Haskell? William Van Anselm You can use it as a uniqueness filter . However, since it doesn't use a data structure like a hash set, it w

How to get only unique elements from a list?

Ordostamasa I have a list, say [1,2,3,4,3,2,4]. For example, I want to output once per element [1,2,3,4]. How to do this in Haskell? William Van Anselm You can use it as a uniqueness filter . However, since it doesn't use a data structure like a hash set, it w

How to get only unique elements from a list?

Ordostamasa I have a list, say [1,2,3,4,3,2,4]. For example, I want to output once per element [1,2,3,4]. How to do this in Haskell? William Van Anselm You can use it as a uniqueness filter . However, since it doesn't use a data structure like a hash set, it w

How to get only unique elements from a list?

Ordostamasa I have a list, say [1,2,3,4,3,2,4]. For example, I want to output once per element [1,2,3,4]. How to do this in Haskell? William Van Anselm You can use it as a uniqueness filter . However, since it doesn't use a data structure like a hash set, it w

How to get only unique elements from a list?

Ordostamasa I have a list, say [1,2,3,4,3,2,4]. For example, I want to output once per element [1,2,3,4]. How to do this in Haskell? William Van Anselm You can use it as a uniqueness filter . However, since it doesn't use a data structure like a hash set, it w

How to get all unique elements from a collection in Swift

NoKey If I get this array: [0, 0, 1, 1, 1, 2, 3, 4, 5, 5] How can I get: [2, 3, 4] This answer will keep the value of the duplicates, but I want to remove that too. Luca Angeletti If you want to filter only one occurrence of numbers, you can use the oldNSCou

How to get all unique elements from a collection in Swift

NoKey If I get this array: [0, 0, 1, 1, 1, 2, 3, 4, 5, 5] How can I get: [2, 3, 4] This answer will keep the value of the duplicates, but I want to remove that too. Luca Angeletti If you want to filter only one occurrence of numbers, you can use the oldNSCou

How to get all unique elements from a collection in Swift

NoKey If I get this array: [0, 0, 1, 1, 1, 2, 3, 4, 5, 5] How can I get: [2, 3, 4] This answer will keep the value of the duplicates, but I want to remove that too. Luca Angeletti If you want to filter only one occurrence of numbers, you can use the oldNSCou

How to get all unique elements from a collection in Swift

NoKey If I get this array: [0, 0, 1, 1, 1, 2, 3, 4, 5, 5] How can I get: [2, 3, 4] This answer will keep the value of the duplicates, but I want to remove that too. Luca Angeletti If you want to filter only one occurrence of numbers, you can use the oldNSCou

How to count unique elements in a collection?

Yaman Azumanyan I have favorites id : 1, url : youtube.com A URL can also be multiple times I need to get all collections and count unique elements like this youtube 10 google 8 lycos 5 this is the code public List<URLEntity> findAll() { List<

How to count unique elements in a collection?

Yaman Azumanyan I have favorites id : 1, url : youtube.com A URL can also be multiple times I need to get all collections and count unique elements like this youtube 10 google 8 lycos 5 this is the code public List<URLEntity> findAll() { List<

How to return N consecutive elements from a collection?

double07: I pass a collection of objects (in my case some Contact class) and need to return a page from this collection. My code feels a lot longer than it needs to be. Am I missing some library that performs more elegantly than if I iterate over each element

How to return N consecutive elements from a collection?

double07: I pass a collection of objects (in my case some Contact class) and need to return a page from this collection. My code feels a lot longer than it needs to be. Am I missing some library that performs more elegantly than if I iterate over each element

Generate 2 unique but random elements in a list

John Smith I need to generate a list with unique elements parties = ['Party A', 'Party B'] I tried def party_generator(size=1, chars=string.ascii_uppercase): parties = [] for y in range(2): party = ''.join(random.choice(chars) for x in range(s

Generate 2 unique but random elements in a list

John Smith I need to generate a list with unique elements parties = ['Party A', 'Party B'] i have tried def party_generator(size=1, chars=string.ascii_uppercase): parties = [] for y in range(2): party = ''.join(random.choice(chars) for x in ra

Generate 2 unique but random elements in a list

John Smith I need to generate a list with unique elements parties = ['Party A', 'Party B'] i have tried def party_generator(size=1, chars=string.ascii_uppercase): parties = [] for y in range(2): party = ''.join(random.choice(chars) for x in ra

Generate 2 unique but random elements in a list

John Smith I need to generate a list with unique elements parties = ['Party A', 'Party B'] i have tried def party_generator(size=1, chars=string.ascii_uppercase): parties = [] for y in range(2): party = ''.join(random.choice(chars) for x in ra