WolframLang: Scan (aka Foreach)

By Xah Lee. Date: . Last updated: .

Map but return Null (aka Foreach)

Scan[f, list]

Same as Map, but return Null. 〔see WolframLang: Map Function to List

Scan allows the use of Return and Throw to exit and return a alternative value.

Scan

xx = {a, {{b,c}, d}, e};

(* map to leafs, but as soon as c is found, exit. *)

Scan[
 Function[{x},
  Print[ x ];
   If[x === c, Return[x] ]],
 xx,
 {-1} ]

(* 
a
b
c
Null
 *)

💡 TIP: When to Use Scan

Scan is useful when:

WolframLang Loop, Iteration, Recursion