Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Created October 28, 2020 16:29
Show Gist options
  • Select an option

  • Save rupeshtr78/dc0d2d58a824272ba33650f72ae04f59 to your computer and use it in GitHub Desktop.

Select an option

Save rupeshtr78/dc0d2d58a824272ba33650f72ae04f59 to your computer and use it in GitHub Desktop.
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
//Filter
a.distinct // "fo barz"
a.drop(4) // "bar baz"
a.dropRight(2) // "foo bar b"
a.dropWhile(_ != ' ') // " bar baz"
a.filter(_ != 'a') // "foo br bz"
a.filterNot(_ != 'a') // "aa"
a.filterNot(_ == 'a') // "foo br bz"
firstTen.find(_ > 4) // Some(5)
a.head // f
a.headOption // Some(f)
"foo bar".init // "foo ba"
List(1,2,3).last // 3
List(1,2,3).lastOption // Some(3)
a.slice(0,5) // foo b
a.slice(2,9) // o bar b
a.tail // oo bar baz
a.take(3) // foo
a.takeRight(3) // baz
a.takeWhile(_ != 'r') // foo ba
Seq(1,1,2,2,3,3).toSet // Set(1, 2, 3)
firstTen.withFilter(_ > 5) // scala.collection.generic.FilterMonadic[Int,List[Int]]
firstTen.withFilter(_ > 5).map(_ * 1) // List[Int] = List(6, 7, 8, 9, 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment