Skip to content

Instantly share code, notes, and snippets.

@horothesun
horothesun / PriorityQueue.swift
Last active October 17, 2019 13:24
`set_timer2` is implemented by `Scheduler.setTimer(waitForMillis:task:)`. A priority-queue's used to keep track of the scheduled tasks by priority (absolute start time).
// TODO: move 'elements' from array to min-heap
final class PriorityQueue<Element: Equatable, Priority> {
private var elements = [Element]()
private let priorityForElement: (Element) -> Priority
private let priorityGreaterThan: (Priority, Priority) -> Bool
init(
priorityForElement: @escaping (Element) -> Priority,
priorityGreaterThan: @escaping (Priority, Priority) -> Bool
@horothesun
horothesun / github_billing.sh
Last active July 13, 2021 02:08
github_billing
# IMPORTANT: GITHUB_TOKEN with "Update ALL user data" permission
#
# Dependencies:
# - curl
# - jq
#
# Run:
# GITHUB_TOKEN=<token> GITHUB_USER=<username> ./github_billing.sh
#
brew install cpanminus kpcli
# https://sourceforge.net/p/kpcli/wiki/Installation%20instructions/
cpanm Crypt::Rijndael Term::ReadKey Sort::Naturally File::KeePass Term::ShellUI Term::ReadLine::Gnu Term::ReadLine::Perl Clipboard Sub::Install Data::Password::passwdqc Data::Password::passwdqc Math::Random::ISAAC Authen::OATH Convert::Base32
@horothesun
horothesun / poll.groovy
Last active August 29, 2021 11:59
Polling in Groovy (useful for Jenkins pipelines)
def <T> T poll( // can throw
long pollIntervalSeconds, // must be bigger than `functionToPoll()` execution time
long timeoutSeconds, // global timeout
Integer maxConsecutiveExceptionsAllowed, // maximum consecutive times `functionToPoll()` can throw before polling fails
Closure<T> functionToPoll, // returns T, can throw
Closure<Boolean> successConditionOnFnResult // returns Boolean, condition over `functionToPoll()` result to determine if polling is completed
) {
def Boolean isDone = false
def Boolean isTimeout = false
def Integer exceptionCounter = 0
@horothesun
horothesun / clone_all_team_repos.sh
Created January 11, 2022 16:26
Clone all team repos
#!/bin/bash
# Usage: GITHUB_OWNER=<owner> GITHUB_TEAM=<team> clone_all_team_repos.sh
#
# Dependencies: gh, jq
[ -z "$GITHUB_OWNER" ] && echo "Error: GITHUB_OWNER must be defined" && exit 10
[ -z "$GITHUB_TEAM" ] && echo "Error: GITHUB_TEAM must be defined" && exit 20
function cloneIfDoesNotExist() {
@horothesun
horothesun / toggle_sidecar.sh
Last active April 6, 2022 19:37
Toggle Apple Sidecar display
#!/bin/bash
# Setup
#
# ln -sf `pwd`/toggle_sidecar.sh ~/bin/toggle_sidecar.sh
# Usage
#
# toggle_sidecar.sh
@horothesun
horothesun / CatsEffectOODecoratorCachedClient.scala
Last active June 16, 2022 21:04
Cats-effect - Cached client
import cats.effect.{IO, Resource}
case class Config()
case class InputA()
case class InputB()
case class InputC()
case class OutputA()
case class OutputB()
case class OutputC()
@horothesun
horothesun / input.csv
Created September 30, 2022 08:47
CSV filtering
user_id name phone
annoyingPrefix_001 Alice 07782123456
annoyingPrefix_002 Bob 07798765432
annoyingPrefix_003 Carl 07711928374
@horothesun
horothesun / findNumber.pl
Created October 9, 2022 20:09
Find a number X such that "prepending" 4 to X equals to 4 times the number generated by "suffixing" X with 4
%% Find a number X such that "prepending" 4 to X equals to
%% 4 times the number generated by "suffixing" X with 4.
%%
%% X = X1,...,Xn . 4,X1,...,Xn = 4 * X1,...Xn,4
%%
%% :- find( 4, 9000, 11000, N ).
%%
natural( 0 ).
natural( s(X) ) :- natural( X ).
@horothesun
horothesun / get_all_gh_secrets.sh
Last active June 4, 2023 10:05
Get all GitHub secrets
#!/bin/bash
# Usage: ./get_all_gh_secrets.sh "<OWNER>" "100"
GITHUB_OWNER=$1
MAX_NUMBER_OF_REPOS=$2
[[ -z "${GITHUB_OWNER}" ]] && echo "Error: GITHUB_OWNER must be passed as first parameter" && exit 10
[[ -z "${MAX_NUMBER_OF_REPOS}" ]] && echo "Error: MAX_NUMBER_OF_REPOS must be passed as second parameter" && exit 20