MathGroup Archive 2014

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Do we need a When function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg132574] Re: Do we need a When function?
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Sun, 13 Apr 2014 05:26:37 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net
  • References: <lfehl0$5kq$1@smc.vnet.net> <li80vk$acm$1@smc.vnet.net>

On 11/04/2014 07:09, Szabolcs Horvát wrote:
> On 2014-3-8, 2:42 , David Bailey wrote:

>
>
> Putting aside the question whether this should be a part of the core
> language, I got quite convinced that this is a pretty useful function.
> I am saying this one month after David's original post, so this opinion
> is based on some practical experience.  During this month I found myself
> remembering and using this function on a number of occasions.  (Not very
> often, but it does keep coming back.)
>
> Admittedly, in all cases I used it with NumericQ, so I might as well
> have hard-coded NumericQ into it ...
>
> SetAttributes[whenNumeric, HoldRest]
> whenNumeric[x_?NumericQ, val_] := val
>
> My point is that it seems that this is going to stay in my personal
> toolbox permanently, it is indeed a useful function.
>
>
> To show an example, most recently I used it while experimenting with
> this piecewise function:
>
> http://mathematica.stackexchange.com/q/45763/12
>
> NIntegrate[when[NumericQ[x], First@test[x]], {x, -2, 2}]
>
> test[] is a function that returns a vector here.  Its implementation
> details are irrelevant for my argument.
>
> Using When here was much easier than defining a separate function only
> to allow NIntegrate to be used on the first component of the vector.
>
> This When[] function doesn't allow me to do anything I couldn't do
> without it.  I wouldn't miss it at all when writing a package.  But it
> is very useful for quick-and-dirty or "improvisational" programming,
> i.e. what I do 90% of the time when I use Mathematica interactively.  It
> saves me time and effort.
>
> Szabolcs
>
> P.S. One thing that bothers me slightly is that I do not yet see clearly
> whether Condition caching might interact badly with this function.
>
>
>
>
Thanks for your comments - you seem to be the only person to understand 
what this function was designed to do!

I did think about simply writing a whenNumeric function, but I felt sure 
there would be occasional other interesting applications. For example:

when[StringQ[x],.....]

I also felt When would be obviously analogous to If.

Since When can be constructed out of Mathematica code, I reckon caching 
will not be a problem - at least not in typical cases.

David Bailey
http://www.dbaileyconsultancy.co.uk




  • Prev by Date: Re: SequenceForm for graphics
  • Next by Date: Re: List vs. array in Mathematica
  • Previous by thread: Re: Do we need a When function?
  • Next by thread: checking the sign of all terms in a long expression