Publish to my blog (weekly)
-
스칼라 강좌 (25) - 부분 함수 (Partial function)
- 그 예외 항목이 먼지 알려주는 함수가 isDefinedAt 입니다.
- PartialFunction 은 아래와 같이 만들며 자동으로 isDefinedAt 이 만들어지는게 특징입니다.
- [Int, Int] 는 인자로 Int 리턴값으로 Int 를 말합니다.
-
-
- Partial Function 을 이해하는 쉬운 방법은, 일반 Function 을 Total Function 으로 이해하면 된다. 즉, 일반 Function 이 주어진 인자에 대해 모든 값을 취한다면, Partial Function 은 주어진 타입에 대해서 특정 값만 취할 수 있다. Scala School 의 원문을 첨부하면
- A function works for every argument of the defined type. In other words, a function defined as (Int) => String takes any Int and returns a String.
A Partial Function is only defined for certain values of the defined type. A Partial Function (Int) => String might not accept every Int. - 처음엔
case가Function을 만든다고 했지만, 사실PartialFunction을 정의한다. -
map나filter는Function을 받지만,PartialFunction은Function의 subtype 이므로 파라미터로 넘겨줄 수 있다.
-
-
pattern matching - How does a "case" anonymous function really work in Scala? - Stack Overflow
- One is for writing anonymous functions that accept tuples and work on their components, as you did with
f1. An example would be the function you pass to theforeachormapmethod on aMap, e.g.Map(1 -> 2, 3 -> 4) map { case (k, v) => k + v }. - The second is for writing an anonymous function that does a
matchon its sole parameter. Yourf2is doing this, but not in any useful way. An example would be the anonymous function passed tocollect, e.g.List(1, -2, 3) collect { case x if x > 0 => -x }.
-
댓글