softmoth / raku-Template-Mustache

Raku library for the Mustache template format
https://modules.raku.org/dist/Template::Mustache:cpan:SOFTMOTH
Artistic License 2.0
21 stars 19 forks source link

Support Iterables #32

Closed vrurg closed 4 years ago

vrurg commented 4 years ago

With this PR it is possible to pass not only Positionals as a key value, but also any Iterable. Especially makes sense for HyperSeq in cases like:

my $seq = gather {
    for ^10000 {
        take $_
    }
}.hyper;

$m.render( $template, %( key => $seq ) );

A real-life example renders 4100 rows SQLite database in 5.5-6sec, comparing to 27-28sec without this patch and hyper. Tested on a 8-core i9 MacBook Pro.

softmoth commented 4 years ago

Thank you for the patch. I definitely agree that accepting Iterable is important.

Can you help me understand, is it sufficient to just change the existing when clause to say when Positional | Iterable? Or does that not work for some reason? I'd prefer to not add another clause with essentially identical code.

Also, it would be nice to add a test case, if there is some specific new behavior here.

vrurg commented 4 years ago

Unless there is something I miss in use of gather for Positional, then both cases could successfully be joined together. The reason I didn't do it in first place was this uncertainty.

In either case, map should be faster than gather/take.

And a test, yes... I'll be back later today with both patches on my hands.

vrurg commented 4 years ago

Ok, done and hopefully ready for merge.

softmoth commented 4 years ago

Thanks again for your help. Sorry that it took so long for me to get back to it!

vrurg commented 4 years ago

No worries, I'm overloaded too. Just asking not to forget to release a new version soon. :)