Skip to main content
Tweeted twitter.com/#!/StackMma/status/290136497791852544
added 825 characters in body
Source Link
matrix42
  • 7.2k
  • 3
  • 26
  • 67

Assuming I have two function:

example 1:

add[{x_, y_, z_}] := x + y - z
add[{1, 3, 5}]

If use pure function,I know I can write it as :

Function[{x}, x[[1]] + x[[2]] - x[[3]]][{1, 3, 5}]

example 2:

fun[{a_, b_}, y_] := If[y~Mod~2 == 0, {a~Join~{y}, b}, {a, b~Join~{y}}]
Fold[fun, {{}, {}}, Range@10]

Fold[Function[{x, y}, 
  If[y < 5, {Join[x[[1]], {y}], x[[2]]}, {x[[1]], 
    Join[x[[2]], {y}]}]], {{}, {}}, Range[10]]

I think this a little prolix with many "[[]]", can you tell me another simpler way?

Updated

Do[
  If[i[[1]] < i[[4]] < i[[7]] && 
    i[[1]]/(10 i[[2]] + i[[3]]) + i[[4]]/(10*i[[5]] + i[[6]]) + 
      i[[7]]/(10 i[[8]] + i[[9]]) == 1, Print@i
   ],
  {i, Permutations[Range[9]]}
  ] // Timing
{2.153, Null}

============

Do[If[(x /. {a_, b_, c_, d_, e_, f_, g_, h_, i_} :> 
      a < d < g && a/(10 b + c) + d/(10 e + f) + g/(10 h + i) == 1), 
   Print@x], {x, Permutations[Range[9]]}] // Timing
{10.374, Null}
(*can't compiled*)

=============

Block[{a, b, c, d, e, f, g, h, i},
  Do[
   If[{a, b, c, d, e, f, g, h, i} = x;
    a < d < g && a/(10 b + c) + d/(10 e + f) + g/(10 h + i) == 1, 
    Print@x], {x, Permutations[Range[9]]}]
  ] // Timing
{2.543, Null}
(*easy compiled*)
(*If Module or Blcok within Do loop, Module will slower than Block *)

Assuming I have two function:

example 1:

add[{x_, y_, z_}] := x + y - z
add[{1, 3, 5}]

If use pure function,I know I can write it as :

Function[{x}, x[[1]] + x[[2]] - x[[3]]][{1, 3, 5}]

example 2:

fun[{a_, b_}, y_] := If[y~Mod~2 == 0, {a~Join~{y}, b}, {a, b~Join~{y}}]
Fold[fun, {{}, {}}, Range@10]

Fold[Function[{x, y}, 
  If[y < 5, {Join[x[[1]], {y}], x[[2]]}, {x[[1]], 
    Join[x[[2]], {y}]}]], {{}, {}}, Range[10]]

I think this a little prolix with many "[[]]", can you tell me another simpler way?

Assuming I have two function:

example 1:

add[{x_, y_, z_}] := x + y - z
add[{1, 3, 5}]

If use pure function,I know I can write it as :

Function[{x}, x[[1]] + x[[2]] - x[[3]]][{1, 3, 5}]

example 2:

fun[{a_, b_}, y_] := If[y~Mod~2 == 0, {a~Join~{y}, b}, {a, b~Join~{y}}]
Fold[fun, {{}, {}}, Range@10]

Fold[Function[{x, y}, 
  If[y < 5, {Join[x[[1]], {y}], x[[2]]}, {x[[1]], 
    Join[x[[2]], {y}]}]], {{}, {}}, Range[10]]

I think this a little prolix with many "[[]]", can you tell me another simpler way?

Updated

Do[
  If[i[[1]] < i[[4]] < i[[7]] && 
    i[[1]]/(10 i[[2]] + i[[3]]) + i[[4]]/(10*i[[5]] + i[[6]]) + 
      i[[7]]/(10 i[[8]] + i[[9]]) == 1, Print@i
   ],
  {i, Permutations[Range[9]]}
  ] // Timing
{2.153, Null}

============

Do[If[(x /. {a_, b_, c_, d_, e_, f_, g_, h_, i_} :> 
      a < d < g && a/(10 b + c) + d/(10 e + f) + g/(10 h + i) == 1), 
   Print@x], {x, Permutations[Range[9]]}] // Timing
{10.374, Null}
(*can't compiled*)

=============

Block[{a, b, c, d, e, f, g, h, i},
  Do[
   If[{a, b, c, d, e, f, g, h, i} = x;
    a < d < g && a/(10 b + c) + d/(10 e + f) + g/(10 h + i) == 1, 
    Print@x], {x, Permutations[Range[9]]}]
  ] // Timing
{2.543, Null}
(*easy compiled*)
(*If Module or Blcok within Do loop, Module will slower than Block *)
edited title
Link
Dr. belisarius
  • 116.2k
  • 13
  • 205
  • 456

Is there a better way to write pure Pure Functions? with Lists as arguments

improved title; added [programming] tag
Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

question about Is there a better way to write pure FunctionFunctions?

Source Link
matrix42
  • 7.2k
  • 3
  • 26
  • 67
Loading