Skip to content

Instantly share code, notes, and snippets.

View junebash's full-sized avatar

June Bash junebash

View GitHub Profile
@junebash
junebash / Effects.swift
Created December 23, 2022 21:43
Effect/Send protocol
// MARK: - Extensions
extension Task where Failure == Never {
public var cancellableValue: Success {
get async {
await withTaskCancellationHandler {
await self.value
} onCancel: {
self.cancel()
}
@junebash
junebash / TaskQueue.swift
Last active February 6, 2023 18:42
A basic task queue
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 {
@junebash
junebash / FormattedMarkdownWeek.swift
Created May 9, 2021 17:09
Print formatted week in Markdown
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)
}
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)
{
@junebash
junebash / CommonChars.Swift
Created January 29, 2020 16:59
Unit 4 Sprint 3 - Swift Code Challenge
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
@junebash
junebash / reverseOnlyLetters.swift
Created January 15, 2020 16:23
iOS11 Code Challenge (2020-01-15) - Reverse Only Letters
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 {
@junebash
junebash / FlipMatrix.swift
Last active December 18, 2019 16:30
Flip Matrix (Lambda School iOS11 Code Challenge)
/*
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:
@junebash
junebash / FindMissingNumbers.swift
Created December 11, 2019 16:10
FindMissingNumbers
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 {
@junebash
junebash / Sum.swift
Created November 13, 2019 16:18
u2s3-codingChallenge - Sum
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 []
}
@junebash
junebash / CGExtensions.swift
Last active August 21, 2020 15:20
CGContext + Add Curved Polygon (+ vector helper methods) — Core Graphics extensions
//
// 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