functional programming - Use of Underscore in Scala -
why works?
def exists(s: set, p: int => boolean): boolean = { forall(s, !p(_)) }
and doesn't?
def exists(s: set, p: int => boolean): boolean = { forall(s, !p()) }
where forall
function, , p
predicate.
the call predicate expecting parameter passed, can't call without passing (which p()
doing).
the underscore kind of scala short-hand "the current value", value int passed p. if explicitly label int i
, de-sugars to:
{ forall(s, (i: int) => !p(i)) }
Comments
Post a Comment