M
M
MaM2017-03-14 18:43:58
Programming languages
MaM, 2017-03-14 18:43:58

Are there context-managed functions?

I wrote the bool some_function(int key,bool) code, in fact the key bool stores only the 1\0 states and does not make sense, and roughly speaking it prevents the code from expanding. Then I thought, is there such a feature that, each time, for different call arguments, a separate context is created, which would be saved until the next call. Then I thought more and came to the conclusion that it would be nice to optimize the functions, the calculation of which is possible at the execution stage, that is, the function implements the concept of memoization and checks if the data has changed, if not, it returns the previous value. Then I also thought, it would not be bad to have data with a change indicator (that is, we produce, we change it) and if it has changed, the function is considered, otherwise the return of the previous one. Then I came to the idea that you can go even further, and for the same arguments (instances of objects, functions), create and assign a context to them, or for example other complex storage rules. The question is, are there languages ​​with this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Pavlov, 2017-03-15
@MaM

Use a dictionary Dictionary<KeyCode, bool>instead of multiple variables. This is if the flags are needed somewhere else. And if these flags are needed only in Update(), then they are not needed:

int GetKey(KeyCode key) // 1 or 0
{
    return Input.GetKeyDown(key) ? 1 : 0;
}
void Update()
{
    int hor = GetKey(KeyCode.A) - GetKey(KeyCode.D);
    int ver = GetKey(KeyCode.W) - GetKey(KeyCode.S);
    transform.position += new Vector3(hor, ver, 0) * CameraSpeed;
}

T
Tom Nolane, 2017-03-14
@tomnolane

* brain explosion here *
can be shorter, and in fact - without thinking?
ps better show the code that is now, and what you want in the end - at least in words in the code itself, if you want to be helped
by upd
than the code is "simpler" && less && without problems, so it = better.
As you described in the comment - when you press any key from the keyboard - the key value changes very conveniently and more pragmatically. In any case, you need to specify || specify conditions for key ... and there are many ways to implement it. Though using random.
It is not entirely clear about the "semantic load"key... in the code should be only those things that are used, or "cram" everything, so that it would be harder for reversers to understand it. Key (if needed) can be set at least through switch or timer or through if , and in fact, this is not so important as its actual need.
I would do (for myself) something like this:

if (Input.GetKeyDown (KeyCode)) {
KeyFlag=true;
}
else if (Input.GetKeyUp (KeyCode)) {
KeyFlag=false;
//или через switch

if(KeyFlag==false) {
 // тут код, когда <b>key</b> = false
}
else {
  // тут код, когда <b>key</b> = true
}

simple, not confusing and easy to understand/debug.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question