tworld
tworld copied to clipboard
English list constructor
gentext.conjunct(ls, word='and')
Convenience function: given a bunch of strings (or texts), glue them together with commas and "and". Or "or".
This will have to stream (like print()) rather than returning a text. Oh well.
But wait! This is a pain in the butt! The original case (see http://seltani.shoutwiki.com/wiki/Act_on_Another_Player) is a list of links. Each entry is a set of link(), print(), endlink() calls. We can't do that without generators or lambdas.
I guess we could pass in a list of texts constructed like text('['+name+'|act("'+str(key)+'")]')
. I think that works, but it's ugly and fragile (escaping issues in the name and key).
Maybe a form gentext.conjunct(ls, key='prop')
which calls prop(val) on every element of ls. This is a terrible idea.
Less terrible: a generator.
conj = gentext.conjunct(ls) # or len(ls)
for obj in ls:
print(obj) # or whatever
print(conj())
Already, a Seltani user could do markup concatenation, right? Build a list of strings containing well-balanced markup, pass it in, and get back a string containing well-balanced markup. Then pass that to text(_)
.
You can do that, but it's error-prone -- you have to escape square brackets in your strings. Anyhow I think it's more effort than the naive solution of a messy if statement in your loop.
How about a generator that yields both the object and the separator, so you can go:
for obj, conj in gentext.conjunct(ls):
print(obj) # or whatever
print(conj)
That's nice, but I think it should be covered by zip().