Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #360 Create a formatted list

Create the end-user text, s, specifying the contents of list a.

For example,
— 'A', 'A and B', 'A, B, and C'

class List:
    def __init__(self, /, sep=', '):
        self.d, self.c = sep, dict()
    def apply(self, /, *x, prefix='', suffix=''):
        self.c[*x] = prefix, suffix
    def parse(self, a):
        a = [*map(str, a)]
        for k, (p, s) in self.c.items():
            for i in k: a[i] = p + a[i] + s
        return self.d.join(a)
x = List()
x.apply(-1, prefix='and ')
s = x.parse(a)
def f(a, sep=', ', con=' and '):
    *x, s = map(str, a)
    return sep.join(x) + con + s if x else s
sysutils
  if Length(a) > 1 then
  begin
    s := String.Join(', ', a, 0, Length(a)-1);
    s := s + ' and ' + a[High(a)]
  end
  else
  begin
    if Length(a) > 0 then
      s := a[0]
    else
      s :='';
  end;

New implementation...
< >
reilas