Skip to content

Instantly share code, notes, and snippets.

@chiroptical
Created June 16, 2024 00:49
Show Gist options
  • Save chiroptical/e1932eb80ae4897a30c2fbb28a7583c2 to your computer and use it in GitHub Desktop.
Save chiroptical/e1932eb80ae4897a30c2fbb28a7583c2 to your computer and use it in GitHub Desktop.
Upstar Profunctor instance in Gleam
// dimap :: (a -> b) -> (c -> d) -> p b c -> p a d
// p b c -> c b
// p a d -> d a
pub type Profunctor(p, q, a, b, c, d) {
Profunctor(dimap: fn(#(fn(a) -> b, fn(c) -> d, p)) -> q)
}
pub type Upstar(f, a) {
Upstar(run: fn(a) -> f)
}
pub fn upstar_profunctor() -> Profunctor(Upstar(c, b), Upstar(d, a), a, b, c, d) {
let dimap = fn(inp: #(fn(a) -> b, fn(c) -> d, Upstar(c, b))) {
let f = inp.0
let g = inp.1
let Upstar(run) = inp.2
Upstar(run: fn(x) { g(run(f(x))) })
}
Profunctor(dimap)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment