Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #16612 > unrolled thread

Do we need a When function?

Started byDavid Bailey <dave@removedbailey.co.uk>
First post2014-03-08 07:42 +0000
Last post2014-04-13 09:26 +0000
Articles 6 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Do we need a When function? David Bailey <dave@removedbailey.co.uk> - 2014-03-08 07:42 +0000
    Re: Do we need a When function? Szabolcs Horvát <szhorvat@gmail.com> - 2014-03-10 08:38 +0000
      Re: Do we need a When function? David Bailey <dave@removedbailey.co.uk> - 2014-03-11 07:17 +0000
    Re: Do we need a When function? Helen Read <readhpr@gmail.com> - 2014-03-10 08:41 +0000
    Re: Do we need a When function? Szabolcs Horvát <szhorvat@gmail.com> - 2014-04-11 06:09 +0000
      Re: Do we need a When function? David Bailey <dave@removedbailey.co.uk> - 2014-04-13 09:26 +0000

#16612 — Do we need a When function?

FromDavid Bailey <dave@removedbailey.co.uk>
Date2014-03-08 07:42 +0000
SubjectDo we need a When function?
Message-ID<lfehl0$5kq$1@smc.vnet.net>
Dear All,

Recently I wanted to create an expression that only evaluated when x was 
a number. Obviously, one way to do that is to write something like:

  If[x<1000000000000,f[x]]

This will stay unevaluated until x is assigned to a number - as required 
- but it is very ugly because it makes it hard to understand what is 
going on.

More generally, it would be nice to write an expression that will only 
evaluate when an expression is true. I solved that by writing a function 
When:

SetAttributes[When, HoldAll];
When[cond_, val_] := val /; cond

However, my point is that this construction is sufficiently useful that 
it should be built in to Mathematica, and my solution might not be 
obvious to all users.

Am I missing a simpler solution?

Do others agree that this is an omission in the language?

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

[toc] | [next] | [standalone]


#16622

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2014-03-10 08:38 +0000
Message-ID<lfjtlk$hib$1@smc.vnet.net>
In reply to#16612
On 2014-3-8, 2:42 , David Bailey wrote:
> Dear All,
>
> Recently I wanted to create an expression that only evaluated when x was
> a number. Obviously, one way to do that is to write something like:
>
>    If[x<1000000000000,f[x]]
>
> This will stay unevaluated until x is assigned to a number - as required
> - but it is very ugly because it makes it hard to understand what is
> going on.
>
> More generally, it would be nice to write an expression that will only
> evaluate when an expression is true. I solved that by writing a function
> When:
>
> SetAttributes[When, HoldAll];
> When[cond_, val_] := val /; cond
>
> However, my point is that this construction is sufficiently useful that
> it should be built in to Mathematica, and my solution might not be
> obvious to all users.
>
> Am I missing a simpler solution?
>
> Do others agree that this is an omission in the language?
>

I see where you are coming from with this.

I think this comes up most often in cases like this:

http://support.wolfram.com/kb/3820

When[] would make it unnecessary to define a separate function.

However one might argue that in situations like this it is better 
practice to define that function anyway, if not for anything else then 
just for structuring the code better (which could avoid errors).

Do you see other common use cases for it?

[toc] | [prev] | [next] | [standalone]


#16639

FromDavid Bailey <dave@removedbailey.co.uk>
Date2014-03-11 07:17 +0000
Message-ID<lfmd9l$n7a$1@smc.vnet.net>
In reply to#16622
On 10/03/2014 08:38, Szabolcs Horvát wrote:
> On 2014-3-8, 2:42 , David Bailey wrote:
>> Dear All,
>>
>> Recently I wanted to create an expression that only evaluated when x was
>> a number. Obviously, one way to do that is to write something like:
>>
>>     If[x<1000000000000,f[x]]
>>
>> This will stay unevaluated until x is assigned to a number - as required
>> - but it is very ugly because it makes it hard to understand what is
>> going on.
>>
>> More generally, it would be nice to write an expression that will only
>> evaluate when an expression is true. I solved that by writing a function
>> When:
>>
>> SetAttributes[When, HoldAll];
>> When[cond_, val_] := val /; cond
>>
>> However, my point is that this construction is sufficiently useful that
>> it should be built in to Mathematica, and my solution might not be
>> obvious to all users.
>>
>> Am I missing a simpler solution?
>>
>> Do others agree that this is an omission in the language?
>>
>
> I see where you are coming from with this.
>
> I think this comes up most often in cases like this:
>
> http://support.wolfram.com/kb/3820
>
> When[] would make it unnecessary to define a separate function.
>
> However one might argue that in situations like this it is better
> practice to define that function anyway, if not for anything else then
> just for structuring the code better (which could avoid errors).
>
> Do you see other common use cases for it?
>
>
Well my particular use was to stop a ToString expression evaluating 
before the argument to ToString is a number.

There are obviously ways to solve this problem, just as there are ways 
to substitute for many Mathematica functions - such as Riffle - but it 
seems like the sort of function that could make code clearer to 
understand - it surprised me that it doesn't exist!

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

[toc] | [prev] | [next] | [standalone]


#16632

FromHelen Read <readhpr@gmail.com>
Date2014-03-10 08:41 +0000
Message-ID<lfjtrv$hl9$1@smc.vnet.net>
In reply to#16612
Use NumberQ

For example:

f[x_?NumberQ] := x^3

Helen Read
University of Vermont



On 3/8/2014 2:42 AM, David Bailey wrote:
> Dear All,
>
> Recently I wanted to create an expression that only evaluated when x was
> a number. Obviously, one way to do that is to write something like:
>
>    If[x<1000000000000,f[x]]
>
> This will stay unevaluated until x is assigned to a number - as required
> - but it is very ugly because it makes it hard to understand what is
> going on.
>
> More generally, it would be nice to write an expression that will only
> evaluate when an expression is true. I solved that by writing a function
> When:
>
> SetAttributes[When, HoldAll];
> When[cond_, val_] := val /; cond
>
> However, my point is that this construction is sufficiently useful that
> it should be built in to Mathematica, and my solution might not be
> obvious to all users.
>
> Am I missing a simpler solution?
>
> Do others agree that this is an omission in the language?
>
> David Bailey
> http://www.dbaileyconsultancy.co.uk
>



[toc] | [prev] | [next] | [standalone]


#16779

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2014-04-11 06:09 +0000
Message-ID<li80vk$acm$1@smc.vnet.net>
In reply to#16612
On 2014-3-8, 2:42 , David Bailey wrote:
> Dear All,
>
> Recently I wanted to create an expression that only evaluated when x was
> a number. Obviously, one way to do that is to write something like:
>
>    If[x<1000000000000,f[x]]
>
> This will stay unevaluated until x is assigned to a number - as required
> - but it is very ugly because it makes it hard to understand what is
> going on.
>
> More generally, it would be nice to write an expression that will only
> evaluate when an expression is true. I solved that by writing a function
> When:
>
> SetAttributes[When, HoldAll];
> When[cond_, val_] := val /; cond
>
> However, my point is that this construction is sufficiently useful that
> it should be built in to Mathematica, and my solution might not be
> obvious to all users.
>
> Am I missing a simpler solution?
>
> Do others agree that this is an omission in the language?
>


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.



[toc] | [prev] | [next] | [standalone]


#16793

FromDavid Bailey <dave@removedbailey.co.uk>
Date2014-04-13 09:26 +0000
Message-ID<lidl88$jg$1@smc.vnet.net>
In reply to#16779
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web