mbutterick / pollen-users

please use https://forums.matthewbutterick.com/c/typesetting/ instead
https://forums.matthewbutterick.com/c/typesetting/
52 stars 0 forks source link

splitf-txexpr: how to use the resulting txexpr #127

Closed oldmankit closed 2 years ago

oldmankit commented 2 years ago

I'm trying to use the splitf-txexpr function but have run into a problem.

From the example:

(define tx '(div "Wonderful day" (meta "weather" "good") "for a walk"))
(define is-meta? (λ (x) (and (txexpr? x) (equal? 'meta (get-tag x)))))
(splitf-txexpr tx is-meta?)

It returns two lists:

'(div "Wonderful day" "for a walk")
'((meta "weather" "good"))

I want to put the first list through ->html, but trying that gives an arity error. It looks like the splitf-txexpr function returns two lists, and ->html will only accept one argument. Surely this must be easy to solve…

I tried to get this down to simplest terms, with two lists, '(1 2) '(3 4), and figure out how to get back just the first list. This seems to do it:

(first (list '(1 2) '(3 4)))

But when I try that with:

(first (list (splitf-txexpr tx is-meta?)))

again I get arity mismatch.

mbutterick commented 2 years ago

splitf-txexpr returns two values — not one list holding two values — so you need to destructure the result with define-values, let-values et al.

#lang racket
(require txexpr pollen/template/html rackunit)
(define tx '(div "Wonderful day" (meta "weather" "good") "for a walk"))
(define is-meta? (λ (x) (and (txexpr? x) (equal? 'meta (get-tag x)))))
(define-values (new-tx items-removed) (splitf-txexpr tx is-meta?))
(check-equal? (->html new-tx) "<div>Wonderful dayfor a walk</div>")
(check-equal? (length items-removed) 1)
oldmankit commented 2 years ago

Thank you! I hadn't properly understand the concept of a "value".