Xplan Xmerge Coding – Functions: join() example

Xplan Quick Xplain

More Function examples:
Join

‘additional ‘.join(functions continued)

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 our favourite ways to teach and impart knowledge. 

This is often essential with Xplan where working with Xmerge you will likely come across a multitude of different coding styles and ways of achieving a 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 used to build this up.  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 in a list or flat sequence.  This can range from owners of products and policies where they can multiple owners, dependants and when you want to use them in a sentence, scope of advice, disclaimers, products.  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[0]):>

Let’s break it down (colour coded)

  • Our function name is joinList
  • We’ve called the throw away 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() we used in this article.  However defining it once as a function and using multiple times makes things quicker and easier.

To call it in the template:

option 1:

<:let depList=[str(name.dep_name) for name in $client.dependent]:>
<:=joinList(deplist):>
# 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.

option 2:

<:=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 option is 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.

Conclusion

This is a great example of how you can take knowledge from all sorts of things you might repeat quite a bit and wrap it up into a function.  You need to use balance so that you aren’t needlessly creating functions but for common things they do deliver on reducing your coding, increasing consistency and often making things more readable.

Matthew Townsend
Xplan Consultant and Developer | Wealth Management Technologist at Create Something
Matthew is an experienced and innovative Xplan consultant and developer, having worked on and developed some of the largest advice projects in the industry. Passionate about building great experiences in xplan that enable businesses and clients to get the most out of this powerful software.

Matthew Townsend

Matthew is an experienced and innovative Xplan consultant and developer, having worked on and developed some of the largest advice projects in the industry. Passionate about building great experiences in xplan that enable businesses and clients to get the most out of this powerful software.

Leave a Reply

Your email address will not be published. Required fields are marked *