indexedFunction in Java?

Post Reply New Topic
RELATED
PRODUCTS

Post

Wow. finally got some parts to run in my JavaScript to Java Conversion! Woot thanks for the fresh tutorial Moß!

Java is next level for me though. I'm wrapping my head around all the keywords final, public, private, protected. new ways to declare stuff. I have a vary shallow use of OOP but havn't coded anything this deep in a very long time. Doing lots of google when I run into this.

My method of getting there is line by line, I convert the code i wrote in JavaScript to Java and use the Moßgraber open source as my savior and guide. I mean this is how I wrote all my JavaScript bitwig stuff too. thank you <3

So here is my question...

Its super simple to create an indexed wrapper function in JavaScript because of its lack of typed variables.

Code: Select all

function makeIndexedFunction(index, f) {
  function indexedFunc(value) {
    f(index, value);
  }
	return indexedFunc
}
This can be pumped into observers for example for quick and dead simple identifiers on the callback.

but in Java its much more complicated.

I'm looking through the Moßgraber code and finding this...

de/mossgrabers/framework/observer/IIndexedValueObserver.java

Code: Select all

// Written by Jürgen Moßgraber - mossgrabers.de
// (c) 2017-2020
// Licensed under LGPLv3 - http://www.gnu.org/licenses/lgpl-3.0.txt

package de.mossgrabers.framework.observer;
/**
 * An observer for indexed value change.
 *
 * @author J&uuml;rgen Mo&szlig;graber
 *
 * @param <V> The type of the value
 */
@FunctionalInterface
public interface IIndexedValueObserver<V>
{
    /**
     * Called if the value has changed.
     *
     * @param index THe index of the value
     * @param value The new value
     */
    void update (int index, V value);
}
Then there is an example of use:

Code: Select all

/**
 * An abstract track bank.
 *
 * @author J&uuml;rgen Mo&szlig;graber
 */
public abstract class AbstractTrackBankImpl extends AbstractChannelBankImpl<TrackBank, ITrack> implements ITrackBank
{
    /** {@inheritDoc} */
    @Override
    public void addNameObserver (final IIndexedValueObserver<String> observer)
    {
        for (int index = 0; index < this.getPageSize (); index++)
        {
            final int i = index;
            this.getItem (index).addNameObserver (name -> observer.update (i, name));
        }
    }
    
}
Ok then I just crapped myself. There is this class abstraction and interface definitions that I'm completely in the dark on. or am I reading too much into this?

I'm also unsure why it is necessary to have such a deep track Abstraction that Moßgraber set up. Is that more just to understand the API and have a clean implementation for the code so you can add quick overrides and such? But it also seems like it is a chore to maintain if there is added features.

I'm going to dig into this further but if you guys got pointers or comments(besides go home noobie :lol: ) to get me on track quickly I will be listening. Thanks!
----------------------------------------------------------------------
/CTRL → http://slashctrl.io
Music & mixes → http://soundcloud.com/kirkwoodwest

Post

another programming concept represented in many of the moßgraber code is this
 

Code: Select all

function doObject (object, f)
{
return function ()
{
f.apply (object, arguments);
};
}
i'm using this all over the place. super duper helpful for callbacks that need to go to objects.

Now back to googling how to achieve in Java.
----------------------------------------------------------------------
/CTRL → http://slashctrl.io
Music & mixes → http://soundcloud.com/kirkwoodwest

Post

Ok... back with more answers...

the doObject() javascript function can be represented in Java using object::function. The :: syntax allows you to pass in a method reference( static or instanced) to a callback. https://www.geeksforgeeks.org/double-co ... r-in-java/

Got to learn more about Lambda expressions now since this all seems to be related, somehow there is a way to wrap it along with additional parameters like an index or some other type of id.
----------------------------------------------------------------------
/CTRL → http://slashctrl.io
Music & mixes → http://soundcloud.com/kirkwoodwest

Post

