Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Closures Functions as First Class Citizens Higher Order Functions

Alireza Rafiei
Alireza Rafiei
4,221 Points

Swift code on higher order functions is fine in XCode but here says it has problems. Why?

Why my code is not accepted? In the second part, it asks for passing differenceBetweenNumbers into mathOperation with couple of integers and assigning it to a constant "difference". In Xcode it appears fine and without error.

higherOrderFunctions.swift
/** 
  For this code challenge, let’s define a math operation as a function that 
  carries out some work on two integers and returns an integer as well. An 
  example is the function below, `differenceBetweenNumbers`, which takes two 
  integers and calculates the difference between the numbers. After calculating, 
  it returns the difference.
*/

func differenceBetweenNumbers(a: Int, b:Int) -> (Int) {
  return a - b
}

// Enter your code below

func mathOperation( operation: (Int, Int) -> (Int), first: Int, second: Int) -> (Int) {
  return operation(first, second)
}

let difference = mathOperation(differenceBetweenNumbers, 10, 4)
Ratik Sharma
Ratik Sharma
32,885 Points

I have the exact same issue. Have you fixed it yet?

1 Answer

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Here is the answer (you could change the ints):

let difference = mathOperation(differenceBetweenNumbers, 2, 1)

This forum post has an answer that explains it.

Alireza Rafiei
Alireza Rafiei
4,221 Points

Changing ints is irrelevant. The forum post you provided, indicates that extra parenthesis for return types cause the problem. I just tried func mathOperation( operation: (Int, Int) -> Int, first: Int, second: Int) -> Int with my current ints and it was okay and the assignment passed.

This is a Treehouse bug. Since it accepts mathOperation with extra parenthesis for the first part of the assignment but for the second, it doesn't and displays an error message which is completely irrelevant to the problem. Needless to say that both ways of writing mathOperation (with or without extra parenthesis) are syntactically valid and XCode has no problem with it. I've reported the bug couple of weeks ago but apparently no one here cares! So...

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

Hey Alireza Rafiei,

The problem should be fixed now. Our backend was inferring (Int) as a tuple containing an Int. That should match the Xcode result now