Title: Push/Pop Groove Post by: sciurius on January 20, 2021, 09:53:04 AM Sometimes I want to temporarily switch to another groove, for example
Code: ... This can be done with StackValue: Code: ... provided there was a groove in the first place. If this is not necessarily so, we need ugly tricks(*): Code: ... or something similar. But if there was no groove in the first place, there is now which may lead to unexpected results. Even though I admit that a 'no groove' situation is uncommon, is there a (better) way to handle this? (*) Footnote: Also due to the limitation that we cannot test for empty values. Also: $_LastGroove returns $_Groove is there was no previous groove. Title: Re: Push/Pop Groove Post by: bvdp on January 20, 2021, 04:21:16 PM Starting from the bottom up ... when switching to a groove code sets $_LastGroove before switching. If there is that happens to be Null (actually, "") then the current groove name is used. I thought that made as much sense as anything.
When MMA initializes it sets both $_Groove and $_LastGroove to "". I see the problem you have. I think the proper way to handle this is to have a special value to test against. Since we don't really support proper strings we need to fake it. I think the easiest might be to pretend we do support quotes ... perhaps we could add "" as a special? So, then we could have If $_LastGroove <> "" ... and be happy? "" or '' or Null or None ... the later two don't excite me. Title: Re: Push/Pop Groove Post by: bvdp on January 20, 2021, 10:10:08 PM I've added a new test condition ISNONE which should handle these cases. Code is in the attached patch file.
What it does is add a test for a NONE or "" content of a variable. So, this will work: Code: if IsNone _LastGroove Note, the argument MUST be a system variable name (_LastGroove) or a user variable name (FOO). No leading $ in either case. Going back to your original example using $_StackValue you would need to use an intermediate variable: Code: zz = $_Stackvalue I think this matches the syntax of NDEF and DEF. Before I commit this, do we need a NotNone? What to do with an arg which isn't a defined name? Right now it errors ... which means one should use a IF DEF before the call. Title: Re: Push/Pop Groove Post by: bvdp on January 21, 2021, 02:34:57 AM Just a thought ... the way your initial code and my sample above use StackValue really defeats the use of this function ... if you need to pull the stacked value and assign it to a variable, why not just do the original assignment to the variable instead of StackValue?
Would it be a smart thing to have new macro $_CopyStackValue or something like that ... which just grabs a copy but leaves the value there? Then we could do something like: Code: StackValue $_Groove And, yes, this points to a need for a NotNone conditional. Title: Re: Push/Pop Groove Post by: sciurius on January 21, 2021, 09:35:19 AM Great work!
So now a variable can have an empty value. I would suggest to name the tests IsEmpty and IsNotEmpty. The value is the empty string, 'none' is a python term (for undefined). But... Just like me you apparently didn't realise that an empty variable is the same as an empty list? This works: Code: If EQ $Var[] 0 So apparently we already can do everything I asked for... We can even add more trickery: what if $Var[] returned -1 if Var is not defined? Code: If LE $Var[] 0 Trivial patch attached. Title: Re: Push/Pop Groove Post by: bvdp on January 22, 2021, 01:42:47 AM Okay, I've applied the patch. Thanks.
I'm in agreement that IsNone is not a great choice. I'm not convinced about IsEmpty and esp. not about IsNotEmpty ... long names with no short cut. But, I can't think of anything shorter. And they are obvious and make sense. I'll work the patches in the next few days and post new patch, hopefully this weekend. Yeah, I keep forgetting about list notation for variable in MMA. Terribly useful stuff. Problem is, of course, that we only remember stuff we use on a regular basis. Oh well, that why there are internet forums! So, now there will be 2 ways to do the same thing. Not a bad thing :) Title: Re: Push/Pop Groove Post by: bvdp on January 22, 2021, 01:51:22 AM You know, I just applied the patch ... but ... do we want this? Before the patch:
Print this is Foo $Foo[] would generate an error. After the patch we get "This is Foo -1" Mind you, this is only evident when using slice notation .. so we can justify by saying "If you're smart enough to use slice, you should be smart enough to understand all this stuff"! Title: Re: Push/Pop Groove Post by: sciurius on January 22, 2021, 06:54:22 PM This would be illegal before we introduced the [] semantics anyway.
The idea was that the empty slice returns the length of the value. A negative length indicating an undefined variable may sound weird, depending how you see it. So yes, [] is a 'power' operation, for power users only :) . Title: Re: Push/Pop Groove Post by: bvdp on January 22, 2021, 11:38:07 PM We support so-called "power chords" (just major chords with the 3rd missing) so why not have power operations and power users to compete the circle :)
|