SHARE
TWEET

Untitled

a guest Sep 16th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #A function is "consistent" if it always returns the same value for the same arguments.
  2. #A function can be consistent even if it is not:
  3. # - deterministic
  4. # - free of side effects
  5. #That said, "F is deterministic and F is free of side effects" does imply "F is consistent"
  6.  
  7. #consistent, deterministic, no side effects
  8. def a(x):
  9.     return x * 2
  10.  
  11. #consistent, deterministic, has side effects
  12. _cache = []
  13. def b(x):
  14.     _cache.append(x)
  15.     return x
  16.  
  17. #consistent, not deterministic, no side effects
  18. #(pointless, yes, but still possible)
  19. def c(x):
  20.     if true_random.choice((False, True)):
  21.         return x
  22.     else:
  23.         return x
  24.  
  25. #consistent, not deterministic, side effects
  26. _cache = []
  27. def d(x):
  28.     _cache.append(x * true_random.random())
  29.     return x
  30.  
  31. #not consistent, deterministic, no side effects
  32. def e(x):
  33.     #... Pretty sure this one is impossible actually
  34.     pass
  35.  
  36. #not consistent, deterministic, has side effects
  37. _cache = []
  38. def f(x):
  39.     _cache.append(x)
  40.     return len(_cache)
  41.  
  42. #not consistent, not deterministic, no side effects
  43. def g(x):
  44.     return x * true_random.random()
  45.  
  46. #not consistent, not deterministic, has side effects
  47. _cache = []
  48. def h(x):
  49.     _cache.append(x * true_random.random())
  50.     return len(_cache)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top