XPLAN XMERGE CODING
More xmerge .join() function examples
February 6, 2016 | matthew-townsend
The ongoing evolution of sentence and join syntax
Telling a story from the beginning and building up to the more complex journeys is one of my favourite ways to teach and impart knowledge.
This is often essential with XPLAN as working with Xmerge you will likely come across a multitude of different coding styles and ways of achieving a desired result. Just learning the shortest or quickest way won’t necessarily prepare you for recognising, diagnosing and resolving other methods when you encounter them.
In this blog writing syntax into a sentence and grammar has been one of the examples we’ve persistently built upon. Some of the articles where you can see the same coding used to illustrate the different points are:
In that same vein, now that we’ve covered off functions we can add another layer to that same story.
joinList – join() function
There are many areas of XPLAN where things aren’t always going to be displayed and output in a list or client friendly sentence. Areas like this include dependants, asset owners, policy owners, scope of advice, fees..the list goes on.
To highlight this function we’re going to stick with our tried and true dependants example (we want to use our dependants in a sentence) and look at how a custom join() function can make this even easier while providing all the consistency and readability benefits of a function.
<:let joinList = lambda mylist: len(mylist)> 1 and (‘ and ’.join([‘, ’.join(mylist[:-1])] + mylist[-1:])) or ‘’.join(mylist):>
Let’s break it down:
- Our function name is joinList
- We’ve called the throw away variable here ‘mylist’. This will really be whatever list variable you add to the function when using it.
- As you can see, by and large this is just an adaption of the .join() function previously used in this article. However defining it once as a function and using multiple times makes things quicker and easier.
Different options available to call it in the template:
<:let depList=[str(name.dep_name) for name in $client.dependent]:>
# James, Johnny and Stamos
With this option, we’ve defined depList (our list of dependant names) separately to the actual function. There can be a variety of reasons to do this, maybe you’ve created your list via a series of iterated appends or extends. Maybe the list is complex and it would difficult to read to have it defined in a table etc.
<:=joinList([str(name.dep_name) for name in $client.dependent]):>
# James, Johnny and Stamos
The above achieves the same result, just with the actual list declared within the function call. The benefit of this approach is that all the code is held together so you don’t need to worry about variables that might be defined elsewhere and spacing with let statements etc.