by throwaway4good · 4y ago · 0 comments
For example, this piece of code is very short and packs a lot of functionality:
path*3 {
fill: none;
d: @pn(M 5 10 Q 5 5 8 1,
M 5 10 Q 4 5 2 4,
M 5 10 Q 6 6 8 4);
}
circle*3 {
fill: @pn(yellow, blueviolet, deeppink);
cx, cy: @pn(8 1, 2 4, 8 4);
r: .5;
}
But unless you know it by hand. The constructs @pn(...), *3, the codes 8 1, 2 4 ... or M 5 10 Q 5 5 8 1. Are not remotely obvious or intuitive. And it is not obvious what M and Q means? And what can I as programmer write instead?I realize that some of these problems comes from SVG however they are problems none the less.
I’d agree that pn seems a little obtuse, but I guess I can see what it’s doing.
Having worked with svg a lot (we build something sort of in the cad space) I can definitely see the desire for something less terse when you’re defining svgs. We end up generating svg fragments using react, for the most part, which works fairly well.
But it is really good way to program? Or is something that is done as a compromise between being able to program by hand, and have a format for drawing programs to save their files in without them being silly big.
For comparison here's a sample of a graphics stream from a PDF:
Q
9469 1441 m
9549 1433 l
9549 1433 9549 1483 9469 1441 c
S
q
In this context Q means push/save the graphics state and q is pop, but again, it's all very similar.is there a point in saving one or two letters on each command? Shld w ll g bck t th gd ld dys f C nd drp rndm lttrs?
An alternative is to have two representations; a binary format to be used by computers and a textual format for humans (programmers) designed for readability, maintainabilty etc.
You might think you want that, what you actually want is to take an index card and write the SVG path abbreviations next to their long names, and put it by your keyboard while you work.
Won't need it for long.
But a more verbose version of e.g. the arc-a? Not interested at all: those seven parameters (+2 from the implicit cursor) would not be particularly palatable even if they were all explicitly named.
I'm working on a fork of it which allows classes to be added, and turns labels into <g> elements with an id attribute.
Pretty great as-is, does more-or-less exactly what you want.
const color = ["yellow", "blueviolet", "deeppink"];
const path = ["M 5 10 Q 5 5 8 1", "M 5 10 Q 4 5 2 4", "M 5 10 Q 6 6 8 4"];
const endPoint = [[8, 1], [2, 4], [8, 4]];
const BallPath = ({ n }) => (
<>
<path fill="none" d={path[n]} />
<circle fill={color[n]} cx={endPoint[n][0]} cy={endPoint[n][1]} r={0.5} />
</>
);
export default () => (
<svg viewBox="0 0 10 10" stroke="#000" strokeWidth=".1">
{[0, 1, 2].map((n) => (<BallPath n={n} />))}
</svg>
);
React yay. Combine with TypeScript for best results. const M = [
{ c:"yellow", d:"M 5 10 Q 5 5 8 1", cx:8, cy:1 },
{ c:"blueviolet", d:"M 5 10 Q 4 5 2 4", cx:2, cy:4 },
{ c:"deeppink", d:"M 5 10 Q 6 6 8 4", cx:8, cy:4 }
];
export default () => <svg viewBox="0 0 10 10" stroke="#000" strokeWidth=".1">
{M.map(({c,d,cx,cy}) => <><path fill="none" d={d} /><circle fill={c} cx={cx} cy={cy} r={0.5} /></>)}
</svg>
seems like as M.map's closure gets bigger, it's already in a good shape for lifting into one or more helpers.Because 1. in the original, they were separate as well, 2. the path is derivative of the endpoint 3. you might want to cycle the colours or they're a pure derivative calculation based of the number of points or whatnot. So combining them like that is definitely prettier and makes sense if the requirements don't change, but also seems like a premature optimisation. (also, forgot about `key`)
Oh I didn't notice that from the example. Is it worth splitting off the last two parts for that?
> you might want to cycle the colours or they're a pure derivative calculation based of the number of points or whatnot
Simply lift color out and use the second argument to map's closure to index it modulo the length of color.
If you really anticipate this, you should have %X.length everywhere, otherwise it just looks like a bug the next guy is going to hit and blame you for it. The problem (as I see it) stems from the hardcoded [0,1,2] iota -- if there's a better (domain-specific) way to eliminate it, then great, but it'll have to consider the rank of M's constituents and I just don't think the juice is worth the squeeze.
> combining them like that is definitely prettier and makes sense if the requirements don't change, but also seems like a premature optimisation
I would urge you to reconsider.
Trying to guess what the next person is going to want to do tends to create more code, not less, and changing a little code is better than changing a lot.
https://hnhiring.com/search?q=hiccup "0 jobs found"
https://hnhiring.com/search?q=Clojure give seven hits, and I doubt HN is in the top five ways Clojurians find employment.
I too have a pet DSL project for graphs. Think JSON, QML, etc.
A central design problem is divining a single syntax for both "groves" (nested objects decorated with key/value pairs) and tabular data.
Asked another way: How would you inline CSV snippets within JSON?
Parenthesis are used for function parameters.
Curly brackets are used for composition: parent { child { key value, key value } }
Square brackets are used for arrays: parent { array [1 2 3] }}
How to simply encode tables (matrices)? Nested arrays? Like: table [ [1 2 3] [4 5 6] ]
Is there another character or lexeme to use?
Maybe delimit rows with pipe char? Like [ 1 2 3 | 4 5 6 ]
Lua got this right, most other languages didn't.
It's an anti-feature, especially from a data modeling perspective. Treating indices as implicit is good, pretending integer indices are special, and must be separated from all other sorts of mapping, is not.
Consider that Lua can express XML without making up a fake key for children:
{tag = xml, attr = {...}, child1, child2, child3}In my hobby DSL, those inlined childX are syntactic sugar for parent{ children=[child1, child2, child3]}.
More generally, I remain uncomfortably undecided about named params (pairs) and indexed params. Esp wrt constructors.
IIRC, I spotted a (hobby?) language that permitted both indexed params (matched in order per method signature) and named params, plus default values. So given "method( int a=10, int b=20)" you could invoke as "method( 1, 2 )", out of order like "method( b: 2, a: 1)" or partially like "method( b: 2 )".
Riffing a bit, this could then allow passing a map of params to a method. Code construction wise, that looks like passing around a context object.
I'll look at Lua again. (It's been a minute.) Thanks.
And as others have said, I knew the M .. Q stuff was SVG anyhow (not that I know it off the top of my head) ... generally the syntax seems nice TBH
I think this depends a lot on who the syntax is tailored towards. This compact stuff reminds me of APL and aspects of Perl. There's nothing wrong with highly specialised brevity. I personally love this syntax, coming from a place of moderate SVG knowledge and CSS fluency. Looks really cool and quick for prototypes. And even the example you posted feels mostly parseable on the first few scans. Nothing insurmountable, and in tune with most other news syntaxes and abstractions in this space tbh.
It's the primitive, everything else is based on it.
It could be made longer, but then a week later you would want it to be shorter, which it already is.
I believe you mean affordance.
Once my SVG file is full of 6.829347908723890's, who cares if it is an M or a Move in terms of being human-writable? And being a vector-based format, there is indeed a real difference between 6.83 and 6.829347908723890 so there isn't a great escape from numbers that a human will find excessively precise. Honestly even the two digits is already getting up there when you see it in a big table with a thousand other numbers.
XML is verbose enough to add significant verbosity to the SVG file even in the presence of floating point numbers rendered as strings, but these sort of cute little "M 5 10 Q 5 5 8 1," go too far the other way. SVG files don't look like that.
SVGs aren't human-writable at any sort of scale anyhow. (Here, "at scale" means in the general case. Sure, you can construct an SVG by hand if you like, but that will never be the way they are primarily generated. They will be primarily generated in tools.) If you conclude from that that using a serialization format optimized for human readability was sort of silly... I agree! But it isn't XML's verbosity making SVGs human unwritable at scale, it's the problem space itself. Nobody wants to be typing in all these numbers. Even if you reduce the cost of the non-number stuff to literally zero and stipulate literal mind reading from the rest of the spec, it would still be impractical to hand-write SVGs in the general case.