Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Created October 28, 2020 16:40
Show Gist options
  • Save rupeshtr78/eec9633db777c1d3c84337c9e0a4fdb3 to your computer and use it in GitHub Desktop.
Save rupeshtr78/eec9633db777c1d3c84337c9e0a4fdb3 to your computer and use it in GitHub Desktop.
package PlayGround
object ScalaCollectionsTransform {
val evens = List(2,4,6)
val odds = List(1,3,5)
val a = "foo bar baz"
val foo = "foo"
val bar = "bar"
val names = List("Al","Chistina","Kim")
val firstTen = (1 to 10).toList
val fiveToFifteen = (5 to 15).toList
///2) Transformer and reduction methods
evens ++ odds // List(2, 4, 6, 1, 3, 5)
evens +: odds // List(2, 4, 6, 1, 3, 5)
0 +: evens // List(0, 2, 4, 6)
odds :+ 7 // List(1, 3, 5, 7)
0 :: evens // List(0, 2, 4, 6)
evens :: odds // List(List(2, 4, 6), 1, 3, 5)
evens ::: odds // List(2, 4, 6, 1, 3, 5)
(0 /: evens)(_ + _) // 12
(evens :\ 0)(_ + _) // 12
// collect and collectFirst take a partial function
firstTen.collect{case x if x % 2 == 0 => x} // List(2, 4, 6, 8, 10)
firstTen.collectFirst{case x if x > 1 => x} // Some(2)
a.diff("foo") // " bar baz"
a.distinct // "fo barz"
names.flatten // List(A, l, C, h, r, i, s, t, i, n, a, K, i, m)
names.flatMap(_.toUpperCase) // List(A, L, C, H, R, I, S, T, I, N, A, K, I, M)
firstTen.groupBy(_ > 5) // Map(false -> List(1, 2, 3, 4, 5), true -> List(6, 7, 8, 9, 10))
firstTen.grouped(2) // Iterator[List[Int]] = non-empty iterator
firstTen.grouped(2).toList // List(List(1, 2), List(3, 4), List(5, 6), List(7, 8), List(9, 10))
firstTen.grouped(5).toList // List(List(1, 2, 3, 4, 5), List(6, 7, 8, 9, 10))
firstTen.indices // Range(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
"foo".indices // Range(0, 1, 2)
firstTen.intersect(fiveToFifteen) // List(5, 6, 7, 8, 9, 10)
a.map(_.toUpper) // FOO BAR BAZ
10.byteValue()
a.mkString(",") // f,o,o, ,b,a,r, ,b,a,z
a.mkString("->", ",", "<-") // ->f,o,o, ,b,a,r, ,b,a,z<-
a.par // a parallel array, ParArray(f, o, o, , b, a, r, , b, a, z)
a.partition(_ > 'e') // (foorz, " ba ba") // a Tuple2
firstTen.partition(_ > 5) // (List(6, 7, 8, 9, 10),List(1, 2, 3, 4, 5))
a.replace('o', 'x') // fxx bar baz
a.replace("o", "x") // fxx bar baz
a.replaceAll("o", "x") // fxx bar baz
a.replaceFirst("o", "x") // fxo bar baz
firstTen.reverse // List(10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
// (more on scan, scanLeft, and scanRight below)
Seq(1,2,3,4,5).scan(1)(_ + _) // List(1, 2, 4, 7, 11, 16)
Seq(1,2,3,4,5).scanLeft(1)(_ + _) // List(1, 2, 4, 7, 11, 16)
Seq(1,2,3,4,5).scanRight(1)(_ + _) // List(16, 15, 13, 10, 6, 1)
a.slice(0,5) // foo b
a.slice(2,9) // o bar b
firstTen.sliding(2) // Iterator[List[Int]] = non-empty iterator
firstTen.sliding(2).toList // List(List(1, 2), List(2, 3), List(3, 4), List(4, 5), List(5, 6), List(6, 7), List(7, 8), List(8, 9), List(9, 10))
firstTen.sliding(4).toList // List(List(1, 2, 3, 4), List(2, 3, 4, 5), List(3, 4, 5, 6), List(4, 5, 6, 7), List(5, 6, 7, 8), List(6, 7, 8, 9), List(7, 8, 9, 10))
firstTen.sliding(2,2).toList // List(List(1, 2), List(3, 4), List(5, 6), List(7, 8), List(9, 10))
firstTen.sliding(2,3).toList // List(List(1, 2), List(4, 5), List(7, 8), List(10))
firstTen.sliding(2,4).toList // List(List(1, 2), List(5, 6), List(9, 10))
a.sortWith(_ < _) // " aabbfoorz"
a.sortWith(_ > _) // "zroofbbaa "
a.sorted // " aabbfoorz"
firstTen.span(_ < 5) // (List(1, 2, 3, 4),List(5, 6, 7, 8, 9, 10))
a.split(" ") // Array(foo, bar, baz)
a.splitAt(3) // (foo," bar baz")
firstTen.startsWith(Seq(1,2)) // true
a.take(3) // foo
a.takeRight(3) // baz
a.takeWhile(_ != 'r') // foo ba
a.toArray // Array(f, o, o, , b, a, r, , b, a, z)
a.toBuffer // ArrayBuffer(f, o, o, , b, a, r, , b, a, z)
a.toList // List(f, o, o, , b, a, r, , b, a, z)
Seq(1,1,2,2,3,3).toSet // Set(1, 2, 3)
firstTen.toStream // scala.collection.immutable.Stream[Int] = Stream(1, ?)
a.toLowerCase // foo bar baz
a.toUpperCase // FOO BAR BAZ
a.toVector // Vector(f, o, o, , b, a, r, , b, a, z)
a.trim // "foo bar baz"
evens.union(odds) // List(2, 4, 6, 1, 3, 5)
Seq(1,2,3).updated(0,10) // List(10, 2, 3)
firstTen.view // scala.collection.SeqView[Int,List[Int]] = SeqView(...)
a.zip(0 to 10) // Vector((f,10), (o,11), (o,12), ( ,13), (b,14), (a,15), (r,16), ( ,17), (b,18), (a,19), (z,20))
Seq(1,2,3).zipAll(Seq('a', 'b'), 0, 'z') // List((1,a), (2,b), (3,z))
Seq(1,2).zipAll(Seq('a', 'b', 'c'), 0, 'z') // List((1,a), (2,b), (0,c))
a.zipWithIndex
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment