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
@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:
@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;
};
//import Foundation
//class P: NSObject {}
protocol P {}
class C: P {}
extension SequenceType
{
func mapAs<T>(type: T.Type) -> [T] {
return map { $0 as! T }
// Much safer! Notice that `iteration` stops increasing after the initial terms are produced.
// Thanks @oisdk for pointing this out.
public struct RecurrenceRelation<Element>: SequenceType, GeneratorType
{
private let recurrence: (T: [Element], n: Int) -> Element
private var storage: [Element]
/// - Parameter initialTerms: The first terms of the sequence.
/// The `count` of this array is the **order** of the recurrence.
// please... don't do this
import Foundation
class F: NSObject
{
@objc func foo() {
print("hi")
}
}
//: Convenience functions/extension on top of GCD.
import Dispatch
var MainQueue: dispatch_queue_t { return dispatch_get_main_queue() }
func GlobalQueue(qos: dispatch_qos_class_t = .Default) -> dispatch_queue_t
{
return dispatch_get_global_queue(qos, 0)
}
// 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 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
}()
@jtbandes
jtbandes / xcpaste.md
Last active August 12, 2018 21:35
xcpaste — copy formatted code as HTML/CSS

example

@jtbandes
jtbandes / Function.h
Last active April 21, 2020 16:51
fun with Obj-C "generics"
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
@interface Function<__contravariant InType, __covariant OutType> : NSObject
{
// interestingly, this does work here (though OutType & InType are not available in the @implementation)
OutType (^_block)(InType);
}