Skip to content

Instantly share code, notes, and snippets.

View erkekin's full-sized avatar
🍺
high fructose corn syrup

Erk Ekin erkekin

🍺
high fructose corn syrup
View GitHub Profile
@erkekin
erkekin / DFA.swift
Last active March 16, 2023 23:26
Deterministic Finite Automata in Swift
// https://www.youtube.com/watch?v=oHVHkkah3MY&t=979s
struct DFA<State: Hashable, Symbol: Hashable> {
init(delta: [DFA<State, Symbol>.Pair : State], initialState: State, finalStates: Set<State>) {
self.delta = delta
self.initialState = initialState
self.finalStates = finalStates
}
struct Pair: Hashable {
extension Set {
private func addCombination(prevCombo: [Element], pivotList: [Element]) -> [([Element], [Element])] {
var pivotList = pivotList
return (0..<pivotList.count)
.map { _ -> ([Element], [Element]) in
(prevCombo + [pivotList.remove(at: 0)], pivotList)
}
}
func combinations(m: Int) -> [[Element]] {
@erkekin
erkekin / Fixture
Created October 4, 2019 22:32
A quick generic fixture reader
import Foundation
import XCTest
struct Fixture<T: Decodable & Equatable> {
let value : T
init(url: URL) throws {
let actualData = try Data(contentsOf: url)
self.value = try JSONDecoder().decode(T.self, from: actualData)
}
func assert(_ t:T) {
@erkekin
erkekin / abbreviate.swift
Last active February 11, 2018 11:23
Gist to turn the sentence "MIDDLE EAST TECHNICAL UNIVERSITY" to "METU", Swift 4+
private func abbreviation(of sentence:String) -> String{
return sentence
.components(separatedBy: CharacterSet.whitespacesAndNewlines)
.flatMap{$0.first}
.map{String($0)}
.joined(separator: "")
}
@erkekin
erkekin / resign.sh
Last active January 24, 2018 11:03
Fetches all branches of different git repositories in a directory
# Creat a directory called all-repos, and clone all projects into it.
mkdir all-repos
cd all-repos
git clone THEPROJECT1-GIT-ADDRESS project1
git clone THEPROJECT2-GIT-ADDRESS project2
.
.
.
protocol Pageable{
var loading:Bool{get}
var paginator:Paginator{get set}
func fetchWith(paginator: Paginator)
}
struct Paginator {
var page = 0
func getURLSchema() -> String?{
guard let schemas = Bundle.main.object(forInfoDictionaryKey: "CFBundleURLTypes") as? [[String:Any]],
let schema = schemas.first,
let urlschema = schema["CFBundleURLName"] as? String
else {return nil}
return urlschema
}
@erkekin
erkekin / CountedSubset
Last active August 27, 2016 16:59
Get a random fixed count subset of an array in Swift
func getRandomElements<T:Equatable>(array:[T], count:Int) -> [T] {
var randomPicks: [T] = []
var randomIndex = Int.random(array.count)
var randomPick = array[randomIndex]
while randomPicks.count != count{
### Keybase proof
I hereby claim:
* I am erkekin on github.
* I am erkekin (https://keybase.io/erkekin) on keybase.
* I have a public key whose fingerprint is 7060 F1FD EA9E 6BA7 F657 AA4C 959B 55E6 B416 AC08
To claim this, I am signing this object:
@erkekin
erkekin /
Created February 27, 2012 15:49