Last active
August 6, 2016 22:13
-
-
Save satorusasozaki/80548f04ce4c3ff28f207fdefc929142 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// take_function_return_function | |
// | |
// Created by Satoru Sasozaki on 8/6/16. | |
// Copyright © 2016 Satoru Sasozaki. All rights reserved. | |
// | |
// Write a function which receives a function as its argument and returns a new function which uses the passed function. | |
// makeNewFunction receives a function with type (Int, Int) -> Int and returns a new function which | |
// increments the result of the function passed into makeNewFunction and returns it. | |
import Foundation | |
func add(first :Int, second: Int) -> Int{ | |
return first + second | |
} | |
func makeNewFunction(function: (Int, Int) -> Int) -> ((Int, Int) -> Int) { | |
func increment(x: Int, y: Int) -> Int { | |
return function(x,y) + 1 | |
} | |
return increment | |
} | |
print(add(1, second: 2)) // should return 3 | |
let addAndIncrement = makeNewFunction(add) | |
print(addAndIncrement(1,2)) // should return 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment