Skip to content

Instantly share code, notes, and snippets.

@pyrtsa
pyrtsa / Color.swift
Created January 9, 2015 08:21
Fun with default arguments in Swift initialisers
import UIKit
struct Color {
let color: UIColor
init(r: CGFloat = 0.0,
g: CGFloat = 0.0,
b: CGFloat = 0.0,
a: CGFloat = 1.0)
{
color = UIColor(red: r, green: g, blue: b, alpha: a)
@pyrtsa
pyrtsa / QuotedString.swift
Created January 23, 2015 14:50
Quoted strings in Swift
struct QuotedString : Printable {
var string: String
init(_ string: String) {
self.string = string
}
var description: String {
let backslash: UnicodeScalar = "\\"
let quote: UnicodeScalar = "\""
let hex: [UnicodeScalar] = ["0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"]
@pyrtsa
pyrtsa / Semigroupy.swift
Last active December 28, 2015 19:40
Semigroupy grouping magic in Swift
// What people often ask DefaultDict for, could be done with just one
// extension method for Optional.
extension Optional {
mutating func mappend(z: Wrapped, _ f: (Wrapped, Wrapped) -> Wrapped) {
self = map {x in f(x, z)} ?? z
}
}
// --- Examples ------------------------------------------------------------
@marchinram
marchinram / UIImage+PixelColor.swift
Last active January 19, 2022 08:53
iOS Swift UIImage subscript extension to get pixel color
import UIKit
extension UIImage {
subscript (x: Int, y: Int) -> UIColor? {
guard x >= 0 && x < Int(size.width) && y >= 0 && y < Int(size.height),
let cgImage = cgImage,
let provider = cgImage.dataProvider,
let providerData = provider.data,
let data = CFDataGetBytePtr(providerData) else {
return nil
@paulirish
paulirish / how-to-view-source-of-chrome-extension.md
Last active June 3, 2024 17:50
How to view-source of a Chrome extension

Option 1: Command-line download extension as zip and extract

extension_id=jifpbeccnghkjeaalbbjmodiffmgedin   # change this ID
curl -L -o "$extension_id.zip" "https://clients2.google.com/service/update2/crx?response=redirect&os=mac&arch=x86-64&nacl_arch=x86-64&prod=chromecrx&prodchannel=stable&prodversion=44.0.2403.130&x=id%3D$extension_id%26uc" 
unzip -d "$extension_id-source" "$extension_id.zip"

Thx to crxviewer for the magic download URL.

@radoyankov
radoyankov / Example.kt
Last active January 27, 2023 08:59
Easy Spannable on Kotlin
val spanned = spannable{ bold("some") + italic(" formatted") + color(Color.RED, " text") }
val nested = spannable{ bold(italic("nested ")) + url("www.google.com", "text") }
val noWrapping = bold("no ") + sub("wrapping ) + sup("also ") + "works"
text_view.text = spanned + nested + noWrapping
@cprovatas
cprovatas / Data+PrettyPrint.swift
Created May 23, 2018 15:52
Pretty print JSON string from Data in Swift 4.1 (especially useful printing to Xcode console)
import Foundation
extension Data {
var prettyPrintedJSONString: NSString? { /// NSString gives us a nice sanitized debugDescription
guard let object = try? JSONSerialization.jsonObject(with: self, options: []),
let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let prettyPrintedString = NSString(data: data, encoding: String.Encoding.utf8.rawValue) else { return nil }
return prettyPrintedString
}
@skuttruf
skuttruf / frac-diff_sk
Last active January 23, 2024 06:35
Python code for fractional differencing of pandas time series
"""
Python code for fractional differencing of pandas time series
illustrating the concepts of the article "Preserving Memory in Stationary Time Series"
by Simon Kuttruf
While this code is dedicated to the public domain for use without permission, the author disclaims any liability in connection with the use of this code.
"""
import numpy as np
import pandas as pd
@MichaelBarney
MichaelBarney / SwiftUI_Ad_Interstitial.swift
Created October 22, 2019 12:15
A google AdMob Interstitial implementation in SwiftUI
import SwiftUI
import GoogleMobileAds
import UIKit
final class Interstitial:NSObject, GADInterstitialDelegate{
var interstitial:GADInterstitial = GADInterstitial(adUnitID: interstitialID)
override init() {
super.init()
LoadInterstitial()
//
// BottomSheetView.swift
//
// Created by Majid Jabrayilov
// Copyright © 2019 Majid Jabrayilov. All rights reserved.
//
import SwiftUI
fileprivate enum Constants {
static let radius: CGFloat = 16