Skip to content

Instantly share code, notes, and snippets.

View justinmeiners's full-sized avatar

Justin Meiners justinmeiners

View GitHub Profile
(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)
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
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 {
extension Sequence where Element == Float {
// average is a vector space operation
func average() -> Element {
let one = Float(1.0)
var mean = Element.zero
var n = Float(0.0)
for x in self {
n += one
// this shows the preprocessor you can use to detect whether to try pledge or not
//
#ifdef __OpenBSD__
#define HAS_PLEDGE 1
#endif
#ifdef HAS_PLEDGE
if (pledge("stdio", NULL) == -1)
{
; The official HD AI
; An Artificial Intelligence Script written by Archon and Promiskuitiv
; Get in contact with Promiskuitiv by sending a mail to neuernamae@web.de
; List of taunts it reacts to:
; Standard taunts.
; 33 - Stop slinging resources. If slinging is requested early and is immediately canceled it may mess up the strategy.
; 38 - Sling Resources. Human player only, stops any unit production except for civilian units.
func foldBinary<T>(_ list: [T], operation: (T, T) -> T) -> T? {
if let initial = list.first {
return list.dropFirst().reduce(initial, operation)
} else {
return nil
}
}
; https://en.wikiversity.org/wiki/Binomial_coefficients
(defun arrangements (n k)
(if (= n 0)
(list nil)
(append
(when (< k n)
(mapcar (lambda (tail)
(cons 0 tail))
(arrangements (- n 1) k)))
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
template<typename I>
std::pair< typename std::iterator_traits<I>::value_type, size_t> mode(I start, I end) {
typedef typename std::iterator_traits<I>::value_type V;
if (start == end) {