Skip to content

Instantly share code, notes, and snippets.

@thiagoh
Created January 9, 2017 14:25
Show Gist options
  • Save thiagoh/5757dcd4fe250a77c23f8aa12bee6789 to your computer and use it in GitHub Desktop.
Save thiagoh/5757dcd4fe250a77c23f8aa12bee6789 to your computer and use it in GitHub Desktop.
//
// main.swift
// SwiftTests
//
// Created by Thiago Andrade on 2017-01-08.
// Copyright © 2017 Thiago Andrade. All rights reserved.
//
import Foundation
func sum(_ a: Int, _ b: Int, clos: ((Int) -> ())? = nil) {
if let clos = clos {
clos(a + b);
} else {
print("do nothing with ", (a + b));
}
}
func times(_ a: Int, _ b: Int, clos: ((Int) -> ())) {
clos(a * b);
}
print("Closure sum");
sum(1, 2);
sum(1, 2) { v in };
sum(8, 7) { v in
print("printing value with closure callback:", v);
}
times(3, 4) { v in
print("printing value with closure callback:", v);
};
sum(8, 7) { v in
times(v, 2) { v in
print("inner inner closure printing value with closure callback:", v);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment