Skip to content

Instantly share code, notes, and snippets.

View theoknock's full-sized avatar

James Alan Bush theoknock

View GitHub Profile
@theoknock
theoknock / ContentView.swift
Created July 5, 2024 18:50
A basic FileDocument template created by ChatGPT
import SwiftUI
import UniformTypeIdentifiers
struct TextFile: FileDocument {
static var readableContentTypes: [UTType] { [.plainText] }
var text = ""
init(initialText: String = "") {
text = initialText
@theoknock
theoknock / ContentView.swift
Last active July 5, 2024 18:05
FileDocument
import SwiftUI
struct ContentView: View {
@Binding var document: TextFile
var body: some View {
HStack {
TextEditor(text: $document.text)
}
}
@theoknock
theoknock / Speech-to-Text-to-Speech.swift
Last active July 3, 2024 16:39
Takes dictation and reads it back.
import SwiftUI
import Speech
import AVFoundation
import Combine
import Observation
@Observable class SpeechRecognizer: NSObject, SFSpeechRecognizerDelegate {
var transcription: String = "Transcription"
var isTranscribing: Bool = false
@theoknock
theoknock / Gemini _Pro_1.5-Bible-Verse-Topical-Search.py
Created June 29, 2024 04:52
Gemini _Pro_1.5-Bible-Verse-Topical-Search.py
"""
Install the Google AI Python SDK
$ pip install google-generativeai
See the getting started guide for more information:
https://ai.google.dev/gemini-api/docs/get-started/python
"""
import os
import SwiftUI
import Speech
import AVFoundation
import Combine
import Observation
@Observable
class SpeechRecognizer {
var transcription: String = ""
var isTranscribing: Bool = false
@theoknock
theoknock / Non-Branching Alternatives to Ternary Expressions.swift
Last active June 10, 2024 04:53
Testing non-branching alternatives to ternary expressions (array-based vectorized expression vs. inline bitwise operation v. ternary conditional expressions). The vectorized expression narrowly beat the ternary; the bitwise operation lags twice over.
import CoreFoundation
import Foundation
var a = 3
var b = 7
var c = 5
var d = 10
func measureTime(for expression: () -> Void, iterations: Int) -> Double {
var totalTime: CFAbsoluteTime = 0
@theoknock
theoknock / Connect iPhone USB Hotspot and Disable Wi-Fi on Startup and Connect
Last active June 10, 2024 04:23
Connect iPhone USB Hotspot and Disable Wi-Fi on Startup and Connect
sudo pico ~/scripts/connect_hotspot.applescript
sudo pico ~/scripts/connect_hotspot.zsh
chmod +x ~/scripts / connect_hotspot.zsh
sudo pico ~/Library/LaunchAgents/com.user.connecthotspot.plist
networksetup -listallnetworkservices
tail -f ~/connect_hotspot.log
@theoknock
theoknock / bitwise_function_pointer_swap.swift
Created June 5, 2024 12:43
Transitions between functions when the value passed to each decrements to zero
import Foundation
typealias PredicateFunction = (UInt) -> UInt
typealias PredicateFunctionPointer = UnsafeMutablePointer<PredicateFunction>
let swapPointers: () -> Void = {
var funcA: PredicateFunction = { predicate in
print("funcA returned", terminator: " ")
return predicate
}
@theoknock
theoknock / ContentView.swift
Last active June 5, 2024 10:03
"Circular distributions can be used even when the variables concerned are not explicitly angles: the main consideration is that there is not usually any real distinction between events occurring at the opposite ends of the range, and the division of the range could notionally be made at any point." https://en.wikipedia.org/wiki/Circular_distribu…
import Foundation
import SwiftUI
import Combine
import Observation
struct ContentView: View {
@State private var randoms: LatticeCircularDistributor = LatticeCircularDistributor(boundLower: 0.3125, boundUpper: 0.3125, threshholdLeft: 0.25, threshholdRight: 0.25)
var body: some View {
Spacer()
VStack {
@theoknock
theoknock / CircularLatticeDistribution.swift
Created May 27, 2024 01:05
A circular distribution or polar distribution is a probability distribution of a random variable whose values are angles, usually taken to be in the range [0, 2π). A circular distribution is often a continuous probability distribution, and hence has a probability density, but such distributions can also be discrete, in which case they are called…
/*
A circular distribution or polar distribution is a probability distribution of a random variable whose values are angles, usually taken to be in the range [0, 2π). A circular distribution is often a continuous probability distribution, and hence has a probability density, but such distributions can also be discrete, in which case they are called circular lattice distributions. Circular distributions can be used even when the variables concerned are not explicitly angles: the main consideration is that there is not usually any real distinction between events occurring at the opposite ends of the range, and the division of the range could notionally be made at any point.
*/
// https://en.wikipedia.org/wiki/Circular_distribution
import SwiftUI
func scale(oldMin: Double, oldMax: Double, value: Double, newMin: Double, newMax: Double) -> Double {
return ((value - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin