Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View mattgallagher's full-sized avatar

Matt Gallagher mattgallagher

View GitHub Profile
@mattgallagher
mattgallagher / AppDelegate.swift
Last active May 18, 2022 17:42
Animated circle views in SwiftUI and AppKit/CoreAnimation
//
// AppDelegate.swift
// SwiftUITestApp
//
// Created by Matt Gallagher on 4/6/24.
// Copyright © 2019 Matt Gallagher. All rights reserved.
//
import Cocoa
import SwiftUI
//
// ContentView.swift
// Layout
//
// Created by Matt Gallagher on 7/6/19.
// Copyright © 2019 Matt Gallagher. All rights reserved.
//
import SwiftUI
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
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 March 26, 2017 10:56
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.
// 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.
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)