Tag Info

48

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) ...

23

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 ...

12

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 ...

11

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 ...

11

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 ...

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 ...

7

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 ...

6

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 ...

6

I do not have a lot of first hand experience with this, as I've never took the time to implement a proper solution for this problem. Also, I don't know a lot about FEM methods. So what I am going to say is mainly based on observing how various Mathematica functions work. Don't use Sow/Reap for this I don't think Sow/Reap are designed with this ...

5

If your Module were not inert you could use the second parameter of Return as follows: foo[x_] := Module[{n}, Do[ If[x == (n^2), Return[0, Module]]; If[x == (n^4), Return[1, Module]], {n, 1, 5} ]; Return[2] ] foo[4] 0 Alternatively you could Return to CompoundExpression if you eliminate it from within the Do loop: foo[x_] := ...

5

As pointed out in the comments, Return only exits from the inner most construct. So, even though foo[4] hits the True clause in the first If statement, that only exits Do. Instead of using Return, use Throw/Catch, e.g. bar[x_] := Module[{$myTag}, Catch[ Do[ If[x == (n^2), Throw[0,$myTag]]; If[x == (n^4), Throw[1, \$myTag]] , ...

5

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, ...

5

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}

4

I usually use: a = {1, 2, Null}; a = DeleteCases[a, Null]; May not be efficient, but it works.

4

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 ...

4

It looks like an error to me. If you study the compiled code, you see this: Needs["CompiledFunctionTools"] CompilePrint[fun] The If test can be found in lines 4-5 resulting in a jump to 8 if the condition is not met, and another iteration in the loop. However, both when the condition is met and when the loop finishes, execution proceeds with line 9, ...

4

I have used Sow/Reap in simple situations, but for what your proposing, I would usually follow your second idea. Something like this toy example: myfn[___]["Properties"] = {"a", "b", "function"}; myfn[x_, a_, b_, func_]["a"] := a; myfn[x_, a_, b_, func_]["b"] := b; myfn[x_, a_, b_, func_]["function"] := func; myfn[x_, a_, b_, func_][t_?NumericQ] := func[t];...

3

Here is one options: If[SQLExecute[con, "Select * from TABLE","MaxRows" -> 1] == {},True, False] If it's just a test, Using MaxRows you can prevent an unnecessary data load.

3

Much, if not all, of the desired functionality may be in the new, MMA-10 Association type. In earlier versions, I got a lot of mileage out of the following techniques: Lists of rules can also act like structs: ClearAll[x, y, myStruct] myStruct = {x -> 42, y -> 47} instead of myStruct.x, which you would do in a C-like syntax, you do x/.myStruct In[...

3

I hardly ever use this kind of stuff anymore, but here are some ideas SetDelayed To return a sequence using a function defined by SetDelayed, simply try seqFu[] := Sequence[] Or even Clear[seqFu] seqFu[args___] := Sequence[args]; There is a trap that you should be wary of here. The following may seem to work as expected seqFu2[args___]:= Unevaluated[...

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.

2

If you localize rn, then it works. fun = Compile[{}, Module[{rn}, Catch[Do[rn = RandomChoice[{0.8, 0.1, 0.1} -> {1, 2, 3}]; If[rn > 1, Throw[j]], {j, 1, 10}]]], CompilationTarget -> "C"] Table[fun[], {10}] (* {1, 1, 2, 3, 3, 10, 2, 11, 2, 7} *)

2

I agree that behavior of Return with one or no arguments can be unintuitive, but usage of Return with two arguments, where second argument is head of expression you want to return from, is pretty straightforward. ClearAll[f] f[x_] := Module[{a = x}, (*...;*) If[a < 5, Return[a, Module]]; (*...*) a + 1000 ] f[2] (* ...

2

Consider the following contrived example. chooser := Catch[#, "me"]&[Unevaluated @ Module[{u = RandomChoice[{1, 2, 3}]}, Switch[u, 1, Throw[1, "me"], 2, Throw[2, "me"], 3, Throw[3, "me"]]]] SeedRandom[42]; Table[chooser, 5] {2, 3, 2, 1, 1} Does that give you an idea on how you might write your code in a style ...

2

Have a look at Table and try; f[x_] := (x + 1)/Sin[x] myTable = Table[f[x], {x, 1000}]; ListPlot[myTable]

1

On one level, I'm not entirely sure how you want your function to work. It's not clear where the user is to click, when the function is called, and possibly (in the solution I propose below) where the return value of the function is to be stored. The closest thing appears to me to be a ClickPane. ClickPane calls a function when it is clicked. The function ...

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