Skip to content

Instantly share code, notes, and snippets.

View gregomni's full-sized avatar

Greg Titus gregomni

  • The Omni Group
  • Eugene
View GitHub Profile
@gregomni
gregomni / main.swift
Created July 21, 2023 23:31
Banana-only Scrabble, part 3
//
// main.swift
// banana
//
// Created by Greg Titus on 7/15/23.
//
import Foundation
import Cocoa
@gregomni
gregomni / gist:9d65d4cdb14e40810022f166382213d6
Created July 16, 2023 16:51
Banana-only Scrabble, part 2, a little less slow and naive
//
// main.swift
// banana
//
// Created by Greg Titus on 7/15/23.
//
import Foundation
import Cocoa
@gregomni
gregomni / gist:5a82662cff008a212a3dcd7ba2bf7564
Created July 15, 2023 22:00
Banana-only Scrabble, naive and slow (for now)
//
// main.swift
// banana
//
// Created by Greg Titus on 7/15/23.
//
import Foundation
enum Letter: Character, Hashable {
// Returns a set of instructions in the form of an array of closed ranges,
// where you should swap the element at the lowerBound of the range continually forward until next to the upperBound,
// and then cancel.
func shortenList<T>(inputList: [T], cancellable: (T, T) -> Bool, swappable: (T, T) -> Bool) -> [ClosedRange<Int>] {
var list = inputList
var result: [ClosedRange<Int>] = []
while list.count > 1 {
var cancelRange: ClosedRange<Int>?
extension Range where Bound: Strideable {
func stride(by: Bound.Stride) -> StrideTo<Bound> {
Swift.stride(from: lowerBound, to: upperBound, by: by)
}
}
extension ClosedRange where Bound: Strideable {
func stride(by: Bound.Stride) -> StrideThrough<Bound> {
Swift.stride(from: lowerBound, through: upperBound, by: by)
}
}
struct LazyChunkedOn<Base: Collection, Subject: Equatable> : Collection {
typealias Element = Base.SubSequence
var underlying: Base
var projection: (Base.Element) -> Subject
struct Index: Comparable {
let start: Base.Index
var subject: Subject?
static func == (lhs: Index, rhs: Index) -> Bool { lhs.start == rhs.start }
@gregomni
gregomni / State.swift
Created June 10, 2019 18:09
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.
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
}
}
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 April 20, 2018 18:08
Array counts in the type system...
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
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()
}