Skip to content

Instantly share code, notes, and snippets.

Avatar

Matt Gallagher mattgallagher

View GitHub Profile
@mattgallagher
mattgallagher / AppDelegate.swift
Last active Mar 5, 2020
Animated circle views in SwiftUI and AppKit/CoreAnimation
View AppDelegate.swift
//
// AppDelegate.swift
// SwiftUITestApp
//
// Created by Matt Gallagher on 4/6/24.
// Copyright © 2019 Matt Gallagher. All rights reserved.
//
import Cocoa
import SwiftUI
View ContentView.swift
//
// ContentView.swift
// Layout
//
// Created by Matt Gallagher on 7/6/19.
// Copyright © 2019 Matt Gallagher. All rights reserved.
//
import SwiftUI
View zeroAllocNearestCommonAncestor.swift
public extension UIView {
public func nearestCommonAncestor(with otherView: UIView) -> UIView? {
let myHierarchyDepth = sequence(first: self, next: { $0.superview }).reduce(0) { (count, view) in count + 1 }
let otherHierarchyDepth = sequence(first: otherView, next: { $0.superview }).reduce(0) { (count, view) in count + 1 }
var longest = sequence(first: myHierarchyDepth < otherHierarchyDepth ? otherView : self, next: { $0.superview })
var shortest = sequence(first: myHierarchyDepth < otherHierarchyDepth ? self : otherView, next: { $0.superview })
return zip(
longest.dropFirst(abs(myHierarchyDepth - otherHierarchyDepth)),
shortest
).first(where: { $0.0 === $0.1 })?.0
View nca.swift
public extension UIView {
// Return nearest common ancestor between two views
public func nearestCommonAncestor(with otherView: UIView) -> UIView? {
var mySuperviews = sequence(first: self, next: { $0.superview }).reversed().makeIterator()
var theirSuperviews = sequence(first: otherView, next: { $0.superview }).reversed().makeIterator()
var nca: UIView? = nil
while let mine = mySuperviews.next(), let theirs = theirSuperviews.next(), mine === theirs {
nca = mine
}
return nca
@mattgallagher
mattgallagher / ViewModel-ViewBinding.swift
Last active Mar 26, 2017
An abstract look at the View-Model/View-Binding pattern I frequently use in my projects and why this pattern uses the Swift `private` keyword.
View ViewModel-ViewBinding.swift
// This file is an abstract representation of code that I use extensively in my apps
// for constructing and maintaining views. The reason the `private` keyword is used here
// is to control the interface between two entities (a function and a class) which typically
// reside in the same file.
//
// The function and the class implement a View-Binding and a View-Model. The two are largely
// representations of the *same* concepts – the latter from data perspective and the former
// from a view-infrastructure perspective. Their inter-relatedness makes it highly desirable to
// place them both in the same file – they may share many small types between them and they
// are perpetually co-evolving.
View CwlSelectionSortAbomination.swift
import Swift
let a = [5,2,7,2,8,8,10,0]
func selectionSort<S: Sequence where S.Iterator.Element: Comparable>( _ input: S) -> [S.Iterator.Element] {
return sequence(first: (remaining: Array(input.enumerated()), smallest: nil as S.Iterator.Element?)) { tuple in
tuple.remaining.min { left, right in
left.element < right.element
}.map { (pair: (offset: Int, element: S.Iterator.Element)) in
(tuple.remaining.filter { $0.offset != pair.offset }, pair.element)
You can’t perform that action at this time.