UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Writing a function that returns a function

Forums > Swift

Hi all,

I want to write a function that takes a string and returns a function that takes and returns a double. The string that this function takes should be a formula (for example "y=ax+b") and the function it returns should be the function that calculates y. I have written some code, but I need some help to make it work. Could someone help me?

3      

Oops, forgot it, this is the function:

func getFunction(from formula: String) -> (Double) -> Double {
        var result = { x in
            0.0 * x
        }

        for character in formula.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "y=", with: "") {

        }

        return result
    }

3      

@casj_swift, if the function only takes in one Double (for x, presumably) and returns a Double (which would be y), where do the other values in y = ax+b come from?

3      

@roosterboy, you have a point there. That should be an array:

func getFunction(from formula: String) -> ([Double]) -> Double {
        var result = { x in
            0.0 * x
        }

        for character in formula.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "y=", with: "") {

        }

        return result
    }

Now my question is: what should I do in the for-in loop? I want to add a part of the calculation to the result for each character in the formula, but how should I do that?

3      

Cas wishes to calculate some maths formulae...

I want to write a function that takes a string and returns a function.
The string that this function takes should be a formula (for example "y=ax+b")

There's another Paul who teaches Swift and XCode development: Paul Hagerty of Stanford University.

For a few semesters he developed a RPN calculator during his lectures. (The lectures may be old, but the logic is still sound!) His logic follows the keystrokes as a user enters a formula into the calculator's keyboard. This sounds similar to what you wish to accomplish.

His logic describes how he captures unary maths operations, such as taking the negative of a number, or squaring a number. He also describes binary operations that take two double values and one operand such as a plus sign, or a divide sign. His logic pushes numbers (10, 42, 3.14, etc) into a stack and then operates on the stack when an operator is encountered (+, -, ^, etc).

You may want to watch his series, and also explore the CalculatorBrain.swift logic that some students have posted to github. I have provided one such link below. Operand order is important! You need a clever data structure to parse a string version of a formula from left to right.

This hint may, or may not, help you think through your logic problem.

Good luck!

Keep coding!

Watch the lectures! This will help you grok the code.
See -> CS 193p Calculator Code

3      

Roosterboy and Obelix, thank you for your advice! I'll watch the lectures and I'll see how I may use CalculatorBrain.swift.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.