Skip to content

Instantly share code, notes, and snippets.

Avatar

Justin Meiners justinmeiners

View GitHub Profile
View intersperse.swift
struct InterspersedSequence<S: Sequence>: Sequence, IteratorProtocol {
var separator: S.Element
var iterator: S.Iterator
enum State {
case separator
case element(nextElement: S.Element?)
}
var state: State
View multiplicity.swift
enum CollectionMultiplicity<C: Collection> {
case none
case one(first: C.Index)
case many(first: C.Index, second: C.Index)
}
extension Collection {
func indexedMultiplicity(where predicate: (Element) -> Bool) -> CollectionMultiplicity<Self> {
guard let i = firstIndex(where: predicate) else {
return .none
@justinmeiners
justinmeiners / FileManager+RemoveIfExists.swift
Last active Jan 14, 2022
iOS swift FileManager remove/delete file only if it exists. "No such file or directory" fix.
View FileManager+RemoveIfExists.swift
// Most implementations first check if the file exists, and then do the remove.
// This is subject to race conditions and requires accessing the file system twice.
// A better solution is to ignore that particular exception.
extension FileManager {
func removeItemIfExists(at url: URL) throws {
func isDoesNotExist(error: NSError) -> Bool {
return error.domain == NSPOSIXErrorDomain && error.code == ENOENT
}
View scene.glsl
float g_epsilon = 0.002;
float g_max = 50.0;
float sdSphere( vec3 toCenter, float s )
{
return length(toCenter)-s;
}
float sdBox( vec3 p, vec3 b )
{
@justinmeiners
justinmeiners / generic_algebra.cpp
Created Aug 21, 2021
Implementing free group "free reduce" and a few other operations in a generic way. (C++20 concepts)
View generic_algebra.cpp
#include <iostream>
#include <iterator>
// With C++20 concepts
#include <concepts>
template<typename F, typename T>
concept unary_operation = std::invocable<F, T>
&& std::same_as<T, std::invoke_result_t<F, T>>;
View sort.cl
; From Page 149 of "LISP" 1st edition.
; This code has been translated to Common Lisp
; from the dialect used in the book:
; table -> *table*
; (store (table n) x) -> (setf (aref table n) x)
; (boole 1 ...) -> (boole boole-and ...)
; greaterp -> >
; lessp -> <
; (quotient x y) -> (floor x y)
View binary_counter.cl
(defun add-to-counter (counter carry zero op &key (test #'eq))
(dotimes (i (length counter))
(if (funcall test (aref counter i) zero)
(progn (setf (aref counter i) carry)
(setf carry zero))
(progn
(setf carry (funcall op (aref counter i) carry))
(setf (aref counter i) zero))
))
carry)
View cat.swift
import Darwin
enum CatError: Error {
case open(path: String)
}
func parsePosixCommands(_ args: [String]) -> ([String], [String]) {
let endOfOptions = args.firstIndex { $0 == "--" } ?? args.count
let certainArguments = args.suffix(from: endOfOptions)
var toConsider = args.prefix(upTo: endOfOptions)
View mean.swift
static func mean<S: Sequence, T>(
of sequence: S,
oneScalar: T,
scale: (T, S.Element) -> S.Element
) -> S.Element? where S.Element: AdditiveArithmetic, T: FloatingPoint {
var it = sequence.makeIterator()
guard let initial = it.next() else {
return nil
View binary_counter.swift
struct BinaryCounter {
private init() {}
static func add<C: MutableCollection>(
counter: inout C,
x: C.Element,
zero: C.Element,
operation op: (C.Element, C.Element) -> C.Element
) -> C.Element where C.Element: Equatable {