Forget about the JavaScript stuff, these are only workarounds for its' OO limitations.

IMHO Java is actually much easier to read than JavaScript (e.g. try to understand the code of any of the web JS frameworks, good luck :-)).
What might confuse you are the Lambda expressions. Simply think about them as an easy implementation of an interface with only one method. This is heavily used for callbacks, which before Java had Lamdas, had to be implemented with internal classes which looked horrible.
I guess you should read about all basic Java concepts first to get some basic understanding.

I use many abstractions because as you might know DrivenByMoss is also available for Reaper. This way I can "simply" exchange the basic adapter to the DAW. Furthermore, I am more stable to changes in the Bitwig API and can hide away workarounds.

Post

Man truthfully I was hoping for a cheat code on how to do it!! :D ... but I'll get after it Moss!

I am definetly enjoying the readability of the language so far. Main thing is I miss the global defintions but this is easily solved by making a class. Its been ages since I've practiced Java while learning OOP, so I could use it in ActionScript and it seems like the language has evolved a bit since then.

Still to this day the lambda still destroys my mind every time in Python and now Java. :D

This makes so much sense withe the abstactions! Very bad ass. Do you also write test code for it?
----------------------------------------------------------------------
/CTRL → http://slashctrl.io
Music & mixes → http://soundcloud.com/kirkwoodwest

Post

Ok. I see the cheat codes now... I hope understanding comes soon about the lambda but I'm afraid I will never fully grok it. Your open source work has really made a difference for me.

Also, thanks to the programming and advanced computers, we have inline syntax highlighting, checking, compilation help, search and basically every modern feature in a code editor that exists now. I attempted to pick up C++ in 92, the Borland C++ Editor was 80x40 characters and pretty much a blank slate. Didn't have any friends or people that could really advance my knowledge base, so I didn't get much further than making a simple calculator.
----------------------------------------------------------------------
/CTRL → http://slashctrl.io
Music & mixes → http://soundcloud.com/kirkwoodwest

Post

___
Last edited by Kirkwood West on Tue Sep 29, 2020 6:08 pm, edited 1 time in total.
----------------------------------------------------------------------
/CTRL → http://slashctrl.io
Music & mixes → http://soundcloud.com/kirkwoodwest

Post

Ok... Cheat codes :D.

Code: Select all

      for (int i = 0; i < CHANNEL_FINDER_TRACK_COUNT - 1; i++) {
        Channel channel = track_bank.getItemAt(i);
        channel.name().markInterested();

        final int index = i;
        channel.name().addValueObserver(name -> this.name_update(name, index));
      }
Then the observer "callback".

Code: Select all

  private void name_update(String s, Integer index) {
    controller_host.println("name: " + s + " : " + String.valueOf(index));
  }
I'm reading Java doesn't have actual callbacks and uses an Observer pattern. So I appologize if I'm using the wrong terminology when discussing these matters.

I don't understand the lambda completly which is probably due to me not understanding programming language types all together. I googled several sources on this subject and its not so clear. I think its because the sources discuss the concepts in deep computer science terminology or the practical use cases are either too basic, vague.

I've read several books on the subject but they in general they speak to a different mind than what I was born and trained with. I lack deep english skills which extends to my ability to understand some concepts presented in these books. I have to read the same pages multiple times to get even a remote understanding and I've tried for years to understand things like this.

Books help me some but I have a strange mind. The best method for me is to have plenty of other code to source ideas from and then find what works and get the work done. Then allow the understanding unfold through experience, time, and conversations with others.

I know that process will never make me a "great" programmer. Which ultimatly sucks but I just have to keep moving forward with my purpose. With what I've learned over the years, I've been able to code many great things in this life from small utilites, web sites, video games and music APIs. Even if I didn't have full understanding of what is going on behind the scenes.
----------------------------------------------------------------------
/CTRL → http://slashctrl.io
Music & mixes → http://soundcloud.com/kirkwoodwest

Post Reply

Return to “Controller Scripting”