Leonid Shifrin
  • Member for 10 years
  • Last seen this week
Pure Functions with Lists as arguments
Accepted answer
24 votes

Preamble This is IMO a very good question. I will try to describe an approach based on code-generation, which in my view would allow one to get the most benefits of declarative rule-based-like ...

View answer
Why does Mathematica claim there is no even prime?
Accepted answer
24 votes

Note: I am not particularly knowledgable in the field of this question, so what I write below may well be wrong. I don't know whether or not this should be considered a bug, but to my mind this is an ...

View answer
Convenient string manipulation
Accepted answer
24 votes

I suggest an approach based on creating lexical and / or dynamic environments (custom scoping constructs if you wish), inside which the rules of our "universe" will be altered. I will illustrate with ...

View answer
Strange behavior with Cases and Position with SetDelayed
Accepted answer
23 votes

What's happening This is not simple by any means. You have encountered another instance of a general situation with lexical scope leaks / emulation / over-protection by symbol renaming. The case at ...

View answer
Importing an mx file into a context
Accepted answer
23 votes

Using Begin and End won't help, because .mx files are lower-level and the way they are loaded is different from normal packages. I was about to say that this isn't possible, but here is a hack which ...

View answer
Nest , Fold ... is there an extension for more than 2 arguments?
Accepted answer
23 votes

