Skip to content

Instantly share code, notes, and snippets.

@worchyld
worchyld / functional-forloop.swift
Created August 6, 2017 07:31
Create 10 decks in a functional for-loop
// Swift 3
struct Deck {
var name: String
}
// usual way;
var decks : [Deck] = [Deck]()
for idx in 1...10 {
decks.append( Deck.init(name: "#\(idx)") )
//
// WalletInteractor.swift
//
// Tries to use an Interactor to fashion off work
// against the Wallet model via delegates
//
// Tries to be single responsibility (?)
//
// Created by AD on 21/06/2020.
// Copyright © 2020 AD. All rights reserved.
@worchyld
worchyld / Wallet.swift
Created October 6, 2022 17:18
Wallet.swift
import Foundation
// Simple cash handling with error throwing
internal protocol WalletDelegate {
func didCredit(_amount: Int) -> Bool
func didDebit(_amount: Int) -> Bool
}
@worchyld
worchyld / deck.swift
Created October 8, 2022 02:23
Swift deck using generics (basic)
// Generic deck management, doesn't care about its contents
protocol DeckDelegate: AnyObject {
associatedtype Element
var size: Int { get }
var isEmpty: Bool { get }
var cards:[Element] { get set }
func push(_ element: Element)
func pop() -> Element?
func shuffle()
@worchyld
worchyld / Die.swift
Last active October 28, 2022 18:49
Die, D6 random number creator using Gameplaykit
import GameplayKit
struct Die: Equatable {
public static var roll: Int {
if #available(iOS 9, *) {
let d6 = GKRandomDistribution.d6()
return (d6.nextInt())
}
else {
let d6 = 6