This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AppDelegate.swift | |
// SwiftUITestApp | |
// | |
// Created by Matt Gallagher on 4/6/24. | |
// Copyright © 2019 Matt Gallagher. All rights reserved. | |
// | |
import Cocoa | |
import SwiftUI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// Layout | |
// | |
// Created by Matt Gallagher on 7/6/19. | |
// Copyright © 2019 Matt Gallagher. All rights reserved. | |
// | |
import SwiftUI |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |