Skip to main content
Post Closed as "Duplicate" by MarcoB, m_goldberg, user9660, dr.blochwave, ilian
edited tags
Link
added 154 characters in body
Source Link

What's the cleanest way Does Mathematica have a functional programming idiom to performloop over a do/while in Mathematicalist till a condition is met?

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:Edit to add: The main goal is to look for a functional programming approach to recursivelyrepeatedly loop over the list and run Plus[] till the Integer length for all of the elements is equal to 1.

What's the cleanest way to perform a do/while in Mathematica?

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 recursively loop over the list and run Plus[] till the Integer length is 1.

Does Mathematica have a functional programming idiom to loop over a list till a condition is met?

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.

added 154 characters in body
Source Link

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 recursively loop over the list and run Plus[] till the Integer length is 1.

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?

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 recursively loop over the list and run Plus[] till the Integer length is 1.

Source Link
Loading