Skip to content

Instantly share code, notes, and snippets.

@saolof
saolof / signals.js
Created April 27, 2024 09:08
This is just deepsignal concatenated on top of usignal with typescript types removed, for the sake of being easily importable with a script tag in personal projects
/* start of usignal */
/* usignal license: https://github.com/WebReflection/usignal/blob/18920ecf613d31b4a8f20f1acc4bcb111a3ab987/LICENSE */
/*! (c) Andrea Giammarchi */
const {is} = Object;
let batches;
@saolof
saolof / rofi-wifi-menu.sh
Last active September 28, 2023 21:56
rofi-wifi-menu-psoix
#!/bin/sh
set -eu
# This is directly inspired by the rofi-wifi-menu script,
# but fixes many of the bugs in it, and makes it POSIX compliant
# so that it can run on distributions that set dash or busybox as
# the default shell.
# Starts a scan of available broadcasting SSIDs
@saolof
saolof / LLL.jl
Last active January 31, 2022 16:33
Numerically stable LLL algorithm using Givens rotations
#
# Loosely based on the paper at https://core.ac.uk/download/pdf/82729885.pdf but with some significant deviation.
#
# This implementation should be robust even for bad inputs with large condition numbers. The reduced basis of a lattice L with basis
# vectors as columns of a matrix M is given by M*lll_unimod(M) .
#
#
# Acceptably optimized. Spends just under 50% of its time in the call to LinearAlgebra.qr for 2000 x 2000 matrices.
# Benchmarks fairly well compared to existing libraries in multiple languages.
@saolof
saolof / rkanren.jl
Last active June 18, 2021 19:27
Quick minikanren implementation that I'm planning to grow into an rkanren with a Dataframe-based datalog-like subset when I will finally have the time
using FunctionalCollections
using MacroTools
using DataFrames
# Data structures designed for this problem
include("persistent_unionfind.jl")
include("pairingheap.jl")
#
# (For datalog-like subset)
@saolof
saolof / pairingheaps_mutable.jl
Last active June 17, 2021 20:28
Mutable pairing heap implementation
#
# Mutable Pairing heap. This version only allocates new nodes when you
# create a heap or add elements to it, so number of allocations == number of nodes. Has O(1) merges.
# Performance is not yet optimized and is much slower than an immutable persistent version I wrote,
# Performance difference may possibly be due to the generational GC making allocation cheaper & more cache friendly,
# and mutating pointers from old to new nodes more expensive than in C or Rust.
#
struct EmptyHeap{T} end
mutable struct PairingTree{T}
@saolof
saolof / pairingheaps.jl
Created June 14, 2021 16:16
Reasonably fast pairing heap implementation
struct EmptyHeap{T} end
struct PairingTree{T}
top::T
length::Int
subheaps::Union{PairingTree{T},Nothing}
tail::Union{PairingTree{T},Nothing} # Intrusive linked list.
end
const PairingHeap{T} = Union{EmptyHeap{T},PairingTree{T}}
Base.show(io::IO,pt::PairingTree{T}) where {T} = show(io,"PairingTree{$(T)}($(pt.top),$(pt.length),$(isnothing(pt.subheaps) ? "empty" : "..." ))")
@saolof
saolof / lifetimes.jl
Created May 26, 2021 18:55
Lexical lifetimes in Julia (simplified prototype)
abstract type GlobalScope end
abstract type _LifeTime{S<:GlobalScope} end
const LifeTime{S<:GlobalScope} = _LifeTime{>:S}
struct LivesExactly{L<:LifeTime,T}
val::T
LivesExactly(lt::Type{L},val::T) where {T,L<:LifeTime} = new{L,T}(val)
end
const LivesAtLeast{L,T} = LivesExactly{<:L,T}
@saolof
saolof / tarjan.jl
Last active April 14, 2021 16:24
Various modern implementations of Tarjans algorithm, for a PR to LightGraphs
using LightGraphs
using SimpleTraits
using BenchmarkTools
using GraphPlot
@traitfn function strongly_connected_components_2(g::AG::IsDirected) where {T <: Integer, AG <: AbstractGraph{T}}
if iszero(nv(g)) return Vector{Vector{T}}() end
strongly_connected_components_tarjan(g,infer_nb_iterstate_type(g))
end
@saolof
saolof / deriv.jl
Last active September 25, 2019 23:17
Quick Julia port of Jon Harrop's benchmark using MLStyle to provide a Match macro (ref: https://gist.github.com/jdh30/f3d90a65a7abc7c9faf5c0299b002db3 )
using MLStyle
import MacroTools
import Printf
@data internal MExpr begin
MInt(Int)
Var(Symbol)
Add(MExpr,MExpr)
Mul(MExpr,MExpr)
Pow(MExpr,MExpr)