This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.swift | |
// InterspesedSequence | |
// | |
// Created by Luciano Almeida on 19/01/19. | |
// Copyright © 2019 Luciano Almeida. All rights reserved. | |
// | |
/// A lazy view for sequence that creates a new sequece where for each iteration of the base sequence the value is inserted. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct HTTPResponse { | |
var statusCode: Int | |
} | |
extension HTTPResponse { | |
func validateStatus<R>(in range: R) -> Bool where R: RangeExpression, R.Bound == Int { | |
return range~=self.statusCode | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RotatedCollection.swift | |
// Collections | |
// | |
// Created by Luciano Almeida on 10/02/19. | |
// Copyright © 2019 Luciano Almeida. All rights reserved. | |
// | |
@_fixed_layout | |
public struct RotatedCollection<Base: Collection> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct P { | |
var b: Bool | |
var i: Int | |
} | |
MemoryLayout<P>.size // returns 9 | |
MemoryLayout<P>.alignment // returns 8 | |
MemoryLayout<P>.stride // returns 16 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@_fixed_layout | |
public struct RotatedCollection<Base: Collection> { | |
@usableFromInline | |
internal let _base: Base | |
@usableFromInline | |
internal let _offset: Int | |
@usableFromInline | |
internal let _computedOffset: Int | |
/// Complexity: O(1) only when `Base` conforms to ramdom access collection. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class AtomicBool: CustomStringConvertible { | |
private var _value = _stdlib_AtomicInt(0) | |
public init(_ value: Bool) { | |
self._value.store(asInt(value)) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [1, 2, 3, 4, 5] | |
let r1: [Int] = array.reversed() // Eager reverse implementation is O(n) when call the function | |
let r2: ReversedCollection<[Int]> = array.reversed() // Lazy reversed collection is O(1) when you call the method. | |
r1.first { $0 == 3 } // Interate 3 times + O(n) complexity of eager call. | |
r2.first { $0 == 3 } // Interate 3 times + O(1) of lazy call. For a large array, this is an example where using a lazy type is more performant than an eager one. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example of creating a Any keyword. | |
SyntaxFactory.makeAnyKeyword() | |
// Example of creating a declaration modifier with a details also create with SyntaxFactory. | |
SyntaxFactory.makeBlankDeclModifier().withDetail( | |
SyntaxFactory.makeUnknown("").withLeadingTrivia(trivia) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This will create a new DeclModifierSyntax with the same information that | |
// makeBlankDeclModifier() but with the detail property equals detail. | |
let detail = SyntaxFactory.makeUnknown("").withLeadingTrivia(trivia) | |
// This will create a new TokenSyntax with the same information that | |
// makeUnknown("") but with the LeadingTrivia property equals trivia. | |
SyntaxFactory.makeBlankDeclModifier().withDetail( | |
SyntaxFactory.makeUnknown("").withLeadingTrivia(trivia) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftSyntax | |
class FindPublicExtensionDeclVisitor: SyntaxVisitor { | |
override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind { | |
if node.modifiers?.contains(where: { $0.name.tokenKind == .publicKeyword }) == true { | |
// Do something if you find a `public extension` declaration. | |
} | |
return SyntaxVisitorContinueKind.skipChildren | |
} | |
} |