开发者

Programming language idiom for "if object.value == some_value then object.value = some_other_value"

开发者 https://www.devze.com 2023-03-12 14:43 出处:网络
I\'ve been wondering myself multiple times if, and if not, why not, there is an idiom/shortcut for the following pseudocode:

I've been wondering myself multiple times if, and if not, why not, there is an idiom/shortcut for the following pseudocode:

if object.value == some_value then object.value = some_other_value

For example, in JavaScript I sometimes write:

if (documen开发者_StackOverflowt.getElementById("toggledDiv").style.display == "block") {
    document.getElementById("toggledDiv").style.display = "none";
}

This seems to be rather tedious. Is there a name for this idiom, and is there a more concise syntax for this in common programming languages?

Thank you!

Edit: To be more precise, I don't care about the braces, but about that you have to reference the attribute at least to times. I'd like to have something like that (pseudocode):

test ( object.value ):
    if (it > 0) it = 0;
    else it -= 1;

e. g.:

test (document.getElementById("toggledDiv").style.display):
    if (it == "block") it = "none";

where it is a keyword that references the tested property. I'm just wondering no programming language seems to have implemented that.

Upate:

Okay, in the meantime I have found something which is a little bit short, but only works in JavaScript:

(function(s){
  if(s.display=='block') 
    s.display="none";
  else 
    s.display='block'; 
})(document.getElementById("toggledDiv").style)


Well, in Haskell, and other FP languages, conditionals, like ternary operators, are first-class expressions, so you can float the assignment out,

a = if x == y then x else z

Making the code a lot cleaner.


I don't know of any languages that support it out of the box, but there are a number that support defining new operators. In theory, you could write something like the following (in psuedo-code)

operator <T> T toggle(T value, T[] values) {
    for(int i=0; i<values.size(); i++) {
        if(value == values[i]) {
            if(values.size() > (i+1)) {
                return values[i+1]
            } else {
                return values[0]

            }
         }
    }
    error "value $value not found in value list $values"
}

Assuming my psuedo-code is correct, this would allow you to do the following:

v = true;
v = v toggle [true, false] ;       // v == false
v = v toggle [true, false] ;       // v == true (loops to beginning of list
v = v toggle [false, true, true] ; // v == true, since true is both the 2nd and 3rd elements of the list

You could also two versions:

  • One that, if the values isn't in the list, returns the original value
  • One that, if the values isn't in the list, throws an error (what my version did)

The former would be less of a toggle and more of... what you asked for, I guess. I was basing the code off the toggle use case from the previous note about css/block/none, where toggle is the more common behavior.

Assuming the language supports it, you could write a toggle= operator too:

v toggle= ['none', 'block']


In JavaScript you can do:

var d = document.getElementById("toggledDiv");
if (d.style.display == "block") d.style.display = "none";
0

精彩评论

暂无评论...
验证码 换一张
取 消