antlr / antlr/stringtemplate4

better (monadic) list manipulation

Open
#143 0 comments 0 reactions 0 assignees View on GitHub
type:feature
Dominant language
Java
Stars
1k
Forks
234
PR merge metrics
No merged PRs in 30d

Description

> as a user of string template, I would like a larger set of ~monadic operations on multi-value attributes, so that I am able to easily template strings of list form

I was rather annoyed to find out that if I had a POJO like this:

```
class Whatever{
@Nullable Integer first;
@Nullable Double second;
@Nullable String third;
}
```

and I wanted String Template to generate something like

```
{first: 1 separator second: 2.345 separator third: six-seven}
```

(for the obviously corresponding object),

but simply _omitting_ the `separator` and value for any missing field.

eg, `second` was null:

```
{first: 1 separator third: six-seven}
```

I'm actually left to using a switch:

```
{<\\>
first: separator <\\>
second: separator <\\>
third: <\\>
}
```

which suffers from a nasty (quadratic?) length-complexity. In other words as I add more fields to my object, the string template requires quadratically more text.

I've spent the last hour attempting to solve this with [maps](https://github.com/antlr/stringtemplate4/blob/master/doc/templates.md#map) and [anonymous templates](https://github.com/antlr/stringtemplate4/blob/master/doc/templates.md#anonymous-subtemplates), the closest I've gotten is

```
<[first, second, third], ["first", "second", "third"]:{value, name|<\\>
: }; separator=" separator "><\\>
```

but, in the case where second is null, this gives me

`{ first: 1 separator separator third: six-seven }`
##

Looking at this from a monadic standpoint --because I remember from your book that you guys are huge fans of functional programming-- I think this has to do with the fact that the mapping and anonymous template schemes dont _quite_ provide for a flatmap --which would give me the base tool I need to get this done.

That said, implementing a fold and/or filter would be very helpful too.

Further, in that particular example I'd like to switch from column-major to row-major.

So the two features I want are:
- ability to switch from simaultanous iteration of lists in a set of lists, to the complete enumeration of a list in a set of lists
- ability to specify a filter

```
<[first, "first"], [second, "second"], [third, "third"]:{value, name |<\\>
name: value; if(value); orientation=inner<\\>
}
```

In general:
perhalps a keyword between the colon and the opening curley to specify other monadic operations, (with the 'implicit' operation being map?)

then we can do nifty things like

```
<[first, "first"], [secon, "second"], [third, "third"]<\\>
:orientation(inner)<\\>
:filter{value, name|name}<\\>
:{value, name|: ; separator=" separator "}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.