Created
June 7, 2010 02:00
-
-
Save Synesso/428118 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Literals | |
| val dogs = List("Barnie", "Princess", "Mungo") | |
| val empty = List() | |
| // * Parameterised Types - you can't have an unparameterised List type | |
| val noStrings = List[String]() | |
| // Immutable (by default) | |
| // * The default type of List is immutable. (the mutable List type must be imported specifically) | |
| val l = List(1,2,3) | |
| l ++ List(4,5,6) | |
| l | |
| // Heads and Tails | |
| // * any list can be dissected with head & tail | |
| // * these are constant time operations | |
| val list = List("first", "second", "all the rest") | |
| list.head | |
| list.tail | |
| list.tail.head | |
| // Alternative syntax for lists :: (cons) | |
| Nil | |
| 1 :: 2 :: 3 :: Nil | |
| // * Cons is a right-associative method. List -> List | |
| // * [] cons 3 = [3] | |
| // * [3] cons 2 = [2,3] | |
| // * [2,3] cons 1 = [1,2,3] | |
| // Pattern matching. | |
| List("iPad", "iPhone", "iPod") match { | |
| case List(a, b, c) => b | |
| case _ => "No match" | |
| } | |
| // ... but! | |
| List("iPad", "iPhone", "iPod") match { | |
| case List(a, b, c, d) => b | |
| case _ => "No match" | |
| } | |
| // Pattern matching with alternate syntax | |
| List("iPad", "iPhone", "iPod", "iPuppy") match { | |
| case a :: b :: c => b | |
| case _ => "No match" | |
| } | |
| List("iPad", "iPhone", "iPod", "iPuppy") match { | |
| case a :: b :: c => { println(c); b } | |
| case _ => "No match" | |
| } | |
| List("iPad") match { | |
| case a :: b :: c => { println(c); b } | |
| case _ => "No match" | |
| } | |
| // First order methods | |
| val list = List('a', 'b', 'c') | |
| list ::: List('d', 'e') | |
| list.length | |
| list.reverse | |
| list.init | |
| list.last | |
| list.drop(2) | |
| list.take(2) | |
| list.splitAt(2) | |
| list.zip(List("Aay", "Bee", "See")) | |
| list.mkString(" -> ") | |
| // Higher order methods | |
| val list = List("Moist,", "my", "evil", "moisture", "buddy") | |
| val lengthFunc: (String) => Int = s => s.length | |
| list.map(lengthFunc) | |
| list.map(s => s.length) | |
| list.map(_.length) | |
| list.map(_.toList) | |
| list.flatMap(_.toList) | |
| list.foreach(println) | |
| list.filter(_.length % 2 != 0) | |
| list.partition(_.length % 2 != 0) | |
| list.find(_.length == 3) | |
| list.find(_.length == 4) | |
| list.takeWhile(_.contains('i')) | |
| list.dropWhile(_.contains('i')) | |
| list.span(_ contains 'i') | |
| list.forall(s => s.toLowerCase.equals(s)) | |
| list.exists(s => s.toLowerCase.equals(s)) | |
| list.reduceLeft(_ + " was " + _ + " not") | |
| list.foldLeft(0) {(max, next) => if (next.length > max) next.length else max} | |
| list.sort(_.length < _.length) | |
| // Methods on the List object | |
| List.range(1,10) | |
| List.range(1,20,3) | |
| List.make(9, "bewdie") | |
| List.unzip(List((1,"a"), (2, "b"), (4, "x"))) | |
| List.map2(List("A","B","C"), List(1,2,3)) { _*_ } | |
| List.forall2(List("be", "right", "there"), List(2,5,5)) { _.length == _ } | |
| // * Example - Project Euler #4 | |
| // A palindromic number reads the same both ways. | |
| // The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. | |
| // Find the largest palindrome made from the product of two 3-digit numbers. | |
| val l = List.range(100,999) | |
| val products = l.flatMap(i => List.range(i, 100, -1).map(i * _)) | |
| val palendromes = products.filter(i => i.toString == i.toString.reverse.toString) | |
| val maybeAnswer = palendromes.sort(_>_).firstOption |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment