Skip to content

Instantly share code, notes, and snippets.

View jonelf's full-sized avatar

Jonas Elfström jonelf

View GitHub Profile
@jonelf
jonelf / unfollow_all_podcasts.applescript
Created August 4, 2026 09:32
Unfollows all podcasts in the macOS Podcasts app
-- Unfollow all shows in Podcasts.app
--
-- Verified against the live UI (macOS 26 / Darwin 25.5.0) using System Events
-- accessibility scripting. Podcasts.app exposes almost no standard container
-- roles (no scroll area/table/outline) -- everything is nested AXGroups, so
-- this walks the tree manually via `UI elements of X` rather than relying on
-- System Events' recursive `every <class> of <container>` (which does not
-- reliably descend through this app's structure).
--
-- HOW TO RUN: osascript unfollow_all_podcasts.applescript
@jonelf
jonelf / Mandelbrot
Created March 23, 2015 14:03
Mandelbrot in Swift
for line in 0...32 {
println("".join((0...78).map { (col: Int) -> String in
var x = 0.0, y = x, i = 0
do {
x = x * x - y * y + Double(col) / 20.0 - 2.3
y = 2 * x * y + Double(line) / 10.0 - 1.5
i++
} while (x * x + y * y) < 4 && i < 78
return "\(UnicodeScalar(0x1f35b + i))"
ruby -e "32.times{|l|puts (0..78).map{|n|x=y=i=0;(x,y,i=x*x-y*y+n/38.0-1.5,2*x*y+l/14.0-1,i+1)until(x*x+y*y>4||i>78);(32+i).chr}*''}"
@jonelf
jonelf / JSON_stringify_bigint.ts
Created February 27, 2025 15:09
JSON.stringify can't handle BigInt by using this replacer it can
// Stringify using the replacer
// const jsonBigInteger = JSON.stringify(bigInteger, bigIntReplacer);
// Parse from JSON
// const fromJsonBigInteger = JSON.parse(jsonBigInteger, bigIntReviver);
export function bigIntReplacer(key: string, value: any): any {
if (typeof value === "bigint") {
return value.toString() + "n";
}
return value;
@jonelf
jonelf / superpermutation
Created March 13, 2024 17:17
Superpermutation in Ruby
# Naive implementation for finding the superpermutation of n symbols
# https://en.wikipedia.org/wiki/Superpermutation
n = 4
permutations = (1..n).to_a.permutation.map{|a| a.join}
s = permutations.first
permutations.shift
while (permutations.length > 0)
idx = nil
@jonelf
jonelf / standard_deviation.swift
Last active February 15, 2024 21:45
Standard Deviation in Swift
func standardDeviation(arr : [Double]) -> Double
{
let length = Double(arr.count)
let avg = arr.reduce(0, {$0 + $1}) / length
let sumOfSquaredAvgDiff = arr.map { pow($0 - avg, 2.0)}.reduce(0, {$0 + $1})
return sqrt(sumOfSquaredAvgDiff / length)
}
let responseTimes = [ 18.0, 21.0, 41.0, 42.0, 48.0, 50.0, 55.0, 90.0 ]
@jonelf
jonelf / test.ts
Created November 13, 2023 12:21
TypeScript any problem
interface Test {
done: boolean,
name: string,
}
function fn(input: Test) {
console.log(input.name)
}
let param1 : Test = {"done": true, "name": "Billie"}
@jonelf
jonelf / gist:3743071
Created September 18, 2012 13:23
10 longest user-agent strings from my access log
The 10 longest user-agent strings from my access log.
All of the are from Internet Explorer and the longest is 370 characters. This is silly!
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS129735; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; WWTClient2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; InfoPath.2; InfoPath.3; .NET4.0C; .NET4.0E)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS125042; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E; BO1IE8_v1;ENUS)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; YPC 3.2.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.0.3705; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; msn OptimizedIE8;ENGB)
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.
@jonelf
jonelf / validatePersonnummer.java
Created November 4, 2021 08:25
Validates a Swedish personal identity number.
package com.example;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.regex.*;
public class Main {
final static String DATE_FORMAT = "yyMMdd";
@jonelf
jonelf / balanced2.rb
Created November 1, 2021 12:38
JSON strings can contain braces
JSON strings can contain {}.
test_data = ["{dsfgsdf}", "{asdf}, {sdfsdf}", "{{dfgd}, {sadf}}", "sdfd}{", "{asdfsad}, {sdfs}}", "{{asdf},{sdfsdf}", '{"test{{{"}, {"{{{fest"}']
test_data.each { |s|
instring = false;
res = s.split(//).reduce(0) { |sum, c|
break sum if sum < 0
instring = !instring if c == '"'
if !instring