Yes, there is. Group your extra arguments in a list, and address them by their positions in the function under Fold. For your particular example: FoldList[#1 (1 + First@#2) - Last@#2 &, 1000, ...

View answer
How do I define an argument pattern for a Head of NOT a given value?
Accepted answer
23 votes

You need Except: f[a : Except[_Integer]] := 2 a In addition to being concise, this has an advantage that you can use it in functions which hold their arguments, and don't need to worry about ...

View answer
How to compare power towers in Mathematica?
23 votes

The idea The idea is that if we have $\log(a+b),\qquad a\gg b$ , then we can equivalently write this as $\log a + \log(1 + b/a)$ and the second part will be small, so that one can first ...

View answer
Custom functions by delegating options in a specific way and using core functions
Accepted answer
23 votes

I have proposed a different solution exactly for this sort of situations. You don't have to create a new function with it, but rather you create an environment such that when your function is executed ...

View answer
Get the name of a symbol passed to a function
Accepted answer
23 votes

You are missing Unevaluated: SetAttributes[f, HoldFirst] f[x_] := {SymbolName[Unevaluated@x], x} because SymbolName does not hold its arguments, so you have to prevent evaluation also there. ...

View answer
Simpler input for the new unit support
Accepted answer
23 votes

Here is a cheap way which does not involve WA, but will only be as good as you make it to be (so that you'd have to customize it yourself): create a dynamic environment: ClearAll[withUnits]; ...

View answer
Map-Thread-Through-Apply a list of functions onto a list of (lists of) values
23 votes

What about Map[MapThread[Compose, {fns, #}] &, list] or Transpose@MapThread[Map, {fns, Transpose[list]}]

View answer
How can I separate a separable function
Accepted answer
23 votes

I would take a logarithmic derivative with respect to one variable - it should be then independent of the other one, then integrate it back over the first variable and exponentiate. The second ...

View answer
Performance tuning for game solving (peg solitaire | senku)
Accepted answer
23 votes

Preamble Here is my first stab at it. This will not be the fastest possible solution (I hope to add some faster ones later), but even it will have no problems with your boards, including the full one ...

View answer
Injecting a sequence of expressions into a held expression
23 votes

How about this: ClearAll[inject]; SetAttributes[inject, HoldRest]; inject[Hold[{args__}], new__] := Hold[{args, new}] This will also accept Sequence[3,4] as a second argument. Sequences are spliced, ...

View answer
Is this a bug in Replace?
Accepted answer
22 votes

This is not a bug. However, this is a great opportunity to shed some light on the inner workings of Replace. What happens, in short, is that it first fully processes the expression, without calling ...

View answer
Strategies to compensate for lack of multi-document interface?
Accepted answer
22 votes

Disclaimer Ok, so first comes the warning: what I offer below is an incomplete and work-in-progress code, which I offer AS IS. It only works on OS X for now, has a number of bugs, features are missing,...

View answer
How do I delete all items that occur more than once?
22 votes

Here is a test list: lst = {{1, 2}, {1, 2}, {3, 4}, {5, 6}, {5, 6}, {7, 8}} Here is one way then: GroupBy[Tally[lst], Last][1][[All, 1]] (* {{3, 4}, {7, 8}} *) The same idea using purely ...

View answer
How to efficiently find positions of duplicates?
22 votes

Here is a version based on sorting, and using Mr. Wizard's dynP function: dynP[l_, p_] := MapThread[l[[# ;; #2]] &, {{0}~Join~Most@# + 1, #} &@Accumulate@p] positionOfDuplicates[list_] :...

View answer
Is it possible to use PatternTest and Optional value on one Pattern simultaneously?
Accepted answer
22 votes

The syntax The answer is yes. I use this construct all the time. Here is the form: add[x_, y : (_?Positive) : 1] := x + y; You can test that it passes all the test cases. Sutble behavior to watch out ...

View answer
Is it possible to Clear all variables (but not functions)?
Accepted answer
22 votes

Automatic way based on Names Here is a simple modification of the recent answer of @R.M, which is based on the definiton of a variable as a symbol which has an OwnValue defined: Clear @@ Select[ ...

View answer
Why modules with no variables?
Accepted answer
22 votes

For a single code statement, this is probably an overkill. If you have two or more of them, you have to group them in any case. CompoundExpression is one obvious choice, such as f[x_]:= ( Print[...

View answer
Mathematica Destructuring
Accepted answer
22 votes

You can use macros / code generation to be able to use the syntax you like. Here is one possibility: ClearAll[withLiteralIndices]; SetAttributes[withLiteralIndices, HoldAll]; withLiteralIndices[code_,...

View answer
Faster alternatives for DayOfWeek
22 votes

I will provide one solution which will be using Java and a simple Java reloader I recently introduced. This solution brings to the table up to 100-fold speed-up for large lists of dates. Preparation ...

View answer
Implementing a function which generalizes the merging step in merge sort
Accepted answer
22 votes

Preamble Since I agree that it would be nice to have a generic function of this type, I will provide a general implementation. First, I will give a generic one based on linked lists, then I will add ...

View answer
Determining all possible traversals of a tree
Accepted answer
22 votes

Here is one way: ClearAll[f]; f[tree_List] := Flatten[f[{}, tree], 1]; f[accum_List, {x_, y_List}] := f[{accum, x}, #] & /@ y; f[x_, y_] := Flatten[{x, y}]; The usage is f[B]

View answer
How do I override options with my own defaults?
Accepted answer
21 votes

Here is what I do in such cases ([edit from 01/10/2020 - apparently, this technique has been exposed on this site earlier, at least partially, in this nice answer by Mr.Wizard - which I definitely ...

View answer
Using Sequence to delete a part of an expression
Accepted answer
21 votes

What happens to the list after we assign an element to Sequence[] The answers given so far already contain most of the pieces needed to explain this behavior, but I decided to still add this answer. ...

View answer
How do I save/export a list so that it can later be easily imported as a list again?
Accepted answer
21 votes

I'd use .mx files (Export / Import in "MX" format): Export["myFile.mx",list] and Import["myFile.mx"] This is fast, and does not really involve serialization / parsing in the usual sense (via ...

View answer
StringReplace, ReplaceAll and Rule interact in a bizarre way
Accepted answer
21 votes

Preamble What happens can be understood when we recall that Rule is a scoping construct. The general issues related to variable renamings in scoping constructs have been considered in more details in ...

View answer
1 2 3
4
5
23