treating last item of a range as a special case in a template

1,753 views
Skip to first unread message

Douglas Fils

unread,
Mar 13, 2012, 9:07:38 AM3/13/12
to golan...@googlegroups.com
I am trying to build a SPARQL query with text/template and need a filter line like:

FILTER (?test = iodpal:DEPTH || ?test = iodpal:PRES )


 It is easy to get  "a or b or c or"  but what I want is "a or b or c"
 
Is there a way with the range index or some other way to get the last time through a slice treated different in template?  Perhaps this considered logic and has to be done external to the template and passed in?  I've tried to resolve ways with nesting or the like to get this to work but can not.  

Thanks
Doug

roger peppe

unread,
Mar 13, 2012, 9:21:57 AM3/13/12
to Douglas Fils, golan...@googlegroups.com

chris dollin

unread,
Mar 13, 2012, 9:31:32 AM3/13/12
to roger peppe, Douglas Fils, golan...@googlegroups.com
On 13 March 2012 13:21, roger peppe <rogp...@gmail.com> wrote:
> http://play.golang.org/p/hwm1AcBBRe

I prefer:

http://play.golang.org/p/hwm1AcBBRe

Chris

I do like the playground!

--
Chris "allusive" Dollin

Douglas Fils

unread,
Mar 13, 2012, 9:41:30 AM3/13/12
to golan...@googlegroups.com
Chris,
  I think you resent the same URL rog did.  Did you mean to re-share and get a new URL/example?

  Regardless..   
   a) Thank you BOTH for your time!   Great community, which is always vital to a language
   b) The playground is an amazing utility for forums and passing around code bits..   

Thanks
Doug

Graham Miller

unread,
Mar 13, 2012, 9:55:36 AM3/13/12
to Douglas Fils, golan...@googlegroups.com
Given how awkward that is, do you think it would make sense to change the "range" action to allow an optional "join" clause?
Something like:

{{range .Slice}}{{.}}{{join}} or {{end}}

Seems like it would be useful, and it's a heck of a lot more readable.

graham

chris dollin

unread,
Mar 13, 2012, 9:57:44 AM3/13/12
to Douglas Fils, golan...@googlegroups.com
On 13 March 2012 13:41, Douglas Fils <drf...@gmail.com> wrote:
> Chris,
>   I think you resent the same URL rog did.  Did you mean to re-share and get
> a new URL/example?

Oh bother, yes.

http://play.golang.org/p/Xy-ZpBCbBp

Chris

--
Chris "allusive" Dollin

Peter Bourgon

unread,
Mar 13, 2012, 10:01:42 AM3/13/12
to chris dollin, Douglas Fils, golan...@googlegroups.com
> {{range $i, $v := .}}{{if $i}} or {{end}}{{$v}}{{end}}

Nice.

Paddy Foran

unread,
Mar 13, 2012, 10:41:54 AM3/13/12
to Peter Bourgon, chris dollin, Douglas Fils, golan...@googlegroups.com
My significantly less elegant solution: http://play.golang.org/p/SEeRSMPhbl

It does have the benefit of demonstrating functions, which I think are kind of necessary to using the template package in any involved capacity. It is also easy to modify for a variety of situations (checking if the current element in the array is equal to a certain value, for instance).

Feel free to point out the gaping flaws in my solution.

roger peppe

unread,
Mar 13, 2012, 12:21:25 PM3/13/12
to Paddy Foran, Peter Bourgon, chris dollin, Douglas Fils, golan...@googlegroups.com
On 13 March 2012 14:41, Paddy Foran <foran...@gmail.com> wrote:
> My significantly less elegant solution: http://play.golang.org/p/SEeRSMPhbl

you can slightly simplify your function, BTW
where you've got: func(index, seq interface{}) bool
you could use: func(index int, seq interface{}) bool

here's another solution that i find amusing,
and invites a whole host of potentially interesting
variations, i think:

http://play.golang.org/p/UaSA8jBrUx

Reply all
Reply to author
Forward
0 new messages