Mailing List Archive: 49091 messages

# Storing a logical condition

### [1/13] from: tim::johnsons-web::com at: 13-Oct-2003 12:02

Hello rebols: I'd like to store a logical condition in a block to evaluate at a later time by 'if or 'either Example code:
>> a: 1
== 1
>> b: [a = 0]
== [a = 0]
>> either b[print "b evaluates to 'true'"][print "b evaluates to 'false'"]
b evaluates to 'true' What do I need to do so that my answer will be: b evaluates to 'false' thanks tim -- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com http://www.johnsons-web.com

### [2/13] from: tomc:darkwing:uoregon at: 13-Oct-2003 13:13

either do reduce b [][] On Mon, 13 Oct 2003, Tim Johnson wrote:

### [3/13] from: SunandaDH:aol at: 13-Oct-2003 16:30

Tim:
> What do I need to do so that my answer will be: > b evaluates to 'false'
If a is still has a value, when you come to check the condition, then all you need is a do after the either: a: 1 b: [a = 0] either do b [print "b evaluates to 'true'"][print "b evaluates to 'false'"] b evaluates to 'false' If a no longer has a value -- maybe it's been unset or is in another context -- then the above code won't work.... a: 1 b: [a = 0] unset 'a either do b [print "b evaluates to 'true'"][print "b evaluates to 'false'"] ** Script Error: a has no value ....In which case, you'll have wanted to evaluate the result and save it in b at the earlier opportunity: a: 1 b: a = 0 unset 'a either b [print "b evaluates to 'true'"][print "b evaluates to 'false'"] b evaluates to 'false' You could of course use this last possibility even if a still has a value -- but it'll set b to the logical value at the time of the test, not the current value of a: a: 1 b: a = 0 a: 0 either do b [print "b evaluates to 'true'"][print "b evaluates to 'false'"] b evaluates to 'false' ;; even though a = 0 right now Sunanda.

### [4/13] from: gedb01:ya:hoo at: 13-Oct-2003 21:45

Hi Tim, --- Tim Johnson <[tim--johnsons-web--com]> wrote: >
> Hello rebols: > I'd like to store a logical condition in a block to
<<quoted lines omitted: 11>>
> thanks > tim
All you have to do now is 'do b':
>> a: 1
== 1
>> b: [a = 0]
== [a = 0]
>> do b
== false
>> a: 0
== 0
>> do b
== true You can use it in conditionals like this:
>> either do b [print "A is zero"][print ["A is " a]]
A is zero
>> a: 42
== 42

### [5/13] from: ingo:2b1 at: 13-Oct-2003 22:35

Hi Tim and Tom, Tom Conlin wrote:
> either do reduce b [][]
or first reduce b [][] Which is faster, around 2 tenths of a second in 1000000 iterations. (Even _if_ you care, you possibly won't care _that_ much ;-) Kind regards, Ingo

### [6/13] from: tim:johnsons-web at: 13-Oct-2003 13:01

Thanks to both Sunanda and Tom: actually I found the following either (do b)[...][...] worked, but your inputs will help keep me out of trouble: especially Sunanda's observations... cheers! tim * [SunandaDH--aol--com] <[SunandaDH--aol--com]> [031013 12:51]:
> Tim: > > What do I need to do so that my answer will be:
<<quoted lines omitted: 31>>
> To unsubscribe from this list, just send an email to > [rebol-request--rebol--com] with unsubscribe as the subject.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com http://www.johnsons-web.com

### [7/13] from: tomc:darkwing:uoregon at: 13-Oct-2003 14:42

then ... either pick reduce b 1[][] should make it really scream! ;^) On Mon, 13 Oct 2003, Ingo Hohmann wrote:

### [8/13] from: brett:codeconscious at: 14-Oct-2003 10:19

Hi Tim, The ALL function is a candidate too: either all b ... Like Ingo's observation, it is significantly faster too - significant if you have a million iterations ;^)
>> timeit [repeat i 1000000 [do b]]
== 0:00:04.387
>> timeit [repeat i 1000000 [all b]]
== 0:00:02.604 Brett.

### [9/13] from: tim:johnsons-web at: 13-Oct-2003 17:51

* Brett Handley <[brett--codeconscious--com]> [031013 16:42]:
> Hi Tim, > The ALL function is a candidate too:
<<quoted lines omitted: 5>>
> >> timeit [repeat i 1000000 [all b]] > == 0:00:02.604
Cool! Who woulda thunk it? That's a good tip. thnx tim
> Brett. > > -- > To unsubscribe from this list, just send an email to > [rebol-request--rebol--com] with unsubscribe as the subject.
-- Tim Johnson <[tim--johnsons-web--com]> http://www.alaska-internet-solutions.com http://www.johnsons-web.com

### [10/13] from: joel:neely:fedex at: 20-Oct-2003 11:20

Hi, Tim, and all! My buffer overflowed, but maybe this is still worth posting... Tim Johnson wrote:
> * Brett Handley <[brett--codeconscious--com]> [031013 16:42]: >>Like Ingo's observation, it is significantly faster too - significant if you
<<quoted lines omitted: 8>>
> thnx > tim
Using a block specified as blk: [dummy = 1] and a function defined as fn: func [] blk I compared the runtimes of loop reps [] ;; to deduct loop overhead loop reps blk ;; "in-line" loop reps [do blk] ;; "do" loop reps [first reduce blk] ;; "first reduce" loop reps [fn] ;; "fn" loop reps [any blk] ;; "any" loop reps [all blk] ;; "all" After deducting the loop overhead from all other timings (average of ten) I calculated the ratio of each relative to the inline case. I tested with DUMMY equal to 1 and 2 (just in case there was an effect on ANY or ALL), but found no significant difference. Timings were done on an Athlon 2400 running wXP, with REPS set to ten million. I was surprised (and educated) by the result for one version! version avg time ratio to in-line ------------ -------- ---------------- in-line 1.827 s 100.0% fn 3.067 s 167.9% ******** any 3.826 s 209.4% all 3.869 s 211.7% do 3.998 s 218.8% first reduce 15.771 s 863.1% Yep! The winner is the lowly, plain-vanilla function! At least for this simple case, the fastest way to defer the evaluation of an expression is simply to make that expression the body of a function with no parameters! I guess sometimes we outsmart ourselves! ;-) -jn- -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446 Enron Accountingg in a Nutshell: 1c=\$0.01=(\$0.10)**2=(10c)**2=100c=\$1

### [11/13] from: greggirwin:mindspring at: 20-Oct-2003 10:51

Thanks for doing that Joel! Your benchmarks are always instructional and valuable to me. -- Gregg

### [12/13] from: brett:codeconscious at: 21-Oct-2003 10:18

> Yep! The winner is the lowly, plain-vanilla function! At least for > this simple case, the fastest way to defer the evaluation of an > expression is simply to make that expression the body of a function > with no parameters!
Thanks for that work Joel. That is something useful to know. I like the way your result encourages one to use a form that has the least effect on the parent/calling expression - whether you evaluate the condition early eg: condition-result: dummy = 1 or you evaluate it late using the function: condition-result: does [dummy = 1] Your "parent expression" eg. print condition-result Need not change (unless of course dummy does). A nice finding.
> I guess sometimes we outsmart ourselves! ;-)
Too often true! :^) Regards, Brett.

### [13/13] from: joel:neely:fedex at: 21-Oct-2003 10:10

Hi, Brett, Actually, you remind me of another point which I failed to raise... Brett Handley wrote:
> ... result encourages one to use a form that has the least effect > on the "parent/calling expression" - whether you evaluate the
<<quoted lines omitted: 5>>
> print condition-result > Need not change (unless of course dummy does). A nice finding.
That also points out another drawback of the any [expr] and all [expr] versions; they do not preserve the type of (the evaluation of) EXPR for FALSE values, but yield NONE instead! This can become an issue in various ways, such as causing erroneous results when comparing with LOGIC! values from other expressions (FALSE does not equal NONE) or causing type errors when handing off to a mezzanine which expects a LOGIC! argument. While there are workarounds based on standard boolean identities: true and all [expr] or false or all [expr] they simply obscure what is being expressed, IMHO. Per the extreme guys, "Do the simplest thing that could possible work!" -jn- -- ---------------------------------------------------------------------- Joel Neely joelDOTneelyATfedexDOTcom 901-263-4446 Enron Accountingg in a Nutshell: 1c=\$0.01=(\$0.10)**2=(10c)**2=100c=\$1

Notes
• Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted