.../Source/Extensions/Int+doTimes.swift
Line | Count | Source |
1 | | // |
2 | | // Int+doTimes.swift |
3 | | // CutBox |
4 | | // |
5 | | // Created by Jason Milkins on 22/8/23. |
6 | | // Copyright © 2023 ocodo. All rights reserved. |
7 | | // |
8 | | |
9 | | import Foundation |
10 | | |
11 | | extension Int { |
12 | | /// Loop equivalent to Ruby's `int.times {|count| code }` |
13 | | /// Usage: `doTimes { code with $0 }` |
14 | 4 | func doTimes(_ code: (Int) -> Void) { |
15 | 4 | guard self > 0 else { |
16 | 1 | return |
17 | 3 | } |
18 | 1.12k | for count in 0..<self { |
19 | 1.12k | code(count) |
20 | 1.12k | } |
21 | 3 | } |
22 | | |
23 | | /// Loop equivalent to Ruby's `int.times { code }` |
24 | | /// Usage: `doTimes { code }` |
25 | 3 | func doTimes(_ code: () -> Void) { |
26 | 3 | guard self > 0 else { |
27 | 1 | return |
28 | 2 | } |
29 | 111 | for _ in 0..<self { |
30 | 111 | code() |
31 | 111 | } |
32 | 2 | } |
33 | | } |