Skip to content

Instantly share code, notes, and snippets.

View Sajjon's full-sized avatar
🦀
Rust, Swift, Zig | SDK, Embedded, Mobile, Backend | Crypto, Fintech, Space, AI

Alexander Cyon Sajjon

🦀
Rust, Swift, Zig | SDK, Embedded, Mobile, Backend | Crypto, Fintech, Space, AI
View GitHub Profile
@Sajjon
Sajjon / Grade.swift
Created May 31, 2019 21:13
Grade NOT compiling but you get the general idea
enum Grade: Comparable, CustomStringConvertible {
case a(InneGrade)
case b(InneGrade)
case c(InneGrade)
case d(InneGrade)
case e(InneGrade)
case f
}
extension Grade {
enum ActivityType {
case football
case chess
}
class Activity {
let type: ActivityType
let payload: Any
}
@Sajjon
Sajjon / SeasonFromMonth.swift
Created May 4, 2019 13:47
Season from Month
enum Month: Int, CaseIterable {
case jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
}
extension Month {
var season: Season {
return Season(month: self)
}
}
@Sajjon
Sajjon / DecodingError+Equatable.swift
Created April 26, 2019 12:45
Making Swift Decodable DecodingError Equatable to ease of use for tests.
extension DecodingError: Equatable {
static func dataCorrupted(description: String) -> DecodingError {
return DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: description))
}
public static func == (lhs: DecodingError, rhs: DecodingError) -> Bool {
switch (lhs, rhs) {
@Sajjon
Sajjon / RecursivelyDeleteKeyValuePairFromJson.swift
Last active April 25, 2019 08:01
Simple ugly way of recursively deleting a key value pair from JSON in Swift
typealias JSON = [String: Any]
func recursivelyDeleteValue(forKey jsonKey: String, in jsonAny: Any) -> Any {
if var jsonObject = jsonAny as? JSON {
jsonObject.removeValue(forKey: jsonKey)
let loopable = jsonObject
for (key, value) in loopable {
jsonObject[key] = recursivelyDeleteValue(forKey: jsonKey, in: value)
}
return jsonObject
} else if let jsonArray = jsonAny as? [JSON] {
@Sajjon
Sajjon / RIPEMD160Tests.swift
Created April 9, 2019 17:50
Test vectors for RIPEMD160
class RIPEMD160Tests: XCTestCase {
func testTestVectors() {
for vector in testVectors {
let asciiMessage = vector.0
let expectedHash = vector.1
XCTAssertEqual(
RIPEMD160.hash(message: asciiMessage, encoding: .ascii).toHexString(),
expectedHash
)
}
@Sajjon
Sajjon / RIPEMD160.swift
Created April 8, 2019 19:31
RIPEMD160 in Swift 5
/// RIPEMD160 implementation in Swift 5.
///
/// Based on the work of Sjors Provoost, found on [Github CryptoCoinSwift][1]
///
/// Migrated to Swift 5 by [Alex Cyon a.k.a. Sajjon][2]
///
/// [1]: https://github.com/CryptoCoinSwift/RIPEMD-Swift
/// [2]: https://github.com/Sajjon
///
@Sajjon
Sajjon / dynamic_member_lookup_environment.swift
Created January 2, 2019 22:10 — forked from DougGregor/dynamic_member_lookup_environment.swift
Using Swift 4.2's @dynamicMemberLookup to expose environment variables
import Darwin
@dynamicMemberLookup
struct Environment {
subscript(dynamicMember name: String) -> String? {
get {
guard let value = getenv(name) else { return nil }
return String(validatingUTF8: value)
}
@Sajjon
Sajjon / OnboardingCoordinator+PushScene.swift
Created November 30, 2018 10:54
Medium article: SLC part 3 - OnboardingCoordinato Push Scene
private extension OnboardingCoordinator {
func toTermsOfService() {
let viewModel = TermsOfServiceViewModel(useCase: onboardingUseCase)
push(scene: TermsOfService.self, viewModel: viewModel) { [unowned self] userDid in
switch userDid {
case .acceptTermsOfService: self.toAnalyticsPermission()
}
}
}
@Sajjon
Sajjon / BaseCoordinator+ModallyPresentScene.swift
Last active November 30, 2018 11:20
Medium article: SLC part 3 - BaseCoordinator Present Scene.
typealias DismissScene = (_ animatedDismiss: Bool, _ presentationCompletion: (() -> Void)?) -> Void
extension BaseCoordinator {
/// This method is used to modally present a Scene.
///
/// - Parameters:
/// - scene: Type of `Scene` (UIViewController) to present.
/// - viewModel: An instance of the ViewModel used by the `Scene`s View.
/// - animated: Whether to animate the presentation of the scene or not.
/// - navigationHandler: **Required** closure handling the navigation steps emitted by the scene's ViewModel.
/// - step: The navigation steps emitted by the `viewmodel`