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 | |
let str = "✑🍍🍎✒" | |
// returns little endian | |
let data = str.withCString(encodedAs: UTF16.self) { (lead) -> Data in | |
let ret = UnsafeBufferPointer(start: lead, count: str.utf16.count) | |
let data = Data(buffer: ret) | |
return data | |
} | |
let decode = String(data: data, encoding: .utf16LittleEndian)! |
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 | |
do { | |
struct Token { | |
var dict: [String: Any]? = nil | |
init(dictionary: [String: Any]) { | |
self.dict = dictionary | |
} | |
func data() -> Data { | |
let pointer = UnsafeMutablePointer<Token>.allocate(capacity: 1) |
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
/* | |
Hanoi Tower | |
Calucuating How many moves in Swift | |
result is 2^n - 1 | |
*/ | |
func hanoi(_ n: Int, from: String="from", target: String="target", other: String="other") -> Int { | |
guard n != 0 else { return 0 } | |
// move top of n-1 disks on from to other | |
var count = hanoi(n-1, from: from, target: other, other: target) |
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
var message: String = "test" | |
// Returns Array<Character> | |
let conv = message.flatMap { (chr) /*Character*/ in | |
return String(chr) + "/" | |
} | |
print(type(of: conv)) //Array<Character> | |
print(conv) //["t", "/", "e", "/", "s", "/", "t", "/"] | |
//https://developer.apple.com/documentation/swift/string/2903352-flatmap | |
let conv2 = message.flatMap { (chr) /*Character*/ -> String? in |
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
func getTupple() -> (first: Int, secound: Int) { | |
return (0, 1) | |
} | |
//Gets value with label | |
let f = getTupple().first | |
print(f) |
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
enum Pair<T, WORLD> { | |
case cons (T, WORLD) // (value, outside) | |
} | |
// Outside the computer | |
typealias WORLD = Any | |
// IO Monad Instance (a.k.a IO Action) | |
typealias IO<T> = (WORLD) -> Pair<T, WORLD> |
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
/* | |
$ swift --version | |
Apple Swift version 4.0.2 (swiftlang-900.0.69.2 clang-900.0.38) | |
Target: x86_64-apple-macosx10.9 | |
*/ | |
class Foo { | |
// not called! | |
deinit { | |
print(type(of: self), #function) |
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 List<T> { | |
var head: Node<T>? | |
var last: Node<T>? | |
class Node<T> { | |
let value: T | |
var next: Node<T>? = nil | |
init(_ value: T) { | |
self.value = value |
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 Value: NSObject { | |
@objc let raw: Int | |
init(_ raw: Int) { | |
self.raw = raw | |
} | |
override var description: String { | |
return "\(self.raw)" |
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 | |
import CoreLocation | |
/* | |
θ = atan2( sin Δλ ⋅ cos φ2 , cos φ1 ⋅ sin φ2 − sin φ1 ⋅ cos φ2 ⋅ cos Δλ ) | |
where φ1,λ1 is the start point, φ2,λ2 the end point (Δλ is the difference in longitude) | |
*/ | |
func calculateBearing(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> Double { | |
let x1 = from.longitude * (Double.pi / 180.0) |