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


Groups > comp.soft-sys.math.mathematica > #16669

Re: Do we need a When function?

From Itai Seggev <itais@wolfram.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Do we need a When function?
Date 2014-03-19 08:23 +0000
Message-ID <lgbk5e$12p$1@smc.vnet.net> (permalink)
References <20140308074231.0A4506A0B@smc.vnet.net> <lg10pf$50i$1@smc.vnet.net> <20140317062651.1B88E69F4@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


David,

Firstly, I would appreciate it if you didn't quote these tiny fragments of my
replies, which suggest I didn't already understand/address points you raise
(or that perhaps you didn't bother to read my email in its entirety).

> On 15/03/2014 07:51, Itai Seggev wrote:
> > If[NumericQ[x], f[x]]   (*are NumberQ, if want an actual number type*)
>
> The difference, which at least Szabolcs appreciates, can be seen in this
> example:
>
> In[3]:= expr = If[NumericQ[x], ToString[x]]
>
> In[4]:= expr /. x -> 2.1
>
> (* The output here is Null *)
>
> In[7]:= SetAttributes[When, HoldAll];
> When[cond_, val_] := val /; cond
>
> In[9]:= expr = When[NumericQ[x], ToString[x]]
>
> Out[9]= When[NumericQ[x], ToString[x]]
>
> In[10]:= expr /. x -> 2.1
>
> Out[10]= "2.1"
>
> The effect of When is to delay its evaluation until something is true -
> which can be quite useful!

But on the other hand you have these behaviors:

In[59]:= ClearAll[When]
SetAttributes[When, HoldAllComplete];
When[cond_, val_] := val /; cond

In[62]:= expr = When[NumericQ[x], ToString[x]]

Out[62]= When[NumericQ[x], ToString[x]]

In[64]:= Block[{x = 2.1}, expr ]

Out[64]= When[NumericQ[x], ToString[x]]

In[65]:= x = 2.1; expr

Out[65]= When[NumericQ[x], ToString[x]]

Are these useful?


Back to comp.soft-sys.math.mathematica | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Re: Do we need a When function? Itai Seggev <itais@wolfram.com> - 2014-03-15 07:51 +0000
  Re: Do we need a When function? David Bailey <dave@removedbailey.co.uk> - 2014-03-17 06:27 +0000
  Re: Do we need a When function? Itai Seggev <itais@wolfram.com> - 2014-03-19 08:23 +0000
    Re: Do we need a When function? David Bailey <dave@removedbailey.co.uk> - 2014-03-23 09:01 +0000

csiph-web