Skip to content

Instantly share code, notes, and snippets.

View yossan's full-sized avatar

Kosuke Yoshimoto yossan

  • Nagoya, Aichi, Japan
View GitHub Profile
@yossan
yossan / calculate_avg.swift
Created February 5, 2018 06:14
Calculation speed comparing a for-statement with a KVC operation
import Foundation
class Value: NSObject {
@objc let raw: Int
init(_ raw: Int) {
self.raw = raw
}
override var description: String {
return "\(self.raw)"
@yossan
yossan / class_list.swift
Last active July 27, 2018 23:04
Implementing Linked List by using class or enum.
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
@yossan
yossan / not_releasing_memory.swift
Created November 12, 2017 08:09
UnsafeRawPointer doesn't release the memory of the underlying instance.
/*
$ 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)
@yossan
yossan / io_monad_sample.swift
Last active September 19, 2019 08:36
IO Monad with Swift
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>
@yossan
yossan / labeled_tuple_sample.swift
Created October 4, 2017 15:08
Getting value with label from a labeled tuple
func getTupple() -> (first: Int, secound: Int) {
return (0, 1)
}
//Gets value with label
let f = getTupple().first
print(f)
@yossan
yossan / string_flatmap_sample.swift
Last active October 4, 2017 15:09
[Swift4] String's flatMap
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
@yossan
yossan / calculate_hanoi.swift
Last active September 18, 2017 03:53
Hanoi Tower
/*
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)
@yossan
yossan / struct2data.swift
Created September 11, 2017 15:53
Converting costom struct to Data in Swift3
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)
@yossan
yossan / string2utf16le.swif
Last active October 18, 2017 05:30
converts string to little endian in swift
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)!
@yossan
yossan / count_down_steream.swift
Last active February 5, 2017 05:33
CountDownStream
/// Creating Stream for CountDown
func createCountDown(_ n: Int) -> Array<Any> {
guard n > 0 else {
return [0]
}
return [n, {
return createCountDown(n-1)
}]
}