Tag Info

Hot answers tagged

30

If does not have the attribute SequenceHold; use the "vanishing function" ##&[] instead of Sequence[]: If[# > 5, #, ## &[]] & /@ Range[10] {6, 7, 8, 9, 10} See this and this for other uses, and SlotSequence if you are confused by ##. To further explain the method above, Sequence is flattened when it appears as an argument (level one) ...


12

It depends what you consider nothing, but you could try something like this If[a, b, Unevaluated[Sequence[]]] for example 3 + If[False, 1, Unevaluated[Sequence[]]] returns 3. Wrapping an argument of a function in Unevaluated is effectively the same as temporarily setting the attribute Hold for that argument meaning that the argument isn't evaluated ...


11

Here is one idea: Clear[sf, mySequence] sf[x_] := If[x > 0, mySequence[8, 9], 0] mySequence /: (h : Except[If])[x___, mySequence[y___], z___] := h[x, y, z] f[1, 2, sf[1], 4] (* ==> f[1, 2, 8, 9, 4] *) So I defined a sf function that returns the sequence as the result of an If statement. This is just an example, illustrating the general ...


10

I note that all answers so far try to solve the problem of assigning a potential Null value by manipulating the return value. I feel it would be more appropriate to make the whole assignment conditional. Like this: If[condition, aa = value] There's also a small bug in your program (count isn't initialized), and, of course, it doesn't sort at the moment. I ...


9

You need to use Module option on return myMod := Module[{i}, Do[ Return[1, Module], {i, 3} ]; Print["test"] ] and now myMod (* 1 *) This is because Return only returns from nearest enclosure, which was Do in your case and not from the whole Module unless you use the Module second option to Return. This is different from other languages ...


7

In Mathematica, “not returning anything” is not possible. An expression that does not return anything has a value of Null, even though you only actually see this Null in certain circumstances: Null     is a symbol used to indicate the absence of an expression or a result. It is not displayed in ordinary output. When Null appears as a complete ...


6

Calculate the List of results you wish to return and use Apply to replace the head: listFn[a_, b___] := If[a > 0, {b}, {0}]; seqFn[args___] := Sequence @@ listFn[args]; f[1, seqFn[2, 3, 4, 5], 6] f[1, seqFn[-2, 3, 4, 5], 6] (*--> f[1, 3, 4, 5, 6] *) (*--> f[1, 0, 6] *) Here listFn represents the calculation of the results and does not need to be ...


5

Maybe I've missed the point, but a sequence in most ways is just another Mathematica expression, so consider just returning a sequence. f[args___] := args g[x, f[a, b, c], y] g[x, a, b, c, y] g[x, f[], y] g[x, y] Update I have edited this answer to incorporate Mr. Wizard's observation that args in f[args___] := args is already a sequence and ...


4

I think I understand what your trouble might be. Here is your code: a := b means that the left-hand-side is a pattern and the right-hand-side should be evaluated when that pattern is found (roughly speaking). The right-hand-side in this case is: Your function in essence returns the entire module. You can see this by changing Module to something else, ...


3

Print returns Null, and you're returning a list of values, some of which are also Null. So: ReverseList[ele_List] := Module[{list = List[], i, k = 1}, For[ i = Length[ele], i > 0, {i--, k++}, {AppendTo[list, ele[[i]]]}]; Print[list]; list] would be slightly better, perhaps? (Obviously you wouldn't really reverse a list ...


2

I'm having to read between the lines because you did not fully specify your problem, however I suspect that you need to use the third argument of Compile. func = Compile[{{length}}, (result = Sum[i^2, {i, length}]; If[NumberQ[result], result, Abort[]]), {{NumberQ[_], True | False}} ] func[5] 55.


1

To return a sequence using a SetDelayed function simply try seqFu[] := Unevaluated[Sequence[]] Or even Clear[seqFu] seqFu[args___] := Unevaluated[Sequence[args]]; But do not fall into the trap that even though for seqFu2[args___]:= Unevaluated[args] seqFu2[1,2] -> Sequence[1,2] you will have the unwanted seqFu2[1] -> 1 To return a sequence ...


1

Wow, Looks like I'm more than a year late to this party, but I thought this would be nice to post. The Sequence @@ {} trick. Here it is using Mr. Wizard's example: If[# > 5, #, Sequence @@ {}] & /@ Range[10] {6, 7, 8, 9, 10}


1

I’m afraid your base assumption here is false, and the sum compiles much better without this call to NumberQ. See: << CompiledFunctionTools`; func = Compile[{{length, _Integer}}, ( Sum[i^2, {i, 1, length}] )]; CompiledFunctionTools`CompilePrint[func] The output of CompilePrint shows that the sum is performed without any call to MainEvaluate, ...


1

Are you definitely sure you need the error checking for NaN inside Compile? The error checking seems to generate very inefficient compiled code. It basically only calls MainEvaluate, so you gain nothing by compiling. data = Range[1000]; func /@ data; // AbsoluteTiming func1 /@ data; // AbsoluteTiming (* ==> {0.1280073, Null} *) (* ==> {0.0140008, ...



Only top voted, non community-wiki answers of a minimum length are eligible