Scala function => as parameter -
could kindly explain me why following
/** * returns set transformed applying `f` each element of `s`. */ def map(s: set, f: int => int): set = x => exists(s, y => f(y) == x)
is not equivalent to
def map(s: set, f: int => int): set = x => exists(s, f(x))
where "exists" function returns whether there exists bounded integer within s
(the first argument) satisfies p
(the second argument).
why need specify "y => f(y) == x"? million!
exists
's second argument has type int => boolean
(right?), in other words, expects function int
boolean
. now, f(x)
doesn't conform type - has type int
. - y => f(y) == x
creates function correct type, returns true if input equals x
.
if excess characters bug - can shorten bit using anonymous argument '_':
def map(s: set, f: int => int): set = x => exists(s, f(_) == x)
Comments
Post a Comment