mbutterick / txexpr Goto Github PK
View Code? Open in Web Editor NEWmoved to https://git.matthewbutterick.com/mbutterick/txexpr
Home Page: https://git.matthewbutterick.com/mbutterick/txexpr
License: MIT License
moved to https://git.matthewbutterick.com/mbutterick/txexpr
Home Page: https://git.matthewbutterick.com/mbutterick/txexpr
License: MIT License
The xml library provides a contract called xexpr/c
, which gives a better error message than xexpr?
when passed in a non-X-expression:
> (define/contract not-an-xexpr xexpr/c '(b "hello " ("world") 42))
; not-an-xexpr: broke its own contract;
; Not an Xexpr. Expected a symbol as the element name, given "world"
; Context:
; '(b "hello " ("world") 42)
; in: xexpr/c
; contract from: (definition not-an-xexpr)
; blaming: (definition not-an-xexpr)
; (assuming the contract is correct)
; at: readline-input:14:17
; [,bt for context]
I'd like to see such contracts for the txexpr-*?
predicates. If making one for all of them is too hard, then I'd be satisfied with just seeing txexpr/c
.
Currently only strings are valid attribute values for a tagged x-expression. Would it make sense to support non-serialized boolean values in attributes, given that boolean attributes are a thing in XML/HTML5?
I understand how to work around it for now. But serializing #t
and #f
into strings on my own, and remembering to omit "false" attributes from x-expressions destined for HTML output, feels like I'm doing something that belongs in the library.
@AlexKnauth
@zyrolasting
@samth
@LeifAndersen
Consistent with Racket’s overall relicensing from LGPL to Apache+MIT, I’d like to move txexpr
from the LGPL to MIT.
Because the MIT license is more permissive, this requires the assent of you, the esteemed contributors.
If you agree, please post a message saying so, e.g. — "I hereby relicense my contributions to txexpr
under the MIT license."
On January 1, if you have not agreed to the relicensing, I will remove your code. Thanks.
I am a fan of match
, but I do agree with your points at https://docs.racket-lang.org/txexpr/index.html#%28part._.Why_not_just_use_match__quasiquote__and_so_on_%29. However, there's a middle ground: add a new match pattern to support txexpr
.
#lang racket
(require txexpr)
(define-match-expander txexpr
(lambda (stx)
(syntax-case stx ()
[(_ tag attrs elems)
#'(? txexpr?
(app txexpr->list (list tag attrs elems)))])))
(match '(p ([class "foo"]) "hello" "world")
[(txexpr t a e)
(println t)
(println a)
(println e)])
Would you be interested in adding this to the library?
See the subject line.
Would this function be an appropriate addition to this package?
https://github.com/sanchom/sanchom.github.io/blob/master-source/util.rkt
(define/contract (merge-successive-strings elements)
(txexpr-elements? . -> . txexpr-elements?)
(define (conditional-merge element current-list)
(if (and (not (empty? current-list))
(string? (first current-list))
(string? element))
(cons (string-append (first current-list) element) (drop current-list 1))
(cons element current-list)))
(reverse (foldl conditional-merge '() elements)))
(module+ test
(check-equal? (merge-successive-strings '()) '())
(check-equal? (merge-successive-strings '("a")) '("a"))
(check-equal? (merge-successive-strings '(a)) '(a))
(check-equal? (merge-successive-strings '(a "b")) '(a "b"))
(check-equal? (merge-successive-strings '("a" "b")) '("ab"))
(check-equal? (merge-successive-strings '("a" a "b")) '("a" a "b"))
(check-equal? (merge-successive-strings '("a" "b" " " "c")) '("ab c"))
(check-equal? (merge-successive-strings '("a" "b" a " " b "c" c d "e" " f" g)) '("ab" a " " b "c" c d "e f" g))
)
Splices rest argument into new txexpr as list of elements.
Should there be a match pattern for txexpr*
?
If so, should the match-expander look like this, or?
(txexpr* tag) = (txexpr tag _ '())
(txexpr* tag attrs) = (txexpr tag attrs '())
(txexpr* tag attrs elem ...) = (txexpr tag attrs (list elem ...))
The weird things are:
If an optional-pattern isn't supplied, should it be filled in with _
, or the pattern for the default argument? Concretely, if attrs
isn't supplied should it be _
or '()
? For now I'm leaning towards _
, because that's common for match-pattern forms like ?
, regexp
, and struct*
.
If a sequence-pattern is empty (which looks the same as not being supplied), should it be filled in with _
, or with the empty sequence? I think it should be the empty sequence, because otherwise it would be hard or unintuitive to express that it must be empty.
However, if someone thinks about optional and sequence arguments as similar, this might seem inconsistent. An unsupplied optional argument looks the same as an empty sequence argument. So, they might think it's weird that if they don't supply any attributes pattern anything goes, but if they don't supply any elements patterns it must be empty.
I'm also unsure with the optional-ness of the attributes pattern because I'm somewhat afraid that someone might try to match with (txexpr* tag elem ...)
when they should use (txexpr* tag _ elem ...)
.
> (get-tag "oi")
; car: contract violation
; expected: pair?
; given: "oi"
from my understanding, the define+provide+safe
macro should be exporting get-tag
with a contract that would raise an error blaming my call to get-tag
not the hidden call to car
. am I doing something wrong? or is this a bug in the sugar
library? if so, the only way I know of fixing this is by using regular constructs like define/contract
, hence the absence of an accompanying PR!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.