The module below takes a list of integers and performs fadic addition on it (if a bit slowly).
f[l_] := Module[{result = l},
While[True,
result = Plus@@@IntegerDigits[result];
ForEach[e_, result,
If[IntegerLength[e] > 1, Continue[]]
];
Break[];
];
result
]
The ForEach is defined as:
SetAttributes[ForEach, HoldAll];
ForEach[pat_, lst_, bod_] :=
ReleaseHold[Hold[Cases[Evaluate@lst, pat :> bod];]];
Example output:
threes = Table[3*x, {x, 1, 30}]
{3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, \
51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90}
f[threes]
{3, 6, 9, 3, 6, 9, 3, 6, 9, 3, 6, 9, 3, 6, 9, 3, \
6, 9, 3, 6, 9, 3, 6, 9, 3, 6, 9, 3, 6, 9}
Since Mathematica doesn't have a do/while construct I have to manually set the loop condition true upon entry and put a test at the bottom of the loop to determine whether we continue or break. In all, using While[] adds an extra line or two depending on how you decide to format the function as compared to just using a goto.
f[l_] := Module[{result = l},
Label[begin];
result = Plus@@@IntegerDigits[result];
ForEach[e_, result,
If[IntegerLength[e] > 1, Goto[begin]]
];
result
]
Even though it works I can't help but think there has to be a better way to break out of and exit the first loop. I'm curious if there is a cleaner way to do this in Mathematica or is this as good as it gets with the current grammar?
Edit to add: The main goal is to look for a functional programming approach to repeatedly loop over the list and run Plus[] till the Integer length for all of the elements is equal to 1.
f[TakeWhile[myList,myCriteria]]
wheref
is listable goes a long way to answering the title of your question, if not particularly efficiently the exact task you have. $\endgroup$