Adding forEach: The Power of SWIFT Closures

  • By admin
  • Published on 10 September 2015

Introduction to Closures

Closures are definitely not a new invention, many languages have had them in the past, and even Javascript has closures. However, the implementation of closures as a first class language construct is certainly very elegant in SWIFT.

Have a scoped unit of code, which can be passed in, returned, operated upon is a necessity when working on event oriented applications - which is essentially every modern Javascript or Mobile application.

In the past I have been forced to simulate closures in Java using a single function interface, where the singular function is essentially a closure. Example:

 

public interface IFunctionPtr {
	public void execute(T t);
}

 

which can then be passed around as a simulated closure, such as:

 

var closure = new IFunctionPtr() {
	@Override
	public void execute(String s) {
		doSomething(s);
	}
}

objectThatAcceptsClosure.closureExecutor(closure);

 

Closures in SWIFT

However, the closure implementation in SWIFT is (a) A first class language construct, (b) Complete. So no such hacks are required.

A really neat example of the power of SWIFT closures is to implement a forEach operation in SWIFT. Many languages have forEach as an available operation for Arrays or Collections. SWIFT doesn’t, though it does have for in { ... }, which is close, but not exactly as succinct.

And while we are at it, wouldn’t it be nice if it could deal with optional collection objects, for example if the input is nil, just don’t do anything instead of throwing a memory access error (yes I agree this may or may not be a good idea).

A typical forEach syntax is:

 

forEach() { do something with each element }

 

As it is obvious, basically the { do something with each element } is nothing but a closure operation to be performed on each element. SWIFT supports closures, so this shouldn’t be tough right? Wouldn’t be it great to have it a global language operation which can work on any collection?

forEach in SWIFT

Here’s the solution, it is powerful, it is elegant, and it is very much demonstrative of the power of closures in SWIFT:

 

 

func forEach(array: T?, op: (V) -> Void) {
    if let opArray: T = array {
        for member: V in opArray { op(member)}
    }
}

 

That’s it. Add this to a file - I used the name LanguageExtensions.swift and now you have a full forEach operation available in SWIFT.

Here is a usage example:

 

forEach(myDictionaryObject.keys) { $0.doSomething() }

 

Nice, powerful, elegant.

For information about TekMonks iOS Enterprise App development services, contact us at [email protected], or fill out the form on the right to contact us.

Full Code LanguageExtensions.swift

 

//
//  LanguageExtensions.swift
//
//  Some SWIFT langauage extensions to make life easier
//
//  Created by TekMonks on 10/1/15.
//  Copyright © 2015 TekMonks. All rights reserved.
//
//  This code can be used without any restrictions as long as 
//  this Copyright banner is maintained.
//

import Foundation

func forEach(array: T?, op: (V) -> Void) {
    if let opArray: T = array {
        for member: V in opArray { op(member)}
    }
}