Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / format_string.cpp
Created January 23, 2017 13:19
Simple fprintf-style std::string formatting in C++
#include <string>
#include <cstdio>
#include <vector>
#include <iostream>
[[gnu::format(printf, 1, 2)]]
auto format(const char * fmt, ...) -> std::string {
va_list args1;
va_start(args1, fmt);
va_list args2;
@pyrtsa
pyrtsa / MemoryLayouts.swift
Created September 4, 2016 06:42
Looking at the memory layout of Array and AnyIterator in Swift
import Foundation
struct P : CustomStringConvertible {
var p: OpaquePointer?
init(_ p: OpaquePointer?) {
self.p = p
}
var description: String {
@pyrtsa
pyrtsa / throttle-SourceKitService
Created July 26, 2016 07:46
Script to keep SourceKitService from eating up all OS resources
#!/bin/bash
limit="${1-10000000}";
echo "Keeping SourceKitService below $limit KiB of virtual memory."
echo "Hit ^C to quit."
while true; do
sleep 1;
p=`pgrep ^SourceKitService$`
if [ -n "$p" ]; then
vsz=`ps -o vsz -p "$p" | tail -1`
@pyrtsa
pyrtsa / AnyEquatable.swift
Created May 25, 2016 10:27
AnyEquatable—making Equatable work with class inheritance and existential wrapping ("type erasure")
// Toggle this boolean to compare against stdlib:
#if true // Stdlib version
// Quick hack to avoid changing the AnyEquatable implementation below.
extension Equatable { typealias EqualSelf = Self }
#else // Modified version
protocol Equatable {
@pyrtsa
pyrtsa / approx.m
Created February 14, 2016 12:43
Rounding and representing as string quantities with uncertainty in MATLAB.
function y = approx(x, n)
% APPROX Round the number to 1+n significant digits.
% y = APPROX(x) shorthand for round(x, 4, 'significant').
% y = APPROX(x, n) shorthand for round(x, 1 + n, 'significant').
% Copyright 2016 Pyry Jahkola
if nargin < 2
n = 3;
end
@pyrtsa
pyrtsa / .prompt-time
Last active February 12, 2023 05:12
Make Bash report the start times all commands, and total durations of long-running ones.
export _ps1_timer
function _ps1_timer_start {
# echo START
_ps1_timer=${_ps1_timer:-`gdate +%s%N 2> /dev/null || date +%s%N`};
}
function _ps1_timer_stop {
# echo STOP
if [ -z "$_ps1_timer" ]; then
@pyrtsa
pyrtsa / Eq.swift
Created December 13, 2015 15:26
"Equatable Any"
struct Eq : Equatable {
let unbox: Any
let equal: Any -> Bool
init(value: Any, equal: Any -> Bool) {
self.unbox = value
self.equal = equal
}
init<T : Equatable>(_ value: T) {
self.init(value: value) { any in
guard let other = any as? T else { return false }
@pyrtsa
pyrtsa / .ghci
Last active August 2, 2018 15:43
.ghci — Essential parts of my Haskell REPL config.
-- Show loaded modules in window title and use a green "λ>" as prompt.
-- Subsequent lines of multi-line commands shall begin with " |".
:set prompt "\SOH\ESC]0;GHCi: %s\BEL\ESC[32;1m\STXλ>\SOH\ESC[0m\STX "
:set prompt2 "\SOH\ESC[32;1m\STX |\SOH\ESC[0m\STX "
-- Paste and evaluate text from the OS X clipboard. (The pasted text also
-- prints in yellow unless pasting quietly using :paste-quiet.)
:set -package process
:def paste \_ -> do { paste <- System.Process.readProcess "pbpaste" [] ""; let cmd = if '\n' `elem` paste then ":{\ntype Ö = ()\n" ++ paste ++ "\n:}" else paste in putStrLn ("\SOH\ESC[33m\STX" ++ paste ++ "\SOH\ESC[0m\STX") >> return (":cmd return " ++ show cmd) }
:def paste-quiet \_ -> do { paste <- System.Process.readProcess "pbpaste" [] ""; let cmd = if '\n' `elem` paste then ":{\ntype Ö = ()\n" ++ paste ++ "\n:}" else paste in return (":cmd return " ++ show cmd) }
@pyrtsa
pyrtsa / ThrowingSubscripts.swift
Created November 12, 2015 17:19
"Throwing subscripts" in Swift 2
infix operator « { associativity left }
infix operator »« { associativity left }
postfix operator » {}
struct Index {
let value: Int
}
enum Error : ErrorType {
case OutOfBounds(index: Int, count: Int)

Keybase proof

I hereby claim:

  • I am pyrtsa on github.
  • I am pyrtsa (https://keybase.io/pyrtsa) on keybase.
  • I have a public key whose fingerprint is 9215 CD40 D8F3 0BCA 0428 64DE D8D8 B2C7 FEBC 8EE6

To claim this, I am signing this object: