This file contains hidden or 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
// ============================================================================ | |
// This is an attempt at implementing a Float8 type by Jens Persson. | |
// It seems to work. | |
// This file is in the form of a command line program which will | |
// do some basic checks and print all Float8 values. | |
// --------------------------------------------------------------------------- | |
// Use it in any way you like, please let me know of any issues or | |
// improvements here: https://forums.swift.org/t/33337/38 | |
// =========================================================================== |
This file contains hidden or 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 | |
/// Writes a canonical hex+ASCII representation of `bytes` into `output`. | |
func hexdump<Target>(_ bytes: UnsafeRawBufferPointer, to output: inout Target) | |
where Target: TextOutputStream | |
{ | |
guard bytes.count > 0 else { return } | |
var asciiString = "" |
This file contains hidden or 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
// | |
// RandomGenerator.swift | |
// | |
// Created by Jens Persson on 2017-05-22. | |
// | |
import Security // Only needed for SecRandomCopyBytes, used for random seed generation in the init() of RandomGenerator | |
// MARK: - RandomGenerator Protocol |
This file contains hidden or 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 AppKit | |
let filteredWords = Set<String>(["app", "apps"]) | |
let questionPrefixes = Set<String>([ | |
"Which", "What", "Who", "Whom", "Whose", "Where", "When", "How", "Why", "Can", | |
"May", "Will", "Is", "Do", "Shall", "Has", "Must", "Would", "Could", "Should" | |
]) | |
extension String { |
This file contains hidden or 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
protocol DefaultInitializable { init() } | |
protocol StaticStorageType : DefaultInitializable { | |
typealias Element: DefaultInitializable = Self | |
static var staticCount: Int { get } | |
} | |
extension StaticStorageType { | |
typealias Plus1 = StaticStorageOfOneMoreThan<Self> | |
typealias Plus2 = Plus1.Plus1 | |
typealias Plus3 = Plus2.Plus1 | |
typealias Times2 = StaticStorageDoubled<Self> |
This file contains hidden or 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
//============================================================================== | |
// This program is possibly demonstrating an issue/opportunity for improvement | |
// of the optimizer. | |
// (Tested with Xcode 7.2 beta 4, OS X 10.10.5, Macbook Pro late 2013) | |
// (Compiled (-O -gnone) as command line app) | |
//============================================================================== | |
// | |
// There is a test at the end which measures the time it takes to do some | |
// matrix (and vector) type operations. | |
// |
This file contains hidden or 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
//---------------------------------------------------------------------------------------------------- | |
// This program compiles and runs. Question: Can generic struct X be made to satisfy TEST? | |
//---------------------------------------------------------------------------------------------------- | |
protocol DefaultInitializable { init() } | |
extension Double : DefaultInitializable {} | |
extension Float : DefaultInitializable {} | |
extension Int : DefaultInitializable {} | |
extension UInt8 : DefaultInitializable {} | |
// ... | |
protocol CountType { static var value: Int { get } } |
This file contains hidden or 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
// This little program takes ~ 6 seconds to compile on my MacBook Pro Late 2013. | |
// It compiles and works as expected. But the type checker spends a lot of time | |
// on the function at the end. | |
// In my actual project, I have much more code like this, and it results in very | |
// long compile times. So I'd like to know if there is something I can do to | |
// speed that up. | |
protocol DefaultInitializable { init() } | |
extension Float : DefaultInitializable {} |
This file contains hidden or 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
// I'm using the concept of bits just to make the code a bit more concrete. | |
// The actual use case has nothing to do with implementing type level bits ... | |
// This code compiles and runs as is, but note the last line. | |
// Q: Should this program compile without errors? If so, what should the last line print? | |
protocol BitType {} | |
struct Clr : BitType {} | |
struct Set : BitType {} | |
protocol BitAndOp { | |
typealias A: BitType = Clr |
This file contains hidden or 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
struct True {} | |
struct False {} | |
protocol ResultPossessingType {} | |
extension ResultPossessingType { typealias Result = False } | |
struct IsIntegerType<T> : ResultPossessingType { | |
static func printResult() { print("Static method prints \(self).Result:", Result.self, self.Result.self) } // <-- Added self.Result.self | |
} | |
extension IsIntegerType where T: IntegerType { | |
typealias Result = True |
NewerOlder