Skip to content

Instantly share code, notes, and snippets.

@noahlz
Created December 23, 2013 21:00
Show Gist options
  • Save noahlz/8104479 to your computer and use it in GitHub Desktop.
Save noahlz/8104479 to your computer and use it in GitHub Desktop.
// Welcome to Scala version 2.9.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_15).
// Type in expressions to have them evaluated.
// Type :help for more information.
def foo(n: Int) = Some(n)
// foo: (n: Int)Some[Int]
List(1,2,3,4).map(foo(_))
// res2: List[Some[Int]] = List(Some(1), Some(2), Some(3), Some(4))
List(1,2,3,4).flatMap(foo(_))
// res3: List[Int] = List(1, 2, 3, 4)
List(1,2,3,4).flatMap(foo(_).toList)
// res4: List[Int] = List(1, 2, 3, 4)
// Extra fun!
List(1,2,3,4).toSet()
// res5: Boolean = false
List(1,2,3,4).toSet
// res6: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
````
@noahlz
Copy link
Author

noahlz commented Dec 23, 2013

Still not clear what the issue is here. flatMap on a List[Opton[Int]] returning List[Int] (due to implicit conversion in Predefs) seems like a feature...?

@ericacm
Copy link

ericacm commented Dec 23, 2013

flatMap works because of Option.option2iterable.
I think List(1,2,3,4).toSet() == false is a bug.

@noahlz
Copy link
Author

noahlz commented Dec 24, 2013

@ericacm Some have told me that the presence of that implicit is the "bug" (I.e. another strike against scala, making it broken as a functional language?). The toSet() thing is some kind of syntax idiosyncrasy, supposedly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment