데프 방법과 발 기능에 대 flatMap을 평평 :데프 방법과 발 기능에 대 flatMap을 평평하게
def toInt(s: String): Option[Int] = {
try {
Some(Integer.parseInt(s.trim))
} catch {
case e: Exception => None
}
}
그리고이 방법이 잘 작동 : 나는 toInt라는 데프 방법을 정의
다음과 같이 flatten 및 flatMap을 사용합니다.
//using toInt method
val x = 1.to(5).toList
val y = List("a")
val z = x ++ y
val q = z.map(_.toString)
//using map and flatten
println(q.map(toInt).flatten)
//using flatMap
println(q.flatMap(toInt))
이제는 동일한 toInt
//using map and flatten
println(q.map(tooInt).flatten)
//using flatMap // this has error
**println(q.flatMap(tooInt))**
당신이 좀 도와 주 시겠어요 : 함수 "tooInt"에서 발을 사용하여 (DEF 방법 등) 기능 : 아래 그림과 같이
val tooInt: String => Option[Int] = s => {
try {
Some(Integer.parseInt(s.trim))
} catch {
case c: Exception => None
}
}
이 flatMap와 평평하게하지만 하지와 함께 잘 작동 이걸 이해하는거야? 그것은 모두 우리가 암시 option2Iterable
에 정의가 있다는 사실에 귀결
q.flatMap(s => tooInt(s))
:
최고 감사합니다, 키란
도 Q를. flatMap (tooInt (_))가 작동합니다. 그 질문은 flatMap에 메소드를 전달할 수있는 이유와 함수 리터럴이 전달할 수없는 문제에 관한 것 같습니다. – jrook