Skip to main content
added 648 characters in body
Source Link
ubpdqn
  • 64.8k
  • 3
  • 65
  • 154

Just to add to the variety:

fun[list_, pos_, colors_] :=
 Module[
  {styles},
  styles = Function[{x}, Style[x, #]] & /@ colors;
  list /. 
   Flatten[Thread /@ 
     MapThread[#1 -> #2 /@ #1 &, {list[[#]] & /@ pos, styles}]]
  ]

Then

fun[list, {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {Red, Green, Blue}]

yields the same result.

EDIT

Noting Mr. Wizard's correction and mainly as (my own) learning opportunity the following is essentially one of Mr. Wizard's approaches slightly different but mapping functions to positions not elements.

func[list_, pos_, col_] :=
 Module[
  {l, el, styles, rules},
  l = Length[list];
  styles = Function[{x}, Style[x, #]] & /@ col;
  rules = Flatten[Thread /@ MapThread[Rule[#1, #2] &, {pos, styles}]];
  el = ReplacePart[Table[Identity, {l}], rules];
  MapThread[Compose, {el, list}]
  ]

Then

func[list, {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {Red, Green, Blue}]

works

Just to add to the variety:

fun[list_, pos_, colors_] :=
 Module[
  {styles},
  styles = Function[{x}, Style[x, #]] & /@ colors;
  list /. 
   Flatten[Thread /@ 
     MapThread[#1 -> #2 /@ #1 &, {list[[#]] & /@ pos, styles}]]
  ]

Then

fun[list, {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {Red, Green, Blue}]

yields the same result.

Just to add to the variety:

fun[list_, pos_, colors_] :=
 Module[
  {styles},
  styles = Function[{x}, Style[x, #]] & /@ colors;
  list /. 
   Flatten[Thread /@ 
     MapThread[#1 -> #2 /@ #1 &, {list[[#]] & /@ pos, styles}]]
  ]

Then

fun[list, {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {Red, Green, Blue}]

yields the same result.

EDIT

Noting Mr. Wizard's correction and mainly as (my own) learning opportunity the following is essentially one of Mr. Wizard's approaches slightly different but mapping functions to positions not elements.

func[list_, pos_, col_] :=
 Module[
  {l, el, styles, rules},
  l = Length[list];
  styles = Function[{x}, Style[x, #]] & /@ col;
  rules = Flatten[Thread /@ MapThread[Rule[#1, #2] &, {pos, styles}]];
  el = ReplacePart[Table[Identity, {l}], rules];
  MapThread[Compose, {el, list}]
  ]

Then

func[list, {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {Red, Green, Blue}]

works

Source Link
ubpdqn
  • 64.8k
  • 3
  • 65
  • 154

Just to add to the variety:

fun[list_, pos_, colors_] :=
 Module[
  {styles},
  styles = Function[{x}, Style[x, #]] & /@ colors;
  list /. 
   Flatten[Thread /@ 
     MapThread[#1 -> #2 /@ #1 &, {list[[#]] & /@ pos, styles}]]
  ]

Then

fun[list, {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {Red, Green, Blue}]

yields the same result.