I find that I often need to treat the last element in the sequence in a special way when iterating it. For example, take the built-in function interpose
:
> (interpose ", " (range 4))
(0 ", " 1 ", " 2 ", " 3)
One way of thinking about this is:
- Take the first element, add ", "
- Take the second element, add ", "
- Take the third element, add ", "
- ...
- Take the second to last element, add ", "
- Take the last element and do nothing
I also find that I need to do something special when building a Mig layout using Seesaw. For example, let's say I want to build this:
+---+---+---+
| 1 | 2 | 3 |
+---+---+---+
| 4 | 5 | 6 |
+---+---+---+
| 7 | 8 | 9 |
+---+---+---+
where each of the numbers is some component (e.g. a button). I can do this by making a Mig layout constraint of the whole panel "flow" and then add the following constraints to each of the components:
- "grow" - components 1, 2, 4, 5, 7, 8, 9
- "grow, wrap" - components 3, 6
Note that the above can be said like this:
- "grow" for each component except the last component in the row
- "grow, wrap" for all other components except the last component in the last row
where again the same "special last element" theme appears.
So two questions:
- Does the above reasoning make sense? I.e. should I change the design fundamentally and think about this in a different way given the above sample problems and the general theme?
- If not, is there an idiomatic and short way to do this?
Yes, you can make helper functions and macros, but I find this often enough that I am inclined to think that it should be one of the above. In other words - do you run into the same types of "problems" and how do you solve them?