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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -