Skip to content

Instantly share code, notes, and snippets.

Greg Titus gregomni

  • The Omni Group
  • Eugene
Block or report user

Report or block gregomni

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@gregomni
gregomni / State.swift
Created Jun 10, 2019
An example of matching up @State values from multiple View trees. SwiftUI probably does something faster/cleaner/nicer, but this is how it could work.
View State.swift
import UIKit
// Just hold a value in a class so that we can reference it.
private class XStateStorage<Value> {
var value: Value
init(value: Value) {
self.value = value
}
}
View mergeSequences.swift
struct SortedMergeSequence<Element: Comparable> : Sequence, IteratorProtocol {
var iterators: [AnyIterator<Element>]
var lastElements: [Element?]
mutating func next() -> Element? {
var smallest: Element? = nil
var smallestIndex : Int? = nil
for index in lastElements.indices {
guard let element = lastElements[index] else { continue }
if (smallest == nil || element < smallest!) {
@gregomni
gregomni / count.swift
Created Apr 20, 2018
Array counts in the type system...
View count.swift
protocol Counted {
associatedtype More : Counted
associatedtype Less : Counted where Self.More.Less == Self
}
struct CountedArray<T, C: Counted> {
var array: [T]
init(array: [T] = []) {
self.array = array
View thing.swift
public protocol Thing {
associatedtype T
func work() throws -> T
}
public struct AnyThing<T> : Thing {
let doWork: () throws -> T
public func work() throws -> T {
return try doWork()
}
View Fallbacks.swift
protocol Fallbackable {
associatedtype Value
static var fallback: Value { get }
}
struct DefaultDict<K: Hashable, V, F: Fallbackable> where V == F.Value {
var dict: [K:V]
subscript(key: K) -> V {
get {
View attributedstringattributes.swift
import Cocoa
enum NSAttributedStringAttribute {
case font(NSFont)
case foregroundColor(NSColor)
// ...
static func getDictionary(_ attributes: [NSAttributedStringAttribute]) -> [NSAttributedStringKey: Any] {
var result: [NSAttributedStringKey: Any] = [:]
View different_style.swift
public func sequence<T>(first: T, next: (T) -> T?, while test: (T) -> Bool) -> UnfoldSequence<T, (T?, Bool)> {
return sequence(state: (first, true), next: { (state: inout (T?, Bool)) -> T? in
switch state {
case (let value?, true):
state.1 = false
if !test(value) {
state.0 = nil; return nil
}
return value
case (let value?, false):
View scalecrop2.swift
extension CGSize {
func scaled(byFactor: CGFloat) -> CGSize {
return CGSize(width: width*byFactor, height: height*byFactor)
}
}
extension UIImage {
static func drawnVia(size: CGSize, opaque: Bool = false, _ scale: CGFloat = 0.0, draw: () -> ()) -> UIImage {
UIGraphicsBeginImageContextWithOptions(size, opaque, scale)
draw()
View scalecrop.swift
extension CGSize {
func scaled(byFactor: CGFloat) -> CGSize {
return CGSize(width: width*byFactor, height: height*byFactor)
}
}
func scaleAndCropImage(image: UIImage, toSize size: CGSize, fitImage: Bool = true) -> UIImage {
guard image.size != size else { return image }
// Calculate scale factor for fit or fill
View Fallbackable.swift
protocol Fallbackable {
associatedtype Value
static var fallback: Value { get }
}
struct DefaultDict<K: Hashable, V, F: Fallbackable where V == F.Value> {
var dict: [K:V]
subscript(key: K) -> V {
get {
You can’t perform that action at this time.