Re: Re: What does & mean?
- To: mathgroup at smc.vnet.net
- Subject: [mg107118] Re: [mg107074] Re: [mg107050] What does & mean?
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Wed, 3 Feb 2010 06:10:21 -0500 (EST)
- References: <201002011114.GAA22737@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
OddQ@#2 is the same as OddQ[#2].
Style[#, c, Bold, 18] & /@ #1 is the same as Map[Style[#, c, Bold, 18],#1]
#1 is the first argument of a function, #2 is the second argument, ## is
the sequence of ALL its arguments, etc.
The example is poor code, however.
i is scoped in Module but never used, whereas y, z, and c are used but not
scoped.
Hence the code would be better written as
f[x_List, m_Integer] := Module[{y, z, c}, y = Partition[x, m];
z = MapThread[(c = If[OddQ@#2, Red, Blue];
Style[#, c, Bold, 18] & /@ #1) &, {y, Range@Length@y}];
Infix[Flatten@z, Style["+", 18]]];
f[Table[1, {15}], 3]
The argument # of Style[#, c, Bold, 18] & /@ #1) & is in a different scope
from the #1 and #2 mentioned, so it would be clearer to write:
f[x_List, m_Integer] := Module[{y, z, c, style}, y = Partition[x, m];
style = Style[#, c, Bold, 18] &;
z = MapThread[(c = If[OddQ@#2, Red, Blue];
style /@ #1) &, {y, Range@Length@y}];
Infix[Flatten@z, Style["+", 18]]];
f[Table[1, {15}], 3]
or
f[x_List, m_Integer] := Module[{y, z, c, style}, y = Partition[x, m];
style[item_] := Style[item, c, Bold, 18];
z = MapThread[(c = If[OddQ@#2, Red, Blue];
style /@ #1) &, {y, Range@Length@y}];
Infix[Flatten@z, Style["+", 18]]];
f[Table[1, {15}], 3]
or:
f[x_List, m_Integer] :=
Module[{y = Partition[x, m], z, style, color},
style[items_List, i_] := Style[#, color[i], Bold, 18] & /@ items;
color[i_?OddQ] = Red;
color[i_?EvenQ] = Blue;
z = MapThread[style, {y, Range@Length@y}];
Infix[Flatten@z, Style["+", 18]]];
f[Table[1, {15}], 3]
or:
f[x_List, m_Integer] :=
Module[{y = Partition[x, m], z, style, color},
style[{i_}][item_] := Style[item, color[i], Bold, 18];
style[items_List, i_] := style[i] /@ items;
color[i_?OddQ] = Red;
color[i_?EvenQ] = Blue;
z = MapIndexed[style, y];
Infix[Flatten@z, Style["+", 18]]];
f[Table[1, {15}], 3]
The code has gotten more and more verbose as I went along, but a careful
study might clarify things for you.
Bobby
On Tue, 02 Feb 2010 02:27:20 -0600, cire g <eric.phys at gmail.com> wrote:
> I have the same problem and I had been using Mathematica for a while,
> for example can any one explain the code to color a sum:
>
> f[X_List, m_Integer] := Module[{Y},
> Y = Partition[X, m];
> Z = MapThread[(c = If[OddQ@#2, Red, Blue];
> Style[#, c, Bold, 18] & /@ #1) &, {Y, Range@Length@Y}];
> Infix[Flatten@Z, Style["+", 18]]];
>
> f[Table[1,{15}],3]
>
> of thread [mg107001]
>
> in the documentation is not a good explaining the use of @,#, & etc...
> that make those codes very hard to decipher...
>
> bests
>
>
>
> Michael Knudsen wrote:
>> Hi,
>>
>> I have recently bought Mathematica, and I have a really tough time
>> getting started. I'm reading the various documents found under
>> "Complete Documentation" at the Mathematica homepage, but it doesn't
>> feel like the right place to start.
>>
>> For example, I'm now trying to solve some simple differential
>> equations, and the documentation provides the following example:
>>
>> A = {{4, -6}, {1,-1}};
>>
>> X[t_] = {x[t], y[t]};
>> system = MapThread[#1 == #2 &, {X'[t], A.X[t]}];
>>
>> sol = DSolve[system, {x,y}, t]
>>
>> However, there is no explanation of how & works here (and it isn't in
>> the MapThread documentation either). Where should one start reading in
>> order to understand basic constructs like this? This particular
>> example is really nasty, since & is generally ignored by search
>> engines.
>>
>> Thanks,
>> Michael Knudsen
>>
>>
>>
>
>
--
DrMajorBob at yahoo.com
- References:
- What does & mean?
- From: Michael Knudsen <micknudsen@gmail.com>
- What does & mean?