Skip to content

Instantly share code, notes, and snippets.

@khanlou
khanlou / A - Usage.swift
Last active July 14, 2024 20:25
This ScrollView has a modifier called `onScroll`, which is updated when scrolls occur.
struct ContentView: View {
@State var scrollOffset: CGPoint = .zero
var body: some View {
ObservableScrollView {
Text("Hello, world!")
.foregroundColor(self.scrollOffset.y == 0 ? .blue : .red)
}
.onScroll { self.scrollOffset = $0 }
tell application "Music"
-- Get list of albums, the fast way...
set albumsWithDups to (album of every track)
set albumsNames to my removeDuplicates(albumsWithDups)
-- Check each album
repeat with currentAlbum in albumsNames
set albumSongs to (every track of library playlist 1 whose album is currentAlbum)
//Copyright 2019 Soroush Khanlou
//
//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 sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE O
import Foundation
struct ProbingDictionary<Key: Hashable, Value> {
enum Item: CustomStringConvertible {
case element(Key, Value)
case empty
case tombstone
var isEmpty: Bool {
if case .empty = self {
@khanlou
khanlou / ObjectStorage.swift
Created April 1, 2019 21:02
ObjectStorage 3.0
//
// ObjectStorage.swift
// Soroush Khanlou
//
// Created by Soroush Khanlou on 3/8/19.
// Copyright © 2019 Soroush Khanlou. All rights reserved.
//
import Foundation
@khanlou
khanlou / URLRequest.swift
Last active February 9, 2019 20:33 — forked from peterprokop/URLRequest.swift
Print NSURLRequest in cURL format
//
// URLRequest.swift
//
// Created by Peter Prokop on 17/08/2017.
// Modified by Soroush Khanlou on 09/12/2018.
import Foundation
public extension URLRequest {
struct LazyUniquedSequence<Element>: Sequence {
let base: AnySequence<Element>
let isEqual: (Element, Element) -> Bool
init<S: Sequence>(_ sequence: S, isEqual: @escaping (Element, Element) -> Bool) where S.Element == Element {
self.base = AnySequence(sequence)
self.isEqual = isEqual
}
@khanlou
khanlou / SKSerialInputStream.h
Last active November 10, 2022 11:41
Partially adapted from AFMultipartBodyStream
//
// SKSerialInputStream.h
// inputstream
//
// Created by Soroush Khanlou on 11/4/18.
// Copyright © 2018 Soroush Khanlou. All rights reserved.
//
#import <Foundation/Foundation.h>
struct LazyUniquedSequence<Element>: Sequence {
let base: AnySequence<Element>
let isEqual: (Element, Element) -> Bool
init<S: Sequence>(_ sequence: S, isEqual: @escaping (Element, Element) -> Bool) where S.Element == Element {
self.base = AnySequence(sequence)
self.isEqual = isEqual
}
@interface HTTPMethod: NSString
+ (HTTPMethod *)GET;
+ (HTTPMethod *)POST;
@end