Content-Length: 351535 | pFad | http://github.com/jozic/scalax-collection/tree/main/docs

3E scalax-collection/docs at main · jozic/scalax-collection · GitHub
Skip to content

Latest commit

 

History

History

docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

scalax-collection Build Status Coverage Status

A small library of extension methods for standard scala collections library. Published to maven central.

2.10 2.11 2.12 2.13 3
Maven Central Maven Central Maven Central Maven Central Maven Central

Collection Examples

An import needed for examples to work:

import com.daodecode.scalax.collection.extensions._

distinctBy

preserving first duplicate

val xs = List(1 -> "one", 1 -> "ten", 2 -> "two", 2 -> "twenty").distinctBy(_._1)

NOTE: Since Scala 2.13 this method is available in standard library

distinctByUsing

or preserving any duplicate you want

val xs = List(1 -> "one", 1 -> "ten", 2 -> "two", 2 -> "twenty").distinctByUsing(_._1, takeFirst = _._2.length > _._2.length)

NOTE: Before 0.3.0 this method was named distinctBy

foldLeftWhile/foldRightWhile

val xs = Iterable(List(1,2,3), List(4,5), List(6,7,8,9)).foldLeftWhile(List.empty[Int])(_.size < 4){ case (acc, l) => acc ++ l }

toCompleteMap

since Scala 2.13 can be seen as equivalent to groupMap(_._1)(_._2)

val cm = List(1 -> "1", 2 -> "2", 1 -> "11").toCompleteMap

mapToMap

can be seen as more efficient replacement for map().toMap combination

val m = List("1" -> "one", "2" -> "two").mapToMap { case (i, s) => i.toInt -> s }

toMapWithKey

since Scala 2.13 can be seen as equivalent to groupMapReduce(f)(identity)((b,_) => b)

val m = List("1", "2", "1").toMapWithKey(_.toInt)

toMapWithValue

since Scala 2.13 can be seen as equivalent to groupMapReduce(identity)(f)((b,_) => b)

val m = List("1", "2", "1").toMapWithValue(_.toInt)

withFrequency

since Scala 2.13 can be seen as equivalent to groupMapReduce(identity)(_ => 1)(_ + _)

val fm = List("a", "b", "c", "a", "b", "d").withFrequency

withFrequencyBy

since Scala 2.13 can be seen as equivalent to groupMapReduce(f)(_ => 1)(_ + _)

val fm = List("ab", "bc", "cd", "ab", "bc", "de").withFrequencyBy(_.head)

mergedWith

Merges two maps using provided function to merge values for duplicate keys

val merged = Map("1" -> 1, "2" -> 2).mergedWith(Map("1" -> 1, "2" -> 2))(_ + _)

minOption/minOptionBy

Finds the smallest element wrapped in Option or None if iterable is empty

val m1 = List.empty[Int].minOption

val m2 = List(1,2,1).minOptionBy(_ * -1)

NOTE: Since Scala 2.13 this is available in standard library

maxOption/maxOptionBy

Finds the largest element wrapped in Option or None if iterable is empty

val m1 = List.empty[Int].maxOption

val m2 = List(1,2,1).maxOptionBy(_ * -1)

NOTE: Since Scala 2.13 this is available in standard library

unzip4/unzip5/unzip6

Similar to unzip and unzip3 from standard library, but for tuples of higher arity (up to 6)

val (ints, strings, chars, doubles) = 
  Iterable(
    (1, "one", '1', 1d),
    (2, "two", '2', 2d),
    (3, "three", '3', 3d),
    (4, "four", '4', 4d)
  ).unzip4

Strings Examples

An import needed for examples to work:

import com.daodecode.scalax._

NonEmptyString

NonEmptyString(null)
NonEmptyString("")
NonEmptyString(" a ")

(null: String) match {
  case NonEmptyString(_) => "boo" 
  case _ => "works!"
}

"" match {
  case NonEmptyString(_) => "boo" 
  case _ => "works!"
}

"works!" match {
  case NonEmptyString(s) => s 
  case _ => "boo"
}

NonBlankString

NonBlankString(null)
NonBlankString("")
NonBlankString(" \n \r \t ")
NonBlankString(" a ")

(null: String) match {
  case NonBlankString(_) => "boo" 
  case _ => "works!"
}

"" match {
  case NonBlankString(_) => "boo" 
  case _ => "works!"
}

"   \t " match {
  case NonBlankString(_) => "boo" 
  case _ => "works!"
}

"works!" match {
  case NonBlankString(s) => s 
  case _ => "boo"
}

NonBlankTrimmedString

NonBlankTrimmedString(null)
NonBlankTrimmedString("")
NonBlankTrimmedString(" \n \r \t ")
NonBlankTrimmedString(" a ")

(null: String) match {
  case NonBlankTrimmedString(_) => "boo" 
  case _ => "works!"
}

"" match {
  case NonBlankTrimmedString(_) => "boo" 
  case _ => "works!"
}

"   \t " match {
  case NonBlankTrimmedString(_) => "boo" 
  case _ => "works!"
}

"  works!\n  " match {
  case NonBlankTrimmedString(s) => s 
  case _ => "boo"
}

Latest stable release

sbt

libraryDependencies += "com.daodecode" %% "scalax-collection" % "0.3.2"

maven

set <scala.binary.version> property to scala version you need, like

<properties>
    <scala.binary.version>2.13</scala.binary.version>
</properties>

and then in dependencies add

<dependency>
    <groupId>com.daodecode</groupId>
    <artifactId>scalax-collection_${scala.binary.version}</artifactId>
    <version>0.3.2</version>
</dependency>








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/jozic/scalax-collection/tree/main/docs

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy