Skip to content

Instantly share code, notes, and snippets.

View jtbandes's full-sized avatar
💜
Working on @foxglove!

Jacob Bandes-Storch jtbandes

💜
Working on @foxglove!
View GitHub Profile
func bytes<T: UnsignedInteger>(val: T) -> GeneratorOf<UInt8> {
var current = val as UInt64
var step = 0
return GeneratorOf<UInt8> {
if step >= sizeof(T) { return nil }
current >>= 8; step++
return UInt8(current & UInt64(UInt8.max))
}
}
func sift<T, S: Sequence where S.GeneratorType.Element == Optional<T>>(xs: S) -> GeneratorOf<T> {
var gen = xs.generate()
return GeneratorOf<T> {
var next: T??
do {
next = gen.next()
if !next { return nil }
} while !(next!)
return next!
}
// The free monoid
protocol Free {
class var ε: Self { get }
func +(lhs: Self, rhs: Self) -> Self
}
extension String: Free {
static var ε: String { return "" }
}
[merge]
conflictstyle = diff3
[alias]
b = branch
ci = commit
co = checkout
cp = cherry-pick
di = diff
ds = diff --stat
f = fetch -pt

Keybase proof

I hereby claim:

  • I am jtbandes on github.
  • I am jtbandes (https://keybase.io/jtbandes) on keybase.
  • I have a public key whose fingerprint is 916E DA3E 8754 7C0B 173E D8E3 A6BB C78B 72E6 00C6

To claim this, I am signing this object:

import Darwin
enum Timebase {
private static let timebase: mach_timebase_info_data_t = {
var data = mach_timebase_info_data_t()
let ret = withUnsafeMutablePointer(&data, mach_timebase_info)
assert(ret == KERN_SUCCESS)
return data
}()
// Single-struct version.
// This seems to work just as well as having a custom generator struct.
// I'm not sure if there are other (performance?) implications, however.
struct PrefixSequence<T> : SequenceType
{
let base: AnySequence<T>
let maxLength: Int
init<S : SequenceType where S.Generator.Element == T>(_ base: S, _ maxLength: Int) {
//import Foundation
//class P: NSObject {}
protocol P {}
class C: P {}
extension SequenceType
{
func mapAs<T>(type: T.Type) -> [T] {
return map { $0 as! T }
@jtbandes
jtbandes / wtf.cpp
Last active August 29, 2015 14:27
a vtable gotcha
class Base1 {
public:
virtual void Foo() = 0;
};
class Base2 {
public:
virtual void Bar() = 0;
};
@jtbandes
jtbandes / neat.cpp
Last active August 29, 2015 14:27
fun things made possible by C++
// Suppose we want to convert argv/argc to a std::vector<std::string> for easier use.
// There are surprisingly many (and surprisingly concise) options:
// First pass:
vector<string> args;
for (int i = 1; i < argc; i++) {
args.push_back(string(argv[i]));
}
// Minor improvement using emplace, which forwards the char* param directly to a string constructor: