💾 Archived View for idiomdrottning.org › matchable-custom-destructuring captured on 2023-01-29 at 03:45:12. Gemini links have been rewritten to link to archived content

View Raw

More Information

➡️ Next capture (2023-03-20)

-=-=-=-=-=-=-

Matchable’s custom destructing

Easily the two features I use the most in Matchable (which I mostly use through my match generics) are ? and =, the custom predicate and the custom accessor respectively.

(Those match generics, probably better known as the fanciest define of all time.)

I’ve found that the ?-> combinator is a great match for = when you conditionally want to coerce a data type into another type.

Another comfy use for = is with a custom destructurer that you can then further destructure with matchable. You can just keep on nesting = procs deep in there at every layer.

Here’s a simple example.

This is from the SVG path lib from earlier today. The context is a procure named distance that takes two attribute alists, like ((coords (x n) (y n))), and calculates the Pythagorean diagonal distance between them.

Here is the custom destructurer, which in turn uses alist-ref in its own destructuring parameter list to grab ((x n) (y n)) and then returns a list of those just two numbers.

(define (get-coords (= (c alist-ref 'coords) atts))
  (list (car (alist-ref 'x atts))
    (car (alist-ref 'y atts))))

And here’s how that destructurer is used in practice, twice, in order to bind the values in those lists directly.

(define (distance (= get-coords (x1 y1)) (= get-coords (x2 y2)))
  (hypothenuse (- x1 x2) (- y1 y2)))

Using = first and then furthering destructuring the value with another pattern might not have been intended (since it was created primarily for record access) but it’s so flexible and it, along with ?, is an easy way for end users to extend Matchable.

Matchable is awesome and I'm so grateful to Andrew Wright and Alex Shinn for putting it together.

?-> A conditionally transforming combinator

An SVG path data library for Chicken Scheme

define vs let