Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5277 > unrolled thread
| Started by | David Bevan <david.bevan@pb.com> |
|---|---|
| First post | 2011-09-29 06:10 +0000 |
| Last post | 2011-09-30 08:05 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
"Lazy" mapping with Or & And? David Bevan <david.bevan@pb.com> - 2011-09-29 06:10 +0000
Re: "Lazy" mapping with Or & And? Ray Koopman <koopman@sfu.ca> - 2011-09-30 08:05 +0000
| From | David Bevan <david.bevan@pb.com> |
|---|---|
| Date | 2011-09-29 06:10 +0000 |
| Subject | "Lazy" mapping with Or & And? |
| Message-ID | <j6124l$m38$1@smc.vnet.net> |
Although Or[f[x1],f[x2],f[x3],...] only evaluates the f[xi] up to the first that returns True, Or @@ f /@ [x1,x2,x3,...] doesn't.
To optimise Or when mapping like this, I've been using mapOrF below. Is the re a better or more preferred solution? Can Hold help?
mapOrF0[f_,s_]:=Or@@f/@s
mapAndF0[f_,s_]:=And@@f/@s
mapOrF[f_,s_]:=TrueQ@Scan[If[f[#],Return[True]]&,s]
mapAndF[f_,s_]:=If[Scan[If[!f[#],Return[False]]&,s],,False,True]
list=Range[1000000];
mapOrF0[#>0&,list]//Timing
mapOrF[#>0&,list]//Timing
{1.297,True}
{0.095,True}
Thanks.
David Bevan
David.Bevan@pb.com
[toc] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2011-09-30 08:05 +0000 |
| Message-ID | <j63t8t$6fr$1@smc.vnet.net> |
| In reply to | #5277 |
On Sep 28, 11:10 pm, David Bevan <david.be...@pb.com> wrote:
> Although Or[f[x1],f[x2],f[x3],...] only evaluates the f[xi] up to
> the first that returns True, Or @@ f /@ [x1,x2,x3,...] doesn't.
>
> To optimise Or when mapping like this, I've been using mapOrF below.
> Is the re a better or more preferred solution? Can Hold help?
>
> mapOrF0[f_,s_]:=Or@@f/@s
> mapAndF0[f_,s_]:=And@@f/@s
>
> mapOrF[f_,s_]:=TrueQ@Scan[If[f[#],Return[True]]&,s]
> mapAndF[f_,s_]:=If[Scan[If[!f[#],Return[False]]&,s],,False,True]
>
> list=Range[1000000];
> mapOrF0[#>0&,list]//Timing
> mapOrF[#>0&,list]//Timing
>
> {1.297,True}
> {0.095,True}
>
> Thanks.
>
> David Bevan
> David.Be...@pb.com
See the Oct 2006 thread "short circuiting" And and Or
https://groups.google.com/group/comp.soft-sys.math.mathematica/browse_frm/thread/966d232547bae196?hl=en
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web