lmiguelvargasf - 1 month ago 7

Swift Question

I have the following function:

`private func getFunctionToPlot() -> ((Double) -> Double) {`

var function: (Double) -> Double = sin + cos

return function

}

I would like to return a function that takes a double and returns a double, but this functions should be something as

`sin + cos`

`sin($0) + cos($0)`

Answer

Your question is a little broad, but the line you claim doesn't compile *does* compile; there's nothing wrong with what you're doing. The only problem is that your code is incomplete. Here is a test example that *is* complete:

```
private func getFunctionToPlot() -> ((Double) -> Double) {
var function: (Double) -> Double
function = {_ in return 2.0}
return function
}
```

**EDIT** As for your revised question, here's a test example of the sort of thing you now seem to be trying to do:

```
func sin(_ d:Double) -> Double {return 1}
func cos(_ d:Double) -> Double {return 1}
private func getFunctionToPlot() -> ((Double) -> Double) {
let f1: (Double) -> Double = sin
let f2: (Double) -> Double = cos
return {f1(f2($0))} // or maybe f1($0) + f2($0)
}
```