4
$\begingroup$

Can anyone explain the code below? The problem is in this part of the code. What is foo, ## & @@? I have been looking at this code for several hours but have no idea how it works.

## & @@ Table[Flatten@{foo["a", "b"], SpanFromLeft, 
            minMax@foo[a, b]}, {foo, functions}]

Full code:

ClearAll[functions, minMax]
functions = {# + #2 &, -# &, # + 2 #2 &, -#2 &, 1/(2 # - #2) &};
minMax = MinMax@*Interval;
DynamicModule[{a = {20, 50}, b = {35, 75}}, 
 Dynamic@Grid[{{Grid[{{Labeled[
         intSlider[Dynamic@{a[[1]], a[[2]]}, {0, 100, 1}], 
         Style["a", 16], Left], SpanFromLeft, SpanFromLeft, 
        SpanFromLeft}, {Labeled[
         intSlider[Dynamic@{b[[1]], b[[2]]}, {0, 100, 1}], 
         Style["b", 16], Left], SpanFromLeft, SpanFromLeft, 
        SpanFromLeft}}, Dividers -> All, ItemSize -> 10]}, {Dynamic@
      Grid[{{"expr", SpanFromLeft, "min (expr)", "max (expr)"} /. 
         s_String :> Item[s, Background -> LightMagenta], ## & @@ 
         Table[Flatten@{foo["a", "b"], SpanFromLeft, 
            minMax@foo[a, b]}, {foo, functions}]}, Dividers -> All, 
       ColumnsEqual -> True, ItemSize -> 10, Alignment -> Center]}}]]

Source: Making tables and update calculated values

$\endgroup$
1
  • $\begingroup$ foo is just a variable name; {foo,functions} tells that this variable must take values from the list functions. For example, numbers = {1,2,3} followed by Table[foo + 1, {foo, numbers} ] returns {2,3,4} $\endgroup$ Feb 12, 2020 at 8:15

1 Answer 1

8
$\begingroup$

Try it:

## & @@ {1, 2, 3}
(* Sequence[1, 2, 3] *)

Sequence is a wrapper for a list that you want expanded into separate function arguments when it appears in an argument list.

I cannot say why the author didn't just do Sequence @@.

$\endgroup$
3
  • 7
    $\begingroup$ Sequence @@ is a lot more typing. $\endgroup$ Feb 11, 2020 at 18:49
  • $\begingroup$ Can you explain minMax = MinMax@*Interval and foo too? I read that @* is a composition but what does MinMax@*Interval@foo[a, b] return? From what I read Interval should have two variables but this one has only one. $\endgroup$
    – emnha
    Feb 11, 2020 at 18:54
  • $\begingroup$ Interval[{min,max}] can accept one list. Although, it can also accept more than one list: Interval[{min1, max1},{min2, max2}, ... ] $\endgroup$ Feb 11, 2020 at 20:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.