Skip to content

Instantly share code, notes, and snippets.

@svanellewee
svanellewee / how-to-defer-like-golang.el
Last active November 30, 2023 13:05
Emacs Lisp Gist: DEFER-ing/ Python's try finally
(defun some-dodgey-function (value)
(let* ((result (/ 10 value)))
(message "Rwesult is %d" result)))
(defun some-recovery-or-cleanup-code-to-always-run ()
(message "I ALWAwYS RUN!!!"))
(unwind-protect
(some-dodgey-function 0)
@svanellewee
svanellewee / aproducer.py
Created February 22, 2023 13:55 — forked from dabeaz/aproducer.py
"Build Your Own Async" Workshop - PyCon India - October 14, 2019 - https://www.youtube.com/watch?v=Y4Gt3Xjd7G8
# aproducer.py
#
# Async Producer-consumer problem.
# Challenge: How to implement the same functionality, but no threads.
import time
from collections import deque
import heapq
class Scheduler:
@svanellewee
svanellewee / start-docker.sh
Last active November 1, 2022 22:29
start multipass docker.
#!/usr/bin/env bash
export DOCKER_HOST=ssh://ubuntu@docker.local
export DOCKER_HOST=tcp://docker.local
multipass set client.primary-name=docker
function start-docker() {
local SSH_KEY="id_$RANDOM"
local PUBLIC_SSH_KEY="${SSH_KEY}.pub"
ssh-keygen -b 2048 -t rsa -f "$SSH_KEY" -P ""
@svanellewee
svanellewee / macos-multipass-docker.sh
Last active October 31, 2022 08:25 — forked from spuf/macos-multipass-docker.sh
macos multipass docker
brew install multipass
multipass launch lts -c 6 -d 20G -m 8G -n docker
multipass set client.primary-name=docker
multipass mount /Volumes/path/to/repo docker
multipass shell
curl -sSL https://get.docker.com/ | sh
echo '{"features": {"buildkit": true}}' > /etc/docker/daemon.json
# or if you have a local k8s cluster (ala microk8s)
@svanellewee
svanellewee / genericprepend.go
Last active April 12, 2022 07:44
Generic Prepend in Go!
package main
// https://play.golang.com/p/PNax2a1rL3q
import (
"fmt"
"math/rand"
"time"
"golang.org/x/exp/constraints"
@svanellewee
svanellewee / Main.kt
Created February 22, 2022 10:45
Golang style pipelines/merge ops in Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlin.random.Random
import kotlin.random.nextUInt
import kotlin.time.ExperimentalTime
import kotlin.time.measureTime
data class TestMessage(val id: Int, val startTime: Long, val data: UInt, val streamName: String, val newData: UInt = 0u)
@svanellewee
svanellewee / Main.kt
Created February 20, 2022 21:09
Kotlin coroutines, ktor, tls k8s logs.
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.network.tls.*
import io.ktor.utils.io.*
import io.ktor.utils.io.core.*
import kotlinx.coroutines.CoroutineScope
@svanellewee
svanellewee / MainActivity.kt
Last active January 30, 2022 21:29
ViewModel, Room
package com.svanellewee.hackdbagain
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.viewModels
import androidx.lifecycle.*
import androidx.room.*
import com.svanellewee.hackdbagain.databinding.ActivityMainBinding
@svanellewee
svanellewee / tmux-start.sh
Created January 25, 2022 09:16
Connect to the same tmux
# pop this in your bashrc
if [[ -z "$TMUX" ]] # hey bash, are you inside a tmux already? Then ignore!
then
# if bash is not currently in a tmux session, look for the first..
first_session=$(tmux ls 2>/dev/null|grep -Po '\K[0-9]+?(?=\: )')
if [[ ! -z "${first_session}" ]]
then
# oh you found a session! Attach to that badboy
tmux attach-session -t "${first_session}"
@svanellewee
svanellewee / create-bundle-truststore.sh
Created November 18, 2021 09:32
Splitting PEM certs and inserting into JKS
#!/usr/bin/env bash
function split-bundle() {
local input_file="${1}"
local prefix="${2:-cert-}"
csplit -z -f "${prefix}" "${input_file}" '/-----BEGIN CERTIFICATE-----/' '{*}'
}
function new-truststore() {
local output_jks="${1:-output.jks}"