Skip to content

Instantly share code, notes, and snippets.

Avatar

Pyry Jahkola pyrtsa

View GitHub Profile
@pyrtsa
pyrtsa / format_string.cpp
Created Jan 23, 2017
Simple fprintf-style std::string formatting in C++
View format_string.cpp
#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 Sep 4, 2016
Looking at the memory layout of Array and AnyIterator in Swift
View MemoryLayouts.swift
import Foundation
struct P : CustomStringConvertible {
var p: OpaquePointer?
init(_ p: OpaquePointer?) {
self.p = p
}
var description: String {
@pyrtsa
pyrtsa / throttle-SourceKitService
Created Jul 26, 2016
Script to keep SourceKitService from eating up all OS resources
View throttle-SourceKitService
#!/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
AnyEquatable—making Equatable work with class inheritance and existential wrapping ("type erasure")
View AnyEquatable.swift
// 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 Feb 14, 2016
Rounding and representing as string quantities with uncertainty in MATLAB.
View approx.m
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 Dec 13, 2016
Make Bash report the start times all commands, and total durations of long-running ones.
View .prompt-time
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 Dec 13, 2015
"Equatable Any"
View Eq.swift
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 Aug 2, 2018
.ghci — Essential parts of my Haskell REPL config.
View .ghci
-- 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 Nov 12, 2015
"Throwing subscripts" in Swift 2
View ThrowingSubscripts.swift
infix operator « { associativity left }
infix operator »« { associativity left }
postfix operator » {}
struct Index {
let value: Int
}
enum Error : ErrorType {
case OutOfBounds(index: Int, count: Int)
View keybase.md

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:

You can’t perform that action at this time.