Last active
November 13, 2020 03:28
-
-
Save hyunjun/4b1c4dd02e91268c9ee34ead6b9284fc to your computer and use it in GitHub Desktop.
list.scala
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
scala> val aa: List[(List[Int], List[Int])] = | |
| List((List(1, 2, 3), List(4, 5, 6)), (List(7, 8, 9), List(10, 11, 12))) | |
| val bb: (List[Int], List[Int]) = (List(1, 2, 3, 7, 8, 9), List(4, 5, 6, 10, 11, 12)) | |
val aa: List[(List[Int], List[Int])] = List((List(1, 2, 3),List(4, 5, 6)), (List(7, 8, 9),List(10, 11, 12))) | |
val bb: (List[Int], List[Int]) = (List(1, 2, 3, 7, 8, 9),List(4, 5, 6, 10, 11, 12)) | |
scala> aa.unzip.map[[t] =>> List[Int]]( | |
| [t] => (_: t) match { | |
| case x: List[List[Int]] => x.flatten | |
| } | |
| ) | |
^ | |
error: identifier expected but '[' found. | |
scala> aa.unzip match { case (a, b) => (a.flatten, b.flatten) } | |
val res7: (List[Int], List[Int]) = (List(1, 2, 3, 7, 8, 9),List(4, 5, 6, 10, 11, 12)) | |
scala> aa.foldLeft((List.empty[Int], List.empty[Int])) { case ((a, b), (c, d)) => (a ++ c, b ++ d) } | |
val res10: (List[Int], List[Int]) = (List(1, 2, 3, 7, 8, 9),List(4, 5, 6, 10, 11, 12)) | |
// scala 3에서 가능하다고 함. Tuple#map과 polymorphic function 사용: https://scastie.scala-lang.org/cbEqHtQhRUSev5Ng4eJoUA |
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
scala> case class Names(firstName: String, lastName: String) | |
class Names | |
scala> val m: Option[Seq[Names]] = Some(List(Names("f1", "l1"), Names("f2", "l2"))) | |
val m: Option[Seq[Names]] = Some(List(Names(f1,l1), Names(f2,l2))) | |
scala> val n: Option[Seq[Names]] = None | |
val n: Option[Seq[Names]] = None | |
scala> def getLastName(names: Option[Seq[Names]]) = names.map(x => x.map(t => s"${t.firstName} ${t.lastName}")).getOrElse(List.empty).filter(_.trim != "") | |
def getLastName(names: Option[Seq[Names]]): Seq[String] | |
scala> getLastName(m) | |
val res5: Seq[String] = List(f1 l1, f2 l2) | |
scala> getLastName(n) | |
val res6: Seq[String] = List() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment