erkyrath / tworld

A choice-based shared online text environment sandbox
MIT License
71 stars 13 forks source link

English list constructor #177

Open erkyrath opened 9 years ago

erkyrath commented 9 years ago
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.

erkyrath commented 9 years ago

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).

erkyrath commented 9 years ago

Maybe a form gentext.conjunct(ls, key='prop') which calls prop(val) on every element of ls. This is a terrible idea.

erkyrath commented 9 years ago

Less terrible: a generator.

conj = gentext.conjunct(ls)  # or len(ls)
for obj in ls:
    print(obj)  # or whatever
    print(conj())
rocketnia commented 9 years ago

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(_).

erkyrath commented 9 years ago

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.

FinnStokes commented 9 years ago

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)
erkyrath commented 9 years ago

That's nice, but I think it should be covered by zip().