This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #= | |
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, '&' => "&", '<' => "<", ">" => ">", '"' => """, ''' => "'") | |
| kebabcase(symbol::Symbol) = replace(string(symbol), '_' => '-') | |
| struct Raw{Character} end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @inline ⋅(u::VectorLike{N,T}, v::VectorLike{N,U}) where {T<:Number, U<:Number, N} = sum(u .* v) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
NewerOlder