Created
May 21, 2013 00:44
-
-
Save ryanlecompte/5616773 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
➜ scala | |
Welcome to Scala version 2.9.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_41). | |
Type in expressions to have them evaluated. | |
Type :help for more information. | |
scala> val extractor: Int => Option[Int] = { i => println("checking i = %d".format(i)); if (i == 10) Some(i) else None } | |
extractor: Int => Option[Int] = <function1> | |
scala> val seq = Seq.range(1,1000000) | |
seq: Seq[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177,... | |
// CALLED TWICE HERE | |
scala> seq.collectFirst(Function.unlift(extractor)) | |
checking i = 1 | |
checking i = 2 | |
checking i = 3 | |
checking i = 4 | |
checking i = 5 | |
checking i = 6 | |
checking i = 7 | |
checking i = 8 | |
checking i = 9 | |
checking i = 10 | |
checking i = 10 | |
res0: Option[Int] = Some(10) | |
// CALLED ONCE HERE | |
scala> seq.view.map(extractor).collectFirst { case Some(v) => v } | |
checking i = 1 | |
checking i = 2 | |
checking i = 3 | |
checking i = 4 | |
checking i = 5 | |
checking i = 6 | |
checking i = 7 | |
checking i = 8 | |
checking i = 9 | |
checking i = 10 | |
res6: Option[Int] = Some(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment