[PATCH] templater: add intersperse() template function

Martin von Zweigbergk martinvonz at google.com
Tue May 3 16:19:52 EDT 2016


On Tue, May 3, 2016 at 1:12 PM, Matt Mackall <mpm at selenic.com> wrote:
> On Tue, 2016-05-03 at 09:58 -0700, Martin von Zweigbergk via Mercurial-devel
> wrote:
>> # HG changeset patch
>> # User Martin von Zweigbergk <martinvonz at google.com>
>> # Date 1462294194 25200
>> #      Tue May 03 09:49:54 2016 -0700
>> # Branch stable
>> # Node ID d88e385e84ca345474c177932e605c79a487a8d4
>> # Parent  61463b8fcef871ff3efff53945df4671c71c4e31
>> templater: add intersperse() template function
>>
>> A pretty common pattern in templates is adding conditional separators
>> like so:
>>
>>   {node}{if(bookmarks, " {bookmarks}")}{if(tags, " {tags}")}
>>
>> With this patch, the above can be simplified to:
>>
>>   {intersperse(" ", node, bookmarks, tags)}
>
> This is very similar to the existing join function:
>
>  join(list, sep)
>
> It's unfortunate that we didn't make that:
>
>  join(sep, list)
>
> Because then it would be natural to have a second form of join:
>
>  join(sep, a, b, c, ..)
>
> However, the current behavior of join doesn't notice empty args (it works like
> Python's join).
>
> So how would you feel about a new function separate():
>
>  separate(sep, list) -> put a separator between non-empty elements of list
>  separate(sep, a, b, c...) -> put a separator between non-empty arguments

Both new name and list support sounds fine to me. I'll prepare a V2.


More information about the Mercurial-devel mailing list