Skip to content

Instantly share code, notes, and snippets.

View LucianoPAlmeida's full-sized avatar
💭
Daniel 11:32

Luciano Almeida LucianoPAlmeida

💭
Daniel 11:32
View GitHub Profile
//
// 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.
struct HTTPResponse {
var statusCode: Int
}
extension HTTPResponse {
func validateStatus<R>(in range: R) -> Bool where R: RangeExpression, R.Bound == Int {
return range~=self.statusCode
}
}
//
// 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> {
@LucianoPAlmeida
LucianoPAlmeida / Pointers.swift
Created February 17, 2019 20:29
Playing around with Swift pointers
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
@_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.
@LucianoPAlmeida
LucianoPAlmeida / AtomicBool.swift
Created March 10, 2019 18:06
AtomicBool abstraction
import Foundation
final class AtomicBool: CustomStringConvertible {
private var _value = _stdlib_AtomicInt(0)
public init(_ value: Bool) {
self._value.store(asInt(value))
}
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.
@LucianoPAlmeida
LucianoPAlmeida / syntax-factory.swift
Last active April 15, 2019 02:25
SwiftSyntax SyntaxFactory
// 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 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)
)
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
}
}