Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

If the data are not ordered pairs, or the {min, max} are not in range, the function should just echo back what you initially typed into it.

This requires that no definition match under the given condition. For example

foo[data : {{_, _} ..}, min_, max_] /; max >= min := "some action"

Now if any part of this pattern including the Condition does not match the input is returned:

foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]
foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]

A valid input evaluates:

foo[{{1, 2}, {3, 4}}, 3, 8]
"some action"

If your condition depends on the output of the function then you need a very specific use of Condition

lhs := Module[{vars}, rhs /; test] allows local variables to be shared between test and rhs. You can use the same construction with Block and With.

So maybe you want something like this:

Clear[foo]

foo[data : {{_, _} ..}, min_, max_] :=
  Module[{mean},
    mean = Mean[ data[[All, 2]] ];
    mean /; min <= mean <= max
  ]

Now:

foo[{{1, 2}, {3, 4}}, 1, 5]
3

But:

foo[{{1, 2}, {3, 4}}, 5, 7]
foo[{{1, 2}, {3, 4}}, 5, 7]

And any other bad input is returned as-is:

foo["not good data", 5, 7]
foo["not good data", 5, 7]

Additional reading:

If the data are not ordered pairs, or the {min, max} are not in range, the function should just echo back what you initially typed into it.

This requires that no definition match under the given condition. For example

foo[data : {{_, _} ..}, min_, max_] /; max >= min := "some action"

Now if any part of this pattern including the Condition does not match the input is returned:

foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]
foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]

A valid input evaluates:

foo[{{1, 2}, {3, 4}}, 3, 8]
"some action"

If your condition depends on the output of the function then you need a very specific use of Condition

lhs := Module[{vars}, rhs /; test] allows local variables to be shared between test and rhs. You can use the same construction with Block and With.

So maybe you want something like this:

Clear[foo]

foo[data : {{_, _} ..}, min_, max_] :=
  Module[{mean},
    mean = Mean[ data[[All, 2]] ];
    mean /; min <= mean <= max
  ]

Now:

foo[{{1, 2}, {3, 4}}, 1, 5]
3

But:

foo[{{1, 2}, {3, 4}}, 5, 7]
foo[{{1, 2}, {3, 4}}, 5, 7]

And any other bad input is returned as-is:

foo["not good data", 5, 7]
foo["not good data", 5, 7]

Additional reading:

If the data are not ordered pairs, or the {min, max} are not in range, the function should just echo back what you initially typed into it.

This requires that no definition match under the given condition. For example

foo[data : {{_, _} ..}, min_, max_] /; max >= min := "some action"

Now if any part of this pattern including the Condition does not match the input is returned:

foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]
foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]

A valid input evaluates:

foo[{{1, 2}, {3, 4}}, 3, 8]
"some action"

If your condition depends on the output of the function then you need a very specific use of Condition

lhs := Module[{vars}, rhs /; test] allows local variables to be shared between test and rhs. You can use the same construction with Block and With.

So maybe you want something like this:

Clear[foo]

foo[data : {{_, _} ..}, min_, max_] :=
  Module[{mean},
    mean = Mean[ data[[All, 2]] ];
    mean /; min <= mean <= max
  ]

Now:

foo[{{1, 2}, {3, 4}}, 1, 5]
3

But:

foo[{{1, 2}, {3, 4}}, 5, 7]
foo[{{1, 2}, {3, 4}}, 5, 7]

And any other bad input is returned as-is:

foo["not good data", 5, 7]
foo["not good data", 5, 7]

Additional reading:

Source Link
Mr.Wizard
  • 273.1k
  • 34
  • 595
  • 1.4k

If the data are not ordered pairs, or the {min, max} are not in range, the function should just echo back what you initially typed into it.

This requires that no definition match under the given condition. For example

foo[data : {{_, _} ..}, min_, max_] /; max >= min := "some action"

Now if any part of this pattern including the Condition does not match the input is returned:

foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]
foo[{{1, 2}, {3, 4}}, 5, 2]

foo[{1, 2, 3}, 3, 5]

A valid input evaluates:

foo[{{1, 2}, {3, 4}}, 3, 8]
"some action"

If your condition depends on the output of the function then you need a very specific use of Condition

lhs := Module[{vars}, rhs /; test] allows local variables to be shared between test and rhs. You can use the same construction with Block and With.

So maybe you want something like this:

Clear[foo]

foo[data : {{_, _} ..}, min_, max_] :=
  Module[{mean},
    mean = Mean[ data[[All, 2]] ];
    mean /; min <= mean <= max
  ]

Now:

foo[{{1, 2}, {3, 4}}, 1, 5]
3

But:

foo[{{1, 2}, {3, 4}}, 5, 7]
foo[{{1, 2}, {3, 4}}, 5, 7]

And any other bad input is returned as-is:

foo["not good data", 5, 7]
foo["not good data", 5, 7]

Additional reading: