Skip to content

Instantly share code, notes, and snippets.

@boraseoksoon
boraseoksoon / MonacoWebView.swift
Created March 5, 2023 08:40
Load Monaco editor in a single html file for MacOS SwiftUI
import SwiftUI
import WebKit
@main
struct MonacoApp: App {
let codeSnippet = """
var body: some Scene {
WindowGroup {
EditorView(content: codeSnippet, language: "swift", theme: "vs-dark")
}
@boraseoksoon
boraseoksoon / fuzz.swift
Last active May 11, 2023 12:26
Swift fuzzy search in SwiftUI
This file has been truncated, but you can view the full file.
// written by https://talk.objc.io/episodes/S01E211-simple-fuzzy-matching
// credits should go to obj.io
import SwiftUI
import Cocoa
struct ContentView: View {
@State var needle: String = ""
var filtered: [(string: String, indices: [String.Index])] {
@boraseoksoon
boraseoksoon / loop.swift
Last active January 6, 2023 22:04
loop (forEach wrapper)
import Foundation
func loop(_ count: Int, body: @escaping ((Range<Int>.Element)) throws -> Void) rethrows {
try (0..<count).forEach(body)
}
func loop(_ count: Int, body: @escaping (() throws -> Void)) rethrows {
(0..<count).forEach { _ in
try? body()
}
@boraseoksoon
boraseoksoon / CopyLaunchedApplicationsInFrontToBackOrder.c
Created January 6, 2023 06:57 — forked from 0xced/CopyLaunchedApplicationsInFrontToBackOrder.c
Get an array of running applications ordered by last use
#import <Carbon/Carbon.h>
#import <dlfcn.h>
/*
* Returns an array of CFDictionaryRef types, each of which contains information about one of the processes.
* The processes are ordered in front to back, i.e. in the same order they appear when typing command + tab, from left to right.
* See the ProcessInformationCopyDictionary function documentation for the keys used in the dictionaries.
* If something goes wrong, then this function returns NULL.
*/
CFArrayRef CopyLaunchedApplicationsInFrontToBackOrder(void)
@boraseoksoon
boraseoksoon / swift_actor_race_condition.swift
Created January 4, 2023 05:46
Swift actor the most boring race condition example
import Foundation
class Class {
func log2() {
print("class log2")
}
func log3() {
print("class log3")
}
@boraseoksoon
boraseoksoon / swift_actor_data_race_only.swift
Created January 4, 2023 05:44
Swift actor the most boring data race example
import Foundation
class Class {
var i = 0
func decrease() {
i-=1
}
func increase() {
@boraseoksoon
boraseoksoon / actor_race_condition_data_race.swift
Last active January 5, 2023 04:42
Swift actor the most boring data race & race condition example (nonisolated)
import Foundation
class Class {
var i = 0
func decrease() {
i-=1
}
func increase() {
@boraseoksoon
boraseoksoon / call_java.clj
Last active December 28, 2022 20:12
call java from clojure
;; https://clojure-doc.org/articles/language/interop/
(doto (java.util.Stack.)
(.push 42)
(.push 13)
(.push 7)) ; ⇒ #<Stack [42, 13, 7]>
(.. (java.util.Date.)
getTime
toString)
@boraseoksoon
boraseoksoon / task_delay.swift
Created December 16, 2022 22:31
task delay swift
import Foundation
// reference: https://www.swiftbysundell.com/articles/delaying-an-async-swift-task/
let loadingSpinnerTask = Task.delayed(byTimeInterval: 0.15) {
self.showLoadingSpinner()
}
Task {
await a()
@boraseoksoon
boraseoksoon / debounce_compatibility.swift
Created December 16, 2022 21:09
debounce prior to iOS 16
import Foundation
// < iOS 16
var task: Task<(), Never>?
func debounce(seconds: Double = 1.0,
operation: @escaping () -> Void) {
task?.cancel()