Skip to content

Instantly share code, notes, and snippets.

@jepers
jepers / float8.swift
Last active March 24, 2020 11:27
A Float8 type
// ============================================================================
// 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
// ===========================================================================
@jepers
jepers / hexdump.swift
Last active April 14, 2019 11:15
Like hexdump -C but in Swift 4.1
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 = ""
@jepers
jepers / RandomGenerator.swift
Created June 3, 2017 06:51
A Swift 3.1 implementation of the Xoroshiro128+ generator
//
// 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
@jepers
jepers / RandomSentenceGenerator.swift
Created June 2, 2017 22:57
A simple way to generate random sentences using NSSpellChecker.
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 {
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>
@jepers
jepers / test.swift
Last active November 29, 2015 03:07
Testing Swift's ability to optimize code using simple custom Float4 and Float4x4 value types, comparing to SIMD counterparts, subscript implementation turns out to make a big difference for F4x4 while not for F4 ...
//==============================================================================
// 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 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 } }
@jepers
jepers / test.swift
Last active January 26, 2016 03:50
Example of something that takes a long time to type check.
// 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 {}
@jepers
jepers / Hmm.swift
Created November 18, 2015 19:26
An example of where I get confused by the interplay between associated types, typealiases and protocol extensions.
// 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
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