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

Closed
opened 2 years ago by oldmankit · 2 comments
oldmankit commented 2 years ago (Migrated from github.com)

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.

I'm trying to use the [splitf-txexpr](https://plt.cs.northwestern.edu/pkg-build/doc/txexpr/index.html#%28def._%28%28lib._txexpr%2Fmain..rkt%29._splitf-txexpr%29%29) 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 (Migrated from github.com)

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)
`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. ```racket #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 (Migrated from github.com)

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

Thank you! I hadn't properly understand the concept of a "value".
This repo is archived. You cannot comment on issues.
No Milestone
No project
No Assignees
1 Participants
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: mbutterick/pollen-users#127
Loading…
There is no content yet.