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 / .htaccess
Created May 8, 2012 02:34
.htaccess file for cross-origin resource sharing
Header set Access-Control-Allow-Origin *
Header set Access-Control-Allow-Headers "X-Requested-With, Content-Type"
Header set Access-Control-Allow-Methods "PUT, POST, GET, OPTIONS, DELETE"
@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 / amqptest.php
Created November 7, 2010 20:42
A quick example of how to use the PHP AMQP extension from PECL.
<?php
// Create a connection
$cnn = new AMQPConnection(array("host" => "localhost"
,"vhost" => "/"
,"port" => 5672
,"login" => "guest"
,"password" => "guest"));
$cnn->connect();
// Declare a new channel
@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 / 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.