A small library of extension methods for standard scala collections library. Published to maven central.
2.10 | 2.11 | 2.12 | 2.13 | 3 |
---|---|---|---|---|
An import needed for examples to work:
import com.daodecode.scalax.collection.extensions._
preserving first duplicate
val xs = List(1 -> "one", 1 -> "ten", 2 -> "two", 2 -> "twenty").distinctBy(_._1)
// xs: List[(Int, String)] = List((1, "one"), (2, "two"))
NOTE: Since Scala 2.13 this method is available in standard library
or preserving any duplicate you want
val xs = List(1 -> "one", 1 -> "ten", 2 -> "two", 2 -> "twenty").distinctByUsing(_._1, takeFirst = _._2.length > _._2.length)
// xs: List[(Int, String)] = List((1, "ten"), (2, "twenty"))
NOTE: Before 0.3.0 this method was named distinctBy
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 }
// xs: List[Int] = List(1, 2, 3, 4, 5)
since Scala 2.13 can be seen as equivalent to groupMap(_._1)(_._2)
val cm = List(1 -> "1", 2 -> "2", 1 -> "11").toCompleteMap
// cm: Map[Int, List[String]] = Map(1 -> List("1", "11"), 2 -> List("2"))
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 }
// m: Map[Int, String] = Map(1 -> "one", 2 -> "two")
since Scala 2.13 can be seen as equivalent to groupMapReduce(f)(identity)((b,_) => b)
val m = List("1", "2", "1").toMapWithKey(_.toInt)
// m: Map[Int, String] = Map(1 -> "1", 2 -> "2")
since Scala 2.13 can be seen as equivalent to groupMapReduce(identity)(f)((b,_) => b)
val m = List("1", "2", "1").toMapWithValue(_.toInt)
// m: Map[String, Int] = Map("1" -> 1, "2" -> 2)
since Scala 2.13 can be seen as equivalent to groupMapReduce(identity)(_ => 1)(_ + _)
val fm = List("a", "b", "c", "a", "b", "d").withFrequency
// fm: Map[String, Int] = Map("a" -> 2, "b" -> 2, "c" -> 1, "d" -> 1)
since Scala 2.13 can be seen as equivalent to groupMapReduce(f)(_ => 1)(_ + _)
val fm = List("ab", "bc", "cd", "ab", "bc", "de").withFrequencyBy(_.head)
// fm: Map[Char, Int] = Map('a' -> 2, 'b' -> 2, 'c' -> 1, 'd' -> 1)
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))(_ + _)
// merged: Map[String, Int] = Map("1" -> 2, "2" -> 4)
Finds the smallest element wrapped in Option
or None
if iterable is empty
val m1 = List.empty[Int].minOption
// m1: Option[Int] = None
val m2 = List(1,2,1).minOptionBy(_ * -1)
// m2: Option[Int] = Some(value = 2)
NOTE: Since Scala 2.13 this is available in standard library
Finds the largest element wrapped in Option
or None
if iterable is empty
val m1 = List.empty[Int].maxOption
// m1: Option[Int] = None
val m2 = List(1,2,1).maxOptionBy(_ * -1)
// m2: Option[Int] = Some(value = 1)
NOTE: Since Scala 2.13 this is available in standard library
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
// ints: Iterable[Int] = List(1, 2, 3, 4)
// strings: Iterable[String] = List("one", "two", "three", "four")
// chars: Iterable[Char] = List('1', '2', '3', '4')
// doubles: Iterable[Double] = List(1.0, 2.0, 3.0, 4.0)
An import needed for examples to work:
import com.daodecode.scalax._
NonEmptyString(null)
// res0: Option[String] = None
NonEmptyString("")
// res1: Option[String] = None
NonEmptyString(" a ")
// res2: Option[String] = Some(value = " a ")
(null: String) match {
case NonEmptyString(_) => "boo"
case _ => "works!"
}
// res3: String = "works!"
"" match {
case NonEmptyString(_) => "boo"
case _ => "works!"
}
// res4: String = "works!"
"works!" match {
case NonEmptyString(s) => s
case _ => "boo"
}
// res5: String = "works!"
NonBlankString(null)
// res6: Option[String] = None
NonBlankString("")
// res7: Option[String] = None
NonBlankString(" \n \r \t ")
// res8: Option[String] = None
NonBlankString(" a ")
// res9: Option[String] = Some(value = " a ")
(null: String) match {
case NonBlankString(_) => "boo"
case _ => "works!"
}
// res10: String = "works!"
"" match {
case NonBlankString(_) => "boo"
case _ => "works!"
}
// res11: String = "works!"
" \t " match {
case NonBlankString(_) => "boo"
case _ => "works!"
}
// res12: String = "works!"
"works!" match {
case NonBlankString(s) => s
case _ => "boo"
}
// res13: String = "works!"
NonBlankTrimmedString(null)
// res14: Option[String] = None
NonBlankTrimmedString("")
// res15: Option[String] = None
NonBlankTrimmedString(" \n \r \t ")
// res16: Option[String] = None
NonBlankTrimmedString(" a ")
// res17: Option[String] = Some(value = "a")
(null: String) match {
case NonBlankTrimmedString(_) => "boo"
case _ => "works!"
}
// res18: String = "works!"
"" match {
case NonBlankTrimmedString(_) => "boo"
case _ => "works!"
}
// res19: String = "works!"
" \t " match {
case NonBlankTrimmedString(_) => "boo"
case _ => "works!"
}
// res20: String = "works!"
" works!\n " match {
case NonBlankTrimmedString(s) => s
case _ => "boo"
}
// res21: String = "works!"
libraryDependencies += "com.daodecode" %% "scalax-collection" % "0.3.2"
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>