List operation Tail in Mx5.16

2
Is it me or is the new list operation tail a bit unlogical? The head operation gives me as expected the first object of a list. The tail operation gives me the rest of the list. Not what I would expect because I would have expected the last object of that list. Example, lets say I have list of 100 objects then the head operation gives me the first object and the tail operation gives me a list of the other 99 objects. But this can easily be achieved by substracting the head from my original list so I would not need a special operation for that. I see far more use cases when I want to have the last object of my list. Again it me be just me that find this new operation not what I would have expected. Regards, Ronald [EDIT] Thanks for the clarification. I had a dog in mind :) When working with lists I often use the head and last and almost never use the tail and init.
asked
3 answers
9

This is the common behavior of head and tail as implemented in many languages.

alt text

(courtesy of http://learnyouahaskell.com/starting-out)

What you are suggesting is the behavior of the combination of init and last which we chose not to implement here. The main use case for this feature would be the head option, though tail has many uses in functional languages here it's mostly added for completeness and ease of use.

Note that by removing the element entirely by subtracting head from the list, it would remove the instance of that object everywhere in the list if it was in there multiple times, so your way of doing it has slightly different results.

If you do find a good use case for init and last then it would be fairly easy to implement at this point but we didn't want to clog the list of operations too much.

answered
0

And a tail would be a lot faster aka better performance than a subtract on a large list

answered
0

Ronald,

Although I agree with Sebastiaan on the correct functioning of the tail function, it's quite easy to implement the function as a reusable java action:

  • Create a java action that has a type parameter named ListOfMendixObjects
  • On the General tab add a parameter of type List and select the type parameter ListOfMendixObjects
  • As return type select the Object type and again select the type parameter ListOfMendixObjects
  • Add the following java code between the Begin en End User Code:
		// BEGIN USER CODE
		if(mendixObjectList.size() > 0){
			return mendixObjectList.get(mendixObjectList.size()-1);
		}
		else{
			return null;
		}
		// END USER CODE

Now  you have an action that will take any list of objects and returns 1 object that has the same type as the type of objects in the list. In the image below I used a list of objects that I retrieved from the db (someEntityList) and the java action will return 1 object of the someEntity type.

 

Hope this helps when in need of the last part of the tail!

 

answered