Skip to content

Instantly share code, notes, and snippets.

View OTDE's full-sized avatar

Seth Chapman OTDE

  • Pacific Northwest
View GitHub Profile
@OTDE
OTDE / code-golfing-rikis-template-engine-in-julia.jl
Created April 29, 2026 01:03
Code-golfing riki's HTML templating engine in Julia
#=
A few days back, I was on lobste.rs the other day and saw a cool post by
riki. The post is about using Lua to write a super-compact HTML generator,
using the table data structure as a base. HTML generators are an infamous
category of programmer tar pit. Regardless of what programming language you
enjoy, if you like to yap (don't lie), then yapping to the internet in your own
special way will usually require you to interact with HTML in some form. While
writing a fully spec-compliant HTML generator in a weekend might be generously
described as a "fool's errand," it's feasible to quickly build something that
will get you _most_ of the way there.
@OTDE
OTDE / html-dsl-code-golf.jl
Created April 29, 2026 00:55
Tiny HTML Julia DSL
const Attribute = Pair{Symbol,<:Any}
const BareElement = Tuple{Symbol,Vararg{Any}}
const HTMLElement = Tuple{Symbol,Union{NamedTuple,Nothing},Vararg{Any}}
const VoidElement = Tuple{Symbol,Union{NamedTuple,Nothing}}
const VoidTags = (:area, :base, :br, :col, :embed, :hr, :img, :input, :link, :meta, :param, :source, :track, :wbr)
escapehtml(text) = replace(text, '&' => "&amp;", '<' => "&lt;", ">" => "&gt;", '"' => "&quot;", ''' => "&#39;")
kebabcase(symbol::Symbol) = replace(string(symbol), '_' => '-')
struct Raw{Character} end
function test_all_pair_constructors()
@testset "Pair constructor tests" begin
log_test_batch_begin("Pair constructor")
@testset "Constructor test set with type: $name" for (type, name) ∈ pair_type_iterable
@testset "Niladic constructor for $name" begin
test_one_pair_constructor(type)
increment_progress()
end
@testset "Monadic constructors for $name" begin
@OTDE
OTDE / dot.jl
Created November 26, 2019 00:05
@inline ⋅(u::VectorLike{N,T}, v::VectorLike{N,U}) where {T<:Number, U<:Number, N} = sum(u .* v)
@OTDE
OTDE / tiny_definition.jl
Created November 25, 2019 23:36
It's two lines!! I could have made it one!!
@inline Base.:+(a::CartesianTuple{N,S,U}, b::CartesianTuple{N,T,U}) where
{S<:Number, T<:Number, U<:ComponentType, N} = promote_type(typeof(a), typeof(b))(a .+ b)
@OTDE
OTDE / component_types.jl
Last active November 25, 2019 23:21
type definitions
abstract type ComponentType end
abstract type VecLike <:ComponentType end
struct Vec <:VecLike end
struct Pnt <:ComponentType end
struct Nrm <:VecLike end
abstract type CartesianTuple{N, T<:Number, U<:ComponentType} <:FieldVector{N, T} end
VectorLike{N, T<:Number} = CartesianTuple{N,T,U} where U<:VecLike
@OTDE
OTDE / main.dart
Created November 8, 2019 01:25
Brute force multiple summation.
void main() {
print(multiplesOf(3) + multiplesOf(5));
}
// Does things the brute-forcey way with a reduce.
int multiplesOf(int multiple) {
var multiples = [for(int i = 1; i <= 1000; i++) i];
multiples.removeWhere((i) => i % multiple != 0);
return multiples.reduce((a, b) => a + b);
}
@OTDE
OTDE / Knapsack.java
Created November 7, 2019 21:27
Final implementation
import java.util.Arrays;
import java.util.List;
public class Knapsack {
private int[][] maxValues;
private List<Item> items;
int maximumValue(int capacity, List<Item> items) {
maxValues = new int[items.size() + 1][capacity + 1];
@OTDE
OTDE / Knapsack.java
Last active November 7, 2019 19:53
Recursive implementation, with nastiness.
import java.util.Arrays;
import java.util.List;
public class Knapsack {
private int[][] valueTable;
private List<Item> items;
int maximumValue(int capacity, List<Item> items) {
valueTable = new int[items.size() + 1][capacity + 1];
@OTDE
OTDE / Knapsack.java
Last active November 7, 2019 19:27
First part of the knapsack
public class Knapsack {
private int[][] valueTable;
private List<Item> items;
int maximumValue(int capacity, List<Item> items) {
valueTable = new int[items.size() + 1][capacity + 1];
this.items = items;
for (int[] row: valueTable) {
Arrays.fill(row, -1);