Kara-Moon Forum
March 28, 2024, 08:29:00 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: You can go back to the main site here: Kara-Moon site
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Push/Pop Groove  (Read 2988 times)
sciurius
Sr. Member
****
Posts: 443



« on: January 20, 2021, 09:53:04 AM »

Sometimes I want to temporarily switch to another groove, for example

Code:
...
Groove Metronome4
1 z
...back to original groove

This can be done with StackValue:

Code:
...
StackValue $_Groove
Groove Metronome4
1 z
Groove $_StackValue

provided there was a groove in the first place. If this is not necessarily so, we need ugly tricks(*):

Code:
...
DefGroove DummyXyZZy
Groove Metronome4
1 z
Groove DummyXyZZy

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.
Logged
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #1 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.

Logged

My online life: http://www.mellowood.ca
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #2 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
        print There is no LastGroove
    else
        Groove $_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
     if IsNone zz ...

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.

* pat.txt (2.18 KB - downloaded 162 times.)
Logged

My online life: http://www.mellowood.ca
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #3 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
Groove Metronome4
1 z
If IsNone _CopyStackValue
   Print Nothing saved
Else Groove $_StackValue
endif

And, yes, this points to a need for a NotNone conditional.
Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 443



« Reply #4 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
print Var is Empty
EndIf

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
print Var is not defined, or Empty
EndIf

Trivial patch attached.

* macro.py.patch.txt (0.59 KB - downloaded 168 times.)
Logged
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #5 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 Smiley
Logged

My online life: http://www.mellowood.ca
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #6 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"!


Logged

My online life: http://www.mellowood.ca
sciurius
Sr. Member
****
Posts: 443



« Reply #7 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 Smiley .
Logged
bvdp
Kara-Moon Master
****
Posts: 1436


WWW
« Reply #8 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 Smiley
Logged

My online life: http://www.mellowood.ca
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.051 seconds with 20 queries.