Skip to content

Instantly share code, notes, and snippets.

@satorusasozaki
Last active August 6, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satorusasozaki/80548f04ce4c3ff28f207fdefc929142 to your computer and use it in GitHub Desktop.
Save satorusasozaki/80548f04ce4c3ff28f207fdefc929142 to your computer and use it in GitHub Desktop.
//
// 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