This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Extensions | |
extension Task where Failure == Never { | |
public var cancellableValue: Success { | |
get async { | |
await withTaskCancellationHandler { | |
await self.value | |
} onCancel: { | |
self.cancel() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
actor TaskQueue { | |
private var queuedOperations: [() async -> Void] = [] // should probably be an actual `Queue`/`Deque` type | |
private var currentTask: Task<Void, Never>? | |
private var iterationTask: Task<Void, Never>? | |
init() {} | |
func addOperation(_ operation: @escaping () async -> Void) { | |
if currentTask == nil { | |
currentTask = Task { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// Prints the week in Markdown format. Useful for planning. Throw this into a TextExpander shell script! | |
func markdownWeek() -> String { | |
let cal = Calendar.current | |
func weekDay(of date: Date) -> Int { | |
cal.component(.weekday, from: date) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct CodeChallengeTestCases<Input, Output> { | |
var title: String? | |
var expected: KeyValuePairs<Input, Output> | |
var solution: (Input) -> Output | |
init( | |
title: String? = nil, | |
expected: KeyValuePairs<Input, Output> = [:], | |
solution: @escaping (Input) -> Output) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func commonChars(_ inputs: [String]) -> [String] { | |
guard let lastString = inputs.last else { return [] } | |
var commonCountedChars = CountedSet<String>() | |
for char in lastString { | |
commonCountedChars.add(String(char)) | |
} | |
var strings = inputs | |
strings.removeLast() | |
// removing last is more computationally efficient than removing first in Swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func reverseOnlyLetters(_ inputString: String) -> String { | |
let lettersReversed = [Character]( | |
inputString.compactMap { char -> Character? in | |
return char.isLetter ? char : nil | |
}.reversed() | |
) | |
var outputCharacters = [Character](inputString) | |
var i = 0 | |
while i < lettersReversed.count { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given a matrix A, return the transpose of A. | |
The transpose of a matrix is the matrix flipped over it's main diagonal, | |
switching the row and column indices of the matrix. | |
Example 1: | |
Input: [[1,2,3],[4,5,6],[7,8,9]] | |
Output: [[1,4,7],[2,5,8],[3,6,9]] | |
Example 2: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func findMissingNumbers(_ nums: [Int]) -> [Int] { | |
var includedNums = [Int:Int]() | |
var missingNums = [Int]() | |
for num in nums { | |
includedNums[num] = num | |
} | |
for i in 1...nums.count { | |
if includedNums[i] == nil { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func indicesToSum(from array: [Int], to target: Int) -> [Int] { | |
for i in 1 ..< array.count { | |
for j in 0 ..< i { | |
if array[i] + array[j] == target { return [j,i] } | |
} | |
} | |
print("ERROR: No sum possible!") | |
return [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CGExtensions.swift | |
// | |
// Created by Jon Bash on 2019-11-06. | |
// Copyright © 2019 Jon Bash. All rights reserved. | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to | |
// deal in the Software without restriction, including without limitation the | |
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
NewerOlder