Hey folks. Thought I’d share typsy, my (whimsically-named) library of Typst tools for programming geeks.
Some highlights:
- classes
- pattern-matching
- enums
- safe counters (no need to find a unique string)
- mutually-recursive functions
Available on Typst universe and GitHub.
What’s in the box? Here are some examples.
Classes:
#import "@preview/typsy:0.1.0": class
#let Adder = class(
fields: (x: int),
methods: (
add: (self, y) => {self.x + y}
)
)
#let add_three = (Adder.new)(x: 3)
#let five = (add_three.add)(2)
Simple pattern-matching:
#import "@preview/typsy:0.1.0": Array, Int, matches
// Fixed-length case.
#matches(Array(Int, Int), (3, 4)) // true
// Variable-length case.
#matches(Array(..Int), (3, 4, 5, "not an int")) // false
Enums + ‘proper’ pattern matching:
#import "@preview/typsy:0.1.0": case, class, enumeration, match
#let Shape = enumeration(
Rectangle: class(fields: (height: int, width: int)),
Circle: class(fields: (radius: int)),
)
#let area(x) = {
match(x,
case(Shape.Rectangle, ()=>{
x.height * x.width
}),
case(Shape.Circle, ()=>{
calc.pi * calc.pow(x.radius, 2)
}),
)
}
Safe counters:
#import "@preview/typsy:0.1.0": safe-counter
#let my-counter1 = safe-counter(()=>{})
#let my-counter2 = safe-counter(()=>{})
// ...these are different counters!
// (All anonymous functions have different identities to the compiler.)
If anyone has any thoughts, let me know