Help me figure this out

//: ### Become an A student
let recentGrade = [“A”, “A”, “A”, “A”, “B”, “D”] // It can be any data

//: Imperative/Non-functional
//var happyGrade: [String] =

/*
print(happyGrade)

//: ### Functional Way
//: Create closure block that returns ‘true’/‘false’
*/
var isMomHappy: (String) → Bool = { (grade) in
return grade == “A”
}

//: Create function that takes the closure

func simpleStringFilter(grades: [String], operation: (String) → Bool) → [String] {
var happyGrade: [String] =

for grade in grades {
    if operation(grade) {
        happyGrade.append(grade)
    } else {
        print("Not the grade i want")
    }
}
return happyGrade

}

simpleStringFilter(grades: recentGrade, operation: isMomHappy)

func simpleStringFilter1(grades: [String], operation: (String) → Bool) {
var happyGrade: [String] =

for grade in grades {
    if operation(grade) {
        happyGrade.append(grade)
    } else {
        print("....")
    }
}
return happyGrade

}

simpleStringFilter1(grades: recentGrade, operation: isMomHappy)

its throwing a “Unexpected non-void return value in void function” on simpleStringFilter1 return happyGrade. I am not understanding why this is happening

Hi @lac_ekim, have you tried adding → [String] after the function declaration? It might make your error go away. It seems like you want the variable happyGrade to return a value. Please try out the code below and let me know if it works!

Best, Gina

func simpleStringFilter1(grades: [String], operation: (String) → Bool) → [String] {
var happyGrade: [String] =

for grade in grades {
    if operation(grade) {
        happyGrade.append(grade)
    } else {
        print("....")
    }
}
return happyGrade

}

1 Like

in other words im a idiot haha…thanks

I have a question, in this

func simpleStringFilter(grades: [String], operation: (String) → Bool) → [String] {
var happyGrade: [String] =

for grade in grades {
    if operation(grade) {
        happyGrade.append(grade)
    } else {
        print("\(grade) not happy")
    }
}
return happyGrade

}
return happyGrade
}

print(simpleStringFilter(grades: recentGrade, operation: isMomHappy))

and

func simpleStringFilter(grades: [String], operation: (String) → Bool) → [String] {
var happyGrade: [String] =

for grade in grades {
    if grade == "A" {
        happyGrade.append(grade)
    } else {
        print("\(grade) not happy")
    }
}
return happyGrade

}

print(simpleStringFilter(grades: recentGrade, operation: isMomHappy))

The top one is supposed to be the way but I couldn’t get it to work right. The bottom one is the one I wore got to work right.

first ones print out

A not happy
A not happy
A not happy
A not happy
B not happy
D not happy

and second

B not happy
D not happy
[“A”, “A”, “A”, “A”]

the second one is correct but the tutorial used the first one and I got a different result then they did. Im trying to get functional programming, I see all the perks to.

The issue is

func mikeFilter(array: [T], operation: (T) → Bool) → [T] {
var result: [T] =

for element in array {
if operation(element) {
result.append(element)
} else {
print(“(result) not happy”)
}
}
return result
}

print(mikeFilter(array: recentGrade, operation: isMomHappy))

and

func simpleStringFilter(grades: [String], operation: (String) → Bool) → [String] {
var happyGrade: [String] =

for grade in grades {
if grade == “A” {
happyGrade.append(grade)
} else {
print(“(grade) not happy”)
}
}
return happyGrade
}

is this,

if operation(grade) {
happyGrade.append(grade)
}

line of code. It doesnt work as it is supposed to

It also won’t allow me to use generics

func simpleStringFilter3(array: [T], operation: (T) → Bool) → [T] {
var result: [T] =

for element in array {
    if element == "A" {
        result.append(element)
    } else {
        print("\(result) not happy")
    }

@lac_ekim,
did you try

func simpleStringFilter3<T>(array: [T] ...

cheers,

Jayant

This topic was automatically closed after 166 days. New replies are no longer allowed.