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
@LucianoPAlmeida
LucianoPAlmeida / GrayScale.swift
Created June 19, 2019 03:42
Playing with CIColorControls filter
func applyGrayScaleFilter(image: UIImage?) -> UIImage? {
guard let ciImage = image?.ciImage else { return nil }
let filter = CIFilter(name: "CIColorControls",
parameters: ["inputImage" : ciImage,
"inputContrast": NSNumber(1.0),
"inputSaturation": NSNumber(0.0),
"inputBrightness": NSNumber(0.0)])
guard let out = filter?.outputImage else { return nil }
return UIImage(ciImage: out)
}
func downsampledImage(at url: URL, to size: CGSize, scale: CGFloat) -> UIImage? {
guard let imageResource = CGImageSourceCreateWithURL(url as CFURL, [kCGImageSourceShouldCache: false] as CFDictionary) else { return nil }
let maxDimensionPixels = max(size.height, size.width) * scale
let options = [
kCGImageSourceShouldCacheImmediately: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceThumbnailMaxPixelSize: maxDimensionPixels
] as CFDictionary
import Foundation
struct Car {
var brand: String?
func drive() {}
}
import SwiftSyntax
class CatRewriter: SyntaxRewriter {
override func visit(_ token: TokenSyntax) -> Syntax {
guard case .stringLiteral = token.tokenKind else { return token }
return token.withKind(.stringLiteral("\"🐱\""))
}
}
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
}
}
// 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)
)
@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)
)
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 / 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))
}
@_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.