60,587 reputation
6183275
bio website mathprogramming-intro.org
location St. Petersburg, Russia
age 38
visits member for 2 years, 10 months
seen 3 mins ago

Ok, an obligatory note: opinions expressed here are mine and not those of my employer.


Nov
6
answered What is the shortest way to switch between heads of nested functions
Nov
5
comment What is the shortest way to switch between heads of nested functions
The problem is that the OP didn't formulate his general needs. There are many ways to solve this problem, which would all give the same result for his example, but will generally behave differently. Re: Thread - one good place to read on it is Wagner's book.
Nov
5
comment What is the shortest way to switch between heads of nested functions
In your approach, you don't need an extra step with /.. You can simply use switch[f_[g_[x__]]]:=g[f[x]]. I was thinking about this method too, but there are many ways to generalize from here, and the OP didn't provide a general example / request.
Nov
5
comment What is the shortest way to switch between heads of nested functions
Thread[f[g[x]], g]
Nov
5
comment What are the applications of using functions whose heads are also functions?
In V10 new operator forms have been introduced. One can use SubValues to get similar functionality. It can be quite useful.
Nov
5
comment A smarter nested With?
@TeakeNutma Yes, sure, that wasn't hard at all, see my edit. Thanks for the accept. This was a nice exercise, and this stuff can be actually useful too.
Nov
5
revised A smarter nested With?
Added intermediate variable for threading over symbols, per OP's request
Nov
5
comment Finding duplicates in a list
In V10 there is DuplicateFreeQ.
Nov
5
comment Mathematica taking too long to calculate recursive function
@DanielLichtblau I did mention this, but was wrong to think of this as of a secondary reason, and to blame memoization. Changed my post now, apparently this is indeed the main and only reason for the slowness.
Nov
5
revised Mathematica taking too long to calculate recursive function
Changed the post as memoization apparently wasn't really an issue.
Nov
5
comment Mathematica taking too long to calculate recursive function
@MichaelE2 Looks like my reasoning was partly wrong. Memoization seems to work also in the OP's case. Will change my answer.
Nov
5
revised Mathematica taking too long to calculate recursive function
added 220 characters in body
Nov
5
comment Mathematica taking too long to calculate recursive function
@MichaelE2 Will do, thanks. But have a look at my edit, I also fixed the recursive code.
Nov
5
revised Mathematica taking too long to calculate recursive function
added 220 characters in body
Nov
5
revised Mathematica taking too long to calculate recursive function
added 220 characters in body
Nov
5
answered Mathematica taking too long to calculate recursive function
Nov
5
comment A smarter nested With?
@TeakeNutma because I wrapped symbols in HoldPattern, and use Unevaluated for the rhs. HoldPattern is exactly the right tool for the job here, since it is invisible to the pattern-matcher (and so FreeQ works fine).
Nov
5
comment A smarter nested With?
@Mr.Wizard I guess, the idea is that we want to avoid user-defined functions in expanded code in definitions, both for efficiency reasons and because we may want to attach conditions. As to With vs Module, With is cleaner (manifestly no side effects), when one knows that variables won't change in the body.
Nov
5
comment A smarter nested With?
@MichaelE2 Thanks for double-checking. I've not done this sort of things for some while, got a bit rusty.
Nov
5
comment A smarter nested With?
@MichaelE2 That was an evaluation leak, thanks for reporting. I seem to have fixed it, although perhaps not very elegantly.