Skip to content

Instantly share code, notes, and snippets.

View djds23's full-sized avatar

Dean Silfen djds23

View GitHub Profile
@djds23
djds23 / SKAction+Keypath.swift
Created March 2, 2021 03:41
An SKAction that writes a value to a specified object.
extension SKAction {
static func write<Root: AnyObject, Value>(
value: Value,
to keyPath: ReferenceWritableKeyPath<Root, Value>,
on object: Root
) -> SKAction {
SKAction.customAction(withDuration: 0) { [weak object] _, _ in
object?[keyPath: keyPath] = value
}
/*
The answers to each of these questions should be written as a function.
Call the function to execute your code.
Open this file in a text editor like "Sublime Text" or "Notepad++"
Write your answers in this file, saving often.
/*
The answers to each of these questions should be written as a function.
Call the function to execute your code.
Open this file in a text editor like "Sublime Text" or "Notepad++"
Write your answers in this file, saving often.
@djds23
djds23 / chapter2-exercise-1.js
Created April 19, 2020 20:35
Chapter 2 For the WAC Study Group
let output = "";
let character = "#";
for (let count = 0; count < 7; count += 1) {
output += character;
console.log(output);
}
import Foundation
enum MovieGenre: Int, Codable {
case action, horror, art, comedy, drama
}
let genre: MovieGenre = .action
let encoder = JSONEncoder()
// On the follow line, the code raises Swift.EncodingError.invalidValue
let jsonValue = try! encoder.encode(genre)
class TrainDepartureService {
func departureTimes(forLine: TrainLine, departingFrom: TrainStation) -> Observable<Date> {
return Observable<TimeInterval>.timer(0, period: 60, scheduler: scheduler)
.flatMapLatest { _ -> Observable<Date> in
network.departureTime(forLine: forLine, departingFrom: departingFrom)
}
}
private let scheduler: SchedulerType
enum TrainLine { … }
enum TrainStation { … }
protocol TrainNetworkHandler {
func departureTime(forLine: TrainLine, departingFrom: TrainStation) -> Observable<Date>
}
func waitForResourceAndButtonTap(
resource: Observable<Bool>,
buttonTap: Observable<Void>
) -> Observable<Bool> {
let emitOnlyAfterResourceHasArrived = Observable
.combineLatest(
buttonTap,
resource.take(1)
)
func simpleCombineStreams(
resource: Observable<Bool>,
buttonTap: Observable<Void>
) -> Observable<Bool> {
return buttonTap
.withLatestFrom(resource)
}
struct FollowerNetworkingMock: FollowNetworkHandler {
let mockFollowers: [User]
let followResponse: Response
func follow(user: User) -> Observable<Response> {
return .just(followResponse)
}
func followers() -> Observable<[User]> {
return .just(mockFollowers)
}
}