This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Base1 { | |
public: | |
virtual void Foo() = 0; | |
}; | |
class Base2 { | |
public: | |
virtual void Bar() = 0; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import Foundation | |
//class P: NSObject {} | |
protocol P {} | |
class C: P {} | |
extension SequenceType | |
{ | |
func mapAs<T>(type: T.Type) -> [T] { | |
return map { $0 as! T } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// please... don't do this | |
import Foundation | |
class F: NSObject | |
{ | |
@objc func foo() { | |
print("hi") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |