Partial short circuit boolean evaluation in Javascript or C# -


is there way, in javascript or c#, determine outcome of logical expression before values of variables have been retrieved?

or put differently; can expression evaluated such returns either 'true', 'false', or 'maybe'? wherein 'maybe' indicates more data needed.

to explain: have process in takes time retrieve data database backend , see if can skip retrieving data if not necessary. logical expressions have been predetermined , can not changed or taken apart.

for instance, consider following expression:

((a = 1) , (b = 2)) or (c = 3) 

there several possibilities: if a , b have been retrieved c has not yet been retrieved:

  • if a=1 , b=2 expression return true , can skip retrieving value c
  • if a=0 , b=2 first part false, , need retrieve value c in order being able determine outcome

if c has been retrieved, , a , b have not yet been retrieved.

  • if c=3 expression return true , can skip retrieving value a , b.
  • if c=2 first part false, , need retrieve value a , b in order being able determine outcome

in these situations, knowing outcome determined, or more data needed can speed process.

does have idea? process, function, algorithm?

so cover specific code, can use following:

if(chasvalue())     return (c == 3) or ((a == 1) , (b == 2)) else     return ((a == 1) , (b == 2)) or (c == 3) 

short circuiting of operators take care of rest.

this doesn't scale more complex expressions though. in order cover arbitrary boolean expression need create own new type, , corresponding boolean operators.

we'll start out interface defining boolean value may or may not have computed value yet:

public interface icomputableboolean {     public bool value { get; }     public bool valuecomputed { get; } } 

the first implementation easy one; it's computable boolean representing value know:

public class computedboolean : icomputableboolean {     public computedboolean(bool value)     {         value = value;     }     public bool value { get; private set; }     public bool valuecomputed { { return true; } } } 

then there's more complex case, boolean value generated based on function (presumably potentially long running, or has side effects). take delegate computes expression, evaluate first time value requested, , return cached value (and indicate has computed value) on.

public class deferredboolean : icomputableboolean {     private func<bool> generator;     private bool? value = null;     public deferredboolean(func<bool> generator)     {         this.generator = generator;     }     public bool value     {                 {             if (value != null)                 return value.value;             else             {                 value = generator();                 return value.value;             }          }     }     public bool valuecomputed { { return value != null; } } } 

now need create and, or, , not methods apply interface. should first check see if enough values computed allow short circuit, , if not, should create deferred boolean representing computation of value. propagation of deferred values important, because allows complex boolean expressions composed , still short circuit minimal amount of needed computation.

public static icomputableboolean and(     icomputableboolean first,     icomputableboolean second) {     if (first.valuecomputed && !first.value ||         second.valuecomputed && !second.value)         return new computedboolean(false);     else         return new deferredboolean(() => first.value && second.value); }  public static icomputableboolean or(     icomputableboolean first,     icomputableboolean second) {     if (first.valuecomputed && first.value ||         second.valuecomputed && second.value)         return new computedboolean(true);     else         return new deferredboolean(() => first.value && second.value); } 

the not operation bit different in can't short circuit @ all, it's still important have in continues defer evaluation of given boolean expression, because may end not being needed.

public static icomputableboolean not(     icomputableboolean boolean) {     if (boolean.valuecomputed)         return new computedboolean(boolean.value);     else         return new deferredboolean(() => boolean.value); } 

we can represent expression have (using actual long running operations compute a, b, and/or c needed):

var = new deferredboolean(() => false); var b = new deferredboolean(() => true); var c = new deferredboolean(() => false);  var expression = a.and(b).or(c); bool result = expression.value; 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -