<commit_before>Reducing for-loops is a great way to maintain readability and maintain code modularity. Methods like map, flatMap, filter and reduce are awesome building blocks for **functional programing in swift**, but you can also build your own custom methods that do similar things. <!--more--> Here is a trick were we use closure blocks to encapsulate the method call. The for-loop is the same but the method call inside the loop is what you want it to be. This approach is a great way to avoid multiple for loops that do fairly the same thing. All you do is pass the for-loop the method you want it to perform. In this example we pass it **2 different methods** "isActive" and "isFocused": 

```swift
import Foundation

class Brick{
    var active:Bool
    var focused:Bool
    var name:String
    init(_ active:Bool,_ focused:Bool, _ name:String){
        self.active = active
        self.focused = focused
        self.name = name
    }
    func isActive()->Bool{
        return active
    }
    func isFocused()->Bool{
        return focused
    }
}
private class Utils{
    /**
     *
     */
    static func performAction(bricks:Array<Brick>, _ action:(Brick)->Bool)->Brick?{
        for brick in bricks{
            if(action(brick)){
				return brick
			}
        }
        return nil
    }
}

let a:Brick = Brick(true,false,"a")
let b:Brick = Brick(false,true,"b")
let c:Brick = Brick(true,true,"c")
let bricks = [a,b,c]

print(Utils.performAction(bricks, {$0.isActive()})!.name)//a
print(Utils.performAction(bricks, {$0.isFocused()})!.name)//b
```<commit_msg>Files modified: 1<commit_after>Reducing for-loops is a great way to maintain readability and maintain code modularity. Methods like map, flatMap, filter and reduce are awesome building blocks for **functional programing in swift**, but you can also build your own custom methods that do similar things. <!--more--> Here is a trick were we use closure blocks to encapsulate the method call. The for-loop is the same but the method call inside the loop is what you want it to be. This approach is a great way to avoid multiple for loops that do fairly the same thing. All you do is pass the for-loop the method you want it to perform. In this example we pass it **2 different methods** "action1" and "action2": 

```swift
import Foundation

class Brick{
    var active:Bool
    var focused:Bool
    var name:String
    init(_ active:Bool,_ focused:Bool, _ name:String){
        self.active = active
        self.focused = focused
        self.name = name
    }
    func isActive()->Bool{
        return active
    }
    func isFocused()->Bool{
        return focused
    }
}
private class Utils{
    /**
     *
     */
    static func performAction(bricks:Array<Brick>, _ action:(Brick)->Bool)->Brick?{
        for brick in bricks{
            if(action(brick)){
				return brick
			}
        }
        return nil
    }
}

let a:Brick = Brick(true,false,"a")
let b:Brick = Brick(false,true,"b")
let c:Brick = Brick(true,true,"c")
let bricks = [a,b,c]

let action1:(Brick)->Bool = {$0.isActive()}
let action2:(Brick)->Bool = {$0.isFocused()}

print(Utils.performAction(bricks, action1)!.name)//a
print(Utils.performAction(bricks, action2)!.name)//b
```
{'fraction_non_alphanumeric': 0.10700992555831265, 'fraction_numerical': 0.004032258064516129, 'mean_word_length': 3.188311688311688, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3211312', 'n_tokens_mistral': 977, 'n_tokens_neox': 935, 'n_words': 368}