Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #16639
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: Do we need a When function? |
| Date | 2014-03-11 07:17 +0000 |
| Message-ID | <lfmd9l$n7a$1@smc.vnet.net> (permalink) |
| References | <lfehl0$5kq$1@smc.vnet.net> <lfjtlk$hib$1@smc.vnet.net> |
| Organization | Time-Warner Telecom |
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
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar
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
csiph-web