I'm taking the Coursera FP Principles in Scala course, and I'm having trouble with the last function in the week 2 assignment. I don't want the answer, but rather some help in understanding Scala functional composition. I think I have the right idea as to how to solve the problem, I'm just getting tripped up on the Scala-specific part.
The gist is: We've been given a type alias, defined as such:
type Set = Int => Boolean
Which I've interpreted to mean a Set is a function that takes an Int and returns a Bool.
We've also been tasked with finishing the function singletonSet, which takes an int and returns a Set. I've written it as such
def singletonSet(x: Int): Set = Set(x)
val three = singletonSet(3)
three(3) //True
three(5) //False
The function I'm having trouble with is the Map function, which has this signature:
def map(s: Set, p: Int => Int): Set
Which I've interpreted to mean A function that takes a set, transforms its elements with function P, and returns a new Set.
The pesudocode: Map returns a function that takes an int, and if that int exists in Set s, return a new Set with the transformed int X (or p(x)
The actual code that's breaking:
def map(s: Set, p: Int => Int): Set = {
x =>
if (s(x)) singletonSet(p(x))
else p(x) => false
}
The error that I'm getting with this format is:
error: not a legal formal parameter.
Note: Tuples cannot be directly destructured in method or function parameters.
Either create a single parameter accepting the Tuple1,
or consider a pattern matching anonymous function: `{ case (param1, param1) => ... }
else p(x) => false
I'm not grokking what's wrong with my implementation. A friend wrote my logic out in Haskell and found it to be successful, so I think my algorithm is correct (although I could be wrong). I'm struggling with the Scala implementation details. Any advice or guidance is much appreciated.
x => ...at the beginning, so you just need the body of the new function which should be a boolean expression, but following theelsekeyword you havep(x) => falsewhich makes no sense and it's definitely not a boolean expression