Skip to content

Instantly share code, notes, and snippets.

View justinmeiners's full-sized avatar

Justin Meiners justinmeiners

View GitHub Profile
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
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
@justinmeiners
justinmeiners / forth.md
Last active February 11, 2022 15:09
Notes on the Forth programming language.
@justinmeiners
justinmeiners / FileManager+RemoveIfExists.swift
Last active January 14, 2022 23:41
iOS swift FileManager remove/delete file only if it exists. "No such file or directory" fix.
// 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
}
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 / jonesforth.f.txt
Last active October 1, 2021 22:14
How to write a Forth compiler. Mirror of Jonesforth (I did not write this.)
\ -*- text -*-
\ A sometimes minimal FORTH compiler and tutorial for Linux / i386 systems. -*- asm -*-
\ By Richard W.M. Jones <rich@annexia.org> http://annexia.org/forth
\ This is PUBLIC DOMAIN (see public domain release statement below).
\ $Id: jonesforth.f,v 1.17 2007/10/12 20:07:44 rich Exp $
\
\ The first part of this tutorial is in jonesforth.S. Get if from http://annexia.org/forth
\
\ PUBLIC DOMAIN ----------------------------------------------------------------------
\
@justinmeiners
justinmeiners / generic_algebra.cpp
Created August 21, 2021 19:39
Implementing free group "free reduce" and a few other operations in a generic way. (C++20 concepts)
#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>>;
; 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)
(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)
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)