Skip to content

Instantly share code, notes, and snippets.

@therealbnut
therealbnut / .gitconfig
Created August 27, 2015 08:48
Swaps the last two commits, perhaps, who knows? Have fun!
[alias]
swap-last-two-commits = "!f() {\n\
set -eu \n\
if [ ! -z \"`git diff HEAD`\" ]; then \n\
echo \"Found uncommited changes... exiting.\" 1>&2 \n\
exit 1 \n\
fi \n\
HEAD_0=\"$(git rev-parse --verify HEAD)\" \n\
HEAD_1=\"$(git rev-parse --verify HEAD~1)\" \n\
echo \"swapping ${HEAD_0} and ${HEAD_1}\" \n\
@therealbnut
therealbnut / crash.swift
Created June 17, 2015 13:12
Assertion failed: (fn->getLoweredFunctionType() == type), function getOrCreateFunction, file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-700.0.38.1/src/swift/lib/SIL/SILModule.cpp, line 231.
import Nimble
import Quick
public func errorOf<T>(expression: () throws -> T?) -> ErrorType? {
do {
try expression()
}
catch {
return error
}
@therealbnut
therealbnut / NSArray+MergeSorted.h
Created May 31, 2012 04:10
An NSArray category to merge multiple sorted NSArrays into one sorted NSArray (not arc compatible, yet?)
//
// NSArray+MergeSorted.h
// NSArray Merge Sorted Category
//
// Created by Andrew Bennett on 31/05/12.
// Copyright (c) 2012 Andrew Bennett. All rights reserved.
//
#import <Foundation/Foundation.h>
public struct SourceLocation: CustomDebugStringConvertible {
init(file: String = __FILE__, line: Int = __LINE__, column: Int = __COLUMN__, function: String = __FUNCTION__) {
self.file = file
self.line = line
self.column = column
self.function = function
}
public let file: String
public let line: Int
@therealbnut
therealbnut / hackerrank-encryption.swift
Created February 20, 2016 15:06
HackerRank Encryption
guard let response = readLine(stripNewline: true) else {
fatalError("Expecting input!")
}
let withoutSpaces = response.characters.filter { $0 != " " }
let length = withoutSpaces.count
let minRows = Int(floor(sqrt(Double(length))))
let columns = minRows * minRows >= length ? minRows : minRows + 1
var output = ""
#!/usr/bin/env bash
set -eu
removeLines() {
for i in $(seq ${1}); do
echo -en "\r\033[A\033[1K"
done
}
echo -e "first line\nmuch longer second line - enough to wrap maybe"
// Output:
// AnyPokemon<🔥>(Ponyta())
// AnyPokemon<🔥>(Charmander())
protocol Pokemon {
associatedtype Power
mutating func attack() -> Power
}
struct Pikachu: Pokemon {
// Using XCode 7.3.1 (it's all I had at the time)
extension MutableCollectionType where Generator.Element: Comparable {
mutating func selectionSortInPlace() {
for currentIndex in self.startIndex ..< self.endIndex {
guard let (minIndex, minElement) = zip((currentIndex ..< self.endIndex), self.suffixFrom(currentIndex))
.minElement({ lhs, rhs in (lhs.1 as! Generator.Element) < (rhs.1 as! Generator.Element) })
else {
continue
}
// Using XCode 7.3.1 (it's all I had at the time)
extension MutableCollectionType where Generator.Element == SubSequence.Generator.Element {
mutating func selectionSort(isOrderedBefore compare: (Generator.Element, Generator.Element) throws -> Bool) rethrows {
for currentIndex in self.startIndex ..< self.endIndex {
// (index,element) pairs after the current index
let pairs = zip(currentIndex ..< self.endIndex, self.suffixFrom(currentIndex)).lazy
// the pair with the minimum element
let pair = try pairs.minElement({ try compare($0.1, $1.1) })
@therealbnut
therealbnut / volumeUUIDForPath.swift
Created April 24, 2017 02:02 — forked from adgray/volumeUUIDForPath.swift
Get Volume UUID for Path
import DiskArbitration
extension URL {
var volumeUUID: String? {
guard self.isFileURL, let session = DASessionCreate(nil) else {
return nil
}
var fsStats = statfs()
guard statfs(self.path, &fsStats) == 0,