Last active
December 23, 2016 03:59
-
-
Save SunKing2/4cbdeeeb6144302702fc260923a7390e to your computer and use it in GitHub Desktop.
Scala List methods, operations and functions Examples code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// lists | |
// Most examples come from | |
// Functional Programming in Scala | |
// Seven Languages in Seven Weeks | |
// Programming In Scala | |
// Learn You A Haskell | |
object ListOperations { | |
def main(args: Array[String]) = { | |
val a = List(1,3,5) | |
val b = List(7, 9) | |
println("******** merge lists, prepend, retrieve one element, ==, >*****") | |
// merge | |
val ab = a ::: b | |
println(ab) // List(1, 3, 5, 7, 9) | |
// prepend an element | |
println(4 :: a) //List(4, 1, 3, 5) | |
// element #2 (zero indexed) | |
println(a(2)) // 5 | |
//compare equality | |
println(a == List(1,3,5)) // true | |
println() | |
// compare greater than | |
import Ordering.Implicits._ | |
println(a > List(1,3,6)) // false | |
println(a > List(2,3,5)) // false | |
println(a > List(1,3,4)) // true | |
println(a > List(0,3,5)) // true | |
println() | |
println("********create a list*****") | |
// create a list of datatype Any | |
println(List(2, 4, "cow")) // List(2, 4, cow) | |
// create a list another way | |
val nums = 1 :: 2 :: 3 :: Nil | |
println(nums) // List(1, 2, 3) | |
println() | |
println("********head tail last init isEmpty length reverse drop take*****") | |
println("List(1,3,5,7,9) head, tail, last, init") | |
println(ab.head + " " + ab.tail + " " + ab.last + " " + ab.init + " " ) | |
// 1 List(3, 5, 7, 9) 9 List(1, 3, 5, 7) | |
println(".isEmpty length reverse=" + ab.isEmpty + " " + ab.length + " " + ab.reverse) | |
// .isEmpty length reverse=false 5 List(9, 7, 5, 3, 1) | |
println(".drop(1) .drop(2)=" + ab.drop(1) + " " + ab.drop(2)) | |
// .drop(1) .drop(2)=List(3, 5, 7, 9) List(5, 7, 9) | |
println(".take(1) .take(2)=" + ab.take(1) + " " + ab.take(2)) | |
// .take(1) .take(2)=List(1) List(1, 3) | |
println() | |
println("********exists forall count filter filterNot*****") | |
// exists : just returns true or false, regardless of how many | |
val bundy = List("al", "peg", "kelly", "bud") | |
println(bundy.exists(a => a.size > 3)) // true | |
// forall true or false: do all elements match? | |
println(bundy.forall(a => a.size < 5)) // false | |
//count | |
println(bundy.count(a => a.size == 3)) // 2 | |
// filter | |
println(bundy.filter(a => a.size == 3)) // List(peg, bud) | |
// next 2 are concise!!! | |
println(bundy.filter(_.size == 3)); // List(peg, bud) | |
println(bundy.filter(_ != "kelly")) // List(al, peg, bud) | |
// filterNot | |
println(bundy.filterNot(a => a.size == 3)) // List(al, kelly) | |
println() | |
println("********forall and foreach*****") | |
// forall and foreach | |
// bundy.forall(s => println(s)) illegal, requires a Boolean | |
bundy.foreach(s => println(s)) // outputs each element | |
bundy.foreach(println) // nice and concise :) | |
println() | |
println("********sort map*****") | |
// sort | |
println(bundy.sorted) // List(al, bud, kelly, peg) | |
// sort using function : by word length, for example | |
println(bundy.sortWith((s, t) => s.size < t.size)) // List(al, peg, bud, kelly) | |
// next is concise! | |
println(bundy.sortWith(_.length < _.length)) // List(al, peg, bud, kelly) | |
// map | |
println(bundy.map(name => name.size)) // List(2, 3, 5, 3) | |
println(bundy.map(name => name.toUpperCase)) // List(AL, PEG, KELLY, BUD) | |
println(bundy.map(_.toUpperCase)) // List(AL, PEG, KELLY, BUD) | |
// the next is what????, remove letter L from each element | |
println(bundy.map(_.filter(_ != 'l'))) // List(a, peg, key, bud) | |
println() | |
println("********foldLeft*****") | |
// (foldLeft) | |
// create String by concatenating all elements: | |
println(bundy.foldLeft("")((b,a) => b + a)) // alpegkellybud | |
println() | |
println("********Numbers array sortWith foldLeft foreach*****") | |
val myNums = List(3,1,7,5) | |
// Here's an aside: sort numbers reverse concise; so beautiful :) | |
println(myNums.sortWith(_ > _)) // List(7, 5, 3, 1 | |
// add up all nums to one sum | |
println(myNums.foldLeft(0)((b,a) => b+a)) // 16 | |
println() | |
println("******** Numbers array various methods *****") | |
// when looking at the API docs, don't forget to look at the | |
// docs for the companion object to the List class | |
println(List.fill(3)(42)) // List(42, 42, 42) | |
println(List.fill(4)("Mary")) // List(Mary, Mary, Mary, Mary) | |
println(List.tabulate(3)(n => "a" + n)) // List(a0, a1, a2) | |
println(1 +: List(3,5,7,9) :+ 11) // List(1, 3, 5, 7, 9, 11) | |
// List.range is is a 1 until 11, not 1 to 11, 3rd argument optional | |
println(List.range(1,11,2)) // List(1, 3, 5, 7, 9) // | |
// create same list using for / yield (comprehension is for (a <- b...) yield element | |
println(for (i <- List.range(1, 10) if i % 2 == 1) yield i) // List(1, 3, 5, 7, 9) | |
println(List(1,3,5,7,9).map(_ * 2)) // List(2, 6, 10, 14, 18) | |
println() | |
println("********replacing, changing in middle of list *****") | |
// these 2 Rex Kerr, Stack Exchange | |
println(List(1,3,5,7,9).updated(3,42)) // List(1, 3, 5, 42, 9) | |
println(List(9,0,2,1,0).map{ case 0 => 7; case x =>x }) // List(9, 7, 2, 1, 7) | |
// these 3 Vasil Remeniuk; Stack Exchange | |
// replace 1 element at index 3 with a 9 | |
println(List(1,2,3,4,5).patch(3,Seq(9),1)) // List(1, 2, 3, 9, 5) | |
// replace 2 elements at index 3 with a 5 | |
println(List(1,2,3,4,5).patch(3,Seq(9),2)) // List(1, 2, 3, 9) | |
// insert a new element at position 3 | |
println(List(1,2,3,4,5).patch(3,Seq(9),0)) // List(1, 2, 3, 9, 4, 5) | |
// Ben Reich , Stack Exchange | |
println(List(1,2,3,4,5).patch(3,List(9),0)) // List(1, 2, 3, 9, 4, 5) | |
} // main | |
} // object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment