Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Keeping a variable constantly up-to-date

Name: Anonymous 2010-07-13 21:04

I'm certain this is a stupid question, [SPOILER]/prog/[SPOILER], but how does one keep a variable updated without doing it manually?

Example of what I'm looking for, preferably in pythong:

x = 10
y = x*2

x += 5

y == x*2
...
True


I could make a simple function to make sure var2 is always proportional to var1, but when I get to dozens or hundreds of variables, it gets to be a pain in the anus.

Name: Anonymous 2010-07-13 21:05

python
ONE WORD THE FORCED INDENTATION OF CODE THREAD OVER

Name: Anonymous 2010-07-13 21:07

>>2
Back to /g/, please.

Name: Anonymous 2010-07-13 21:19

>>1
javascript:
x=10;
__defineGetter__('y',function(){return x*2});
alert(y);
x+=5;
alert(y):

Name: Anonymous 2010-07-13 21:21

>>3
???

>>1
You use one canonical variable and accessor functions.
Alternatively you could use observers to keept things updated, but that's more state and more pain. Either way sprinkle liberally with metaprogramming.
This is of course to be taken purely as an invitation for you to post your follow-up troll.

Name: Anonymous 2010-07-13 21:23

just use a function

Name: Anonymous 2010-07-13 21:25

>>4
I've been fiddling around with HTML5's canvas and testing out game development, so that was actually pretty handy.

Thanks.

Name: Anonymous 2010-07-13 21:33

>>1
1. Make y a function instead of a variable where y() = x * 2
2. Update all these variables in one Update() function and call it automatically at an appropriate time (eg for a game, every frame)
3. Make a setX function or accessor that automatically updates y

(1) is probably preferred if performance isn't essential.  If y is always 2*x, one of the variables is redundant.

Name: Anonymous 2010-07-13 22:19

>>8
If the calculation is expensive, you can check if any of the independent variables have changed before updating the dependent variable. This may be preferred even if performance is essential.

Name: Anonymous 2010-07-13 22:46

(function()
{ __defineGetter__('x', function(){ return _x; });
  __defineSetter__('x', function(v){ _x = v; y = v * 2; }); })();
x = 10;
alert(y);
x += 5;
alert(y);

Name: Anonymous 2010-07-13 22:50

>>10
(function()
{ var _x;
  __defineGetter__('x', function(){ return _x; });
  __defineSetter__('x', function(v){ _x = v; y = v * 2; }); })();
x = 10;
alert(y);
x += 5;
alert(y);

Name: Anonymous 2010-07-13 23:02

>>1
Maybe you could begin by using a real programming language.

Name: Anonymous 2010-07-13 23:28

>>12
yeah, that's probably why there's all that javascript and no FIOC solutions.

Name: Anonymous 2010-07-13 23:31

>>10,11
function()
{

return             return {
{                      ok: true       
    ok: false      };
};
SILENT ERROR!      Works well in Javascript

http://www.youtube.com/watch?v=hQVTIJBZook#t=30m35s

Name: Anonymous 2010-07-14 1:25

this is a good question--approved

Name: FrozenVoid 2010-07-14 1:54

Name: Anonymous 2010-07-14 2:15

>>13
That was a terrible! quip, you know it.

Name: Anonymous 2010-07-14 2:30

>>16
What the JavaScript programmer of your caliber uses instead of getters?

Name: FrozenVoid 2010-07-14 2:45

>>18
If you look at it you closely don't even need to use anything higher level(OOP junk layers+syntactic sugar) than functions:
o={valx:0,valy:1,x:function(){this.valx=this.valy*2;return this.valx},y:function(){this.valy=this.valx*2;return this.valy}}
o.valx+=10
o.y()+1//return 21



__________________
Orbis terrarum delenda est

Name: Anonymous 2010-07-14 5:00

OP here, and I realized I'm completely retarded.

I actually could use a function and do it efficiently.

I'd just completely forgotten the existence of lambda for one-line functions.

Name: Anonymous 2010-07-14 5:11

If you used lisp, you have a few options:
1. function
Can't set to it, but if you define a setf function, you can make it act like an accessor.
2. accessor (like setters/getters in other
Needs an object to work with.
3. (global or local) symbol macro
Looks like a normal variable/symbol, but in reality it will expand to a form which will either be a function call, place form (setf) or some object accessor (which are implemented using generic functions and place forms).

In most other languages, you'll have to settle with accessors and normal functions or methods.

Name: Anonymous 2010-07-14 6:04

>>20
I'd just completely forgotten the existence of lambda for one-line functions.
That is not what lambda is for, regardless of what Guido will tell you.
>>21
3. (global or local) symbol macro
To be honest, I never liked these, although for something like OPs problem it may be fine. R6RS added a convenient form for doing them too, that I hadn't noticed until yesterday
(define-syntax y (identifier-syntax (* 2 x)))
Of course, this macro would always refer to the x in the scope which the it is bound, but I think that is the behaviour OP would want.

Name: Anonymous 2010-07-14 7:05

>>22
Whether or not that's what it's "for", it gets the job done and it's much simpler (and less line-consuming) than

x = 2
def y():
    x*2

every single time I need a new variable.

Name: Anonymous 2010-07-14 7:35

>>23
python already lets you stick it all on one line
$ cat anus.py
def foo(): print "hax my anus"
def bar(): print "fuck my anus"; foo()
bar()
$ python anus.py
fuck my anus
hax my anus

Name: Anonymous 2010-07-14 15:50

>>2

I don't get it. That wasn't one word.

Name: Anonymous 2010-07-14 16:58

>>25
             |
             |
             |
   (_|       |
             |
         L   |
    _        |
   ( |       |
             |
             |
             |

Name: Anonymous 2010-07-14 19:29

>>24

def cocks(): return False

cocks = lambda: False


The latter is much easier to read when going through lists of variables.

Name: Anonymous 2010-07-14 22:15

Name: Anonymous 2010-07-15 10:57

>>14
CC transcribes “Javascript” as “Jobless rates”. Huh.

Name: Anonymous 2011-02-03 1:27

Name: Anonymous 2011-02-04 17:14

Don't change these.
Name: Email:
Entire Thread Thread List