Skip to content

Instantly share code, notes, and snippets.

@prufrock
prufrock / .swift
Last active January 23, 2024 22:01
Combine two arrays in swift into an array of pairs sorted by the first.
extension Array where Element: Comparable {
/// Combine both arrays into pairs and sort the result based on the first element.
/// - Parameter other: The array of elements to combine with.
/// - Returns: An array of pairs.
func aligned<T>(with other: Array<T>) -> [(Element, T)] {
let aligned = zip(self, other)
return aligned.sorted { $0.0 < $1.0 }
}
}
@prufrock
prufrock / sphp.sh
Created January 9, 2023 20:30 — forked from rhukster/sphp.sh
Easy Brew PHP version switching
#!/bin/bash
# Creator: Phil Cook
# Modified: Andy Miller
osx_major_version=$(sw_vers -productVersion | cut -d. -f1)
osx_minor_version=$(sw_vers -productVersion | cut -d. -f2)
osx_patch_version=$(sw_vers -productVersion | cut -d. -f3)
osx_patch_version=${osx_patch_version:-0}
osx_version=$((${osx_major_version} * 10000 + ${osx_minor_version} * 100 + ${osx_patch_version}))
homebrew_path=$(brew --prefix)
brew_prefix=$(brew --prefix | sed 's#/#\\\/#g')
@prufrock
prufrock / Keycodes.swift
Created November 22, 2022 22:14 — forked from swillits/Keycodes.swift
Swift Keyboard Keycodes
struct Keycode {
// Layout-independent Keys
// eg.These key codes are always the same key on all layouts.
static let returnKey : UInt16 = 0x24
static let enter : UInt16 = 0x4C
static let tab : UInt16 = 0x30
static let space : UInt16 = 0x31
static let delete : UInt16 = 0x33
static let escape : UInt16 = 0x35
@prufrock
prufrock / machine.js
Last active August 31, 2021 02:06
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@prufrock
prufrock / machine.js
Created August 29, 2021 02:55
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@prufrock
prufrock / MetalAdder.swift
Last active June 5, 2020 18:28
I converted this Apple Metal tutorial, https://developer.apple.com/documentation/metal/basic_tasks_and_concepts/performing_calculations_on_a_gpu?preferredLanguage=occ, into Swift with some tweaks. This should technicall be "MetalAdder.playground" but GitHub doesn't seem to know what to make of the ".playground" extension. If you want to try it o…
import PlaygroundSupport
import MetalKit
// The length in Apple's example but it takes too long to build 2 buffers of that length
//let arrayLength: Int = 1 << 24
let arrayLength: Int = 1 << 20
let bufferSize = arrayLength * MemoryLayout<Float>.stride
class MetalAdder {
var device: MTLDevice
@prufrock
prufrock / index.php
Last active August 29, 2018 02:51
An HTTP server that prints back your request in PHP. Put this file in a folder, open a terminal in the folder and type: "php -S localhost:979"` and whizz bang pow! You'll have a server you can use to test your requests!
<?php
# Inspired by https://stackoverflow.com/a/49007601
function stdout($arg) {
if (is_object($arg) || is_array($arg) || is_resource($arg)) {
$output = print_r($arg, true);
} else {
$output = (string) $arg;
}
file_put_contents('php://stdout', $output . \PHP_EOL);
@prufrock
prufrock / robossh.sh
Last active August 29, 2015 14:27
Rename this to ssh and put it some place where it will override ssh(but don't delete ssh) and you'll get robo backgrounds in your iterm. A modification of the ImageMagick script at http://kpumuk.info/mac-os-x/how-to-show-ssh-host-name-on-the-iterms-background/
#!/bin/bash
# SSH with host name and IP address in background (only in iTerm.app)
# First, check to see if we have the correct terminal!
if [ "$(tty)" == 'not a tty' ] || [ "$TERM_PROGRAM" != "iTerm.app" ] ; then
/usr/bin/ssh "$@"
exit $?
fi
@prufrock
prufrock / hellolcd.c
Created December 14, 2013 21:48
This is adapted from Arduino's LiquidCrystel HelloWorld Sketch to work the with sainSmart 1602 LCD Keypad. The pins are different then the LCD this sketch was originally made for as this poster pointed out: http://forum.arduino.cc/index.php?topic=96168.0
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
@prufrock
prufrock / .vimrc
Last active December 31, 2015 04:09
My vim configuration
execute pathogen#infect()
set background:dark
syntax on
filetype plugin indent on
set nowrap
set shiftwidth=4
set ts=4
set expandtab
set encoding=utf-8