Skip to main content
deleted 1 character in body
Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47

The general solution for using different heads and levels:

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[ListMap[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

The general solution for using different heads and levels:

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

The general solution for using different heads and levels:

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

deleted 62 characters in body
Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47

The general solution for using different heads and levels (you can't use x symbol in fn unless you modify the code):

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

The general solution for using different heads and levels (you can't use x symbol in fn unless you modify the code):

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

The general solution for using different heads and levels:

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

added 62 characters in body
Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47

The general solution for using different heads and levels (you can't use x symbol in fn unless you modify the code):

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

The general solution for using different heads and levels:

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

The general solution for using different heads and levels (you can't use x symbol in fn unless you modify the code):

customMap[fn_, arr_, heads_List, level_] := 
 Replace[arr, Alternatives @@ (Pattern[x, Blank[#]] & /@ heads) -> fn[x], level]

customMap[(# + 3) &, {a, 3 b, {c, d}, e}, {List}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 1

Using customMap:

ListMap[fn_, arr_, level_] := customMap[fn, arr, {List}, level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

Solution 2

Using Replace:

ListMap[fn_, arr_, level_] := Replace[arr, x_List -> fn[x], level]

ListMap[(# + 3) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {3 + c, 3 + d}, e} *)

If you want to apply to all elements of a list at a specific level:

ListMap[fn_, arr_, level_] := Replace[arr, x_List :> fn /@ x, level]


ListMap4[{#, x} &, {a, b, c}, {0}]

(*Out: {{a, x}, {b, x}, {c, x}} *)


ListMap[(# + 7) &, {a, 3 b, {c, d}, e}, {1}]

(*Out: {a, 3 b, {7 + c, 7 + d}, e} *)

Also, levels start at 0, you can change the function definition but I think it's better to be consistent with other functions.

added 461 characters in body
Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47
Loading
Source Link
Ben Izd
  • 9.5k
  • 1
  • 15
  • 47
Loading