Forum

Get values from Dictionary(TKey,(Result from Fuction)

hi guys ,

hope you can help me to understand and get this .

i,d like to make a dictionary of Dictionary<int,a function of (double , double , bool >> and by getting the values of the dictionary get the result of that function inside .

i was trying this

Dictionary<int, Func<double,double,bool,double>> Dicti1 = new Dictionary<int,Func<double,double,bool,double>>();

//thinking first 3 are fucntion parameters last the result 
//Func<T1, T2, T3, TResult>

so i did try adding something like this function :

public double FuncDoble1(double x, double y , bool Ad)
{
	
	if (Ad)
	{
		x = x + y;
	}
	if (!Ad)
	{ 
	var r = new Random();
	double z = r.NextDouble()-0.5 ;
		x = x - y + z ;
	}
	
    return x;
}

and i wanted to do things like this

Dicti1.Add(1,FuncDoble1(2,2,true));//this  does not work 
//
var val  = Dicti1.Values.ToList();
            FTest.AssignFrom(val);
//

how i could do this of adding a functions parameters to dictionary and returns the result of it as value .

thanks in advance

in case you simply want to store the result of your function in a dictionary:

Dictionary<int, double> Dicti1 = new ...
Dicti1.Add(1, FuncDoble1(2, 2, true));

in case you want to store the function in your dictionary and call the function later on:

Dictionary<int, Func<double, double, bool, double>> Dicti1 = new ...
Dicti1.Add(1, FuncDoble1);
// At some point later you could do
var aFunc = Dicti1[1](1);
var result = aFunc(2, 2, true);

in case you want to create a new function with some arguments already applied use lambda notation to let the compiler build some anonymous functions for you:

Dictionary<int, Func<bool, double>> Dicti1 = new ...
Dicti1.Add(1, (a) => FuncDoble1(2, 2, a));
// At some point later you could do
var anotherFunc = Dicti1[1](1);
var result = anotherFunc(true);

i tried to read your thoughts and came up with a solution that memoizes function calls.
i hope that is what you wanted.

when asking for a functions output with a new combination of input values then we obviously need to call that function and store the result. if later on a known combination of input values reoccurs we can use the stored result.

note that i didn’t include any code to empty the ditctionary. so this dictionary will grow until - you know - someone stands up and shouts “memory leak!”…

more explanation in the code.

memoize func.zip (15.9 kB)

in this version i just replaced the Tuple<double, double, bool> which occured over and over in the code with a type alias (MyKey). that way it gets more readable:

namespace VVVV.Nodes
{	
	using MyKey = Tuple<double, double, bool>;
	
	[PluginInfo(Name = "Memoize", Category = "Value", Version = "Double_Double_Bool_Double",
				Help = "Memoize a Results of a Function", Tags = "", 
				Bugs = "To make this work with different functions we either need to empty the dictionary when a new function is connected or make the function part of the key.")]
	public class Double_Double_Bool_DoubleValueMemoizeNode : IPluginEvaluate
	{
		// we accept any function that has the signature 
		// double -> double -> bool -> double
		[Input("Func")](Input("Func"))
		ISpread<Func<double, double, bool, double>> FFunc;
		
		[Input("X")](Input("X"))
		ISpread<double> FX;
		
		[Input("Y")](Input("Y"))
		ISpread<double> FY;
		
		[Input("Ad")](Input("Ad"))
		ISpread<bool> FAd;		

		[Output("Output")](Output("Output"))
		ISpread<double> FOutput;

		Dictionary<MyKey, double> Results = new Dictionary<MyKey, double>();
		
		public void Evaluate(int SpreadMax)
		{
			FOutput.SliceCount = SpreadMax;

			for (int i = 0; i < SpreadMax; i++)
			{
				// generate a key that is made of 
				// current x, current y, current ad input value
				MyKey key = new MyKey(FX[i](i), FY[i](i), FAd[i](i));
				
				// the result of the function will be stored in this variable
				double result;
				
				// lets have a look if we already have a result of the function for 
				// those three input values.
				// if we have a result we dont need to call the function
				// if have no result we need to call the function and store the result in the dictionary
				if (!Results.TryGetValue(key, out result))
				{
					result = FFunc[i](FX[i](i](FX[i), FY[i](i), FAd[i](i));
					Results[key](key) = result;
				}			
				
				FOutput[i](i) = result;
			}
		}
	}
}

wow thanks guys , lot of things to learn and digest , going to play with it now i,ll get back ;D

hi , i got some progress thx but still many doubts , how could i apply a timer to each dictionary key so the value of dictionary is the time counting itself , i saw this Action Dictionary but it may not be for that

what exactly are you trying to do with it?

hi tonfilm , in general learning more c# and specially dictionary staff i love it so very glad for previous examples of gregsn and elias ,

in this particular , i,m trying to accomplish two task one is trying to make some animations basic plugs i made similar to this drag&slide file attached but dictionary ID based .

So instead if inputting slice based , inputting like id(key) and value based .

i made this draft plug as i thought it could behave the dictionary version also attached .

you input keys and values of the id you want to modify then once the id is not present in the input the friction part takes place (this will replace the Add pin) , then a getid pin which gets values and id of existing keys something like the custom in box2d .

in other hand but related because i think one task helps the other , i wanted to do a id alive time plug dictionary based this is why i wanted to associate a timer to each key been the timer the value of dictionary .

i have tried a few approaches but not good , not sure if it will be best to make all into new class and if so how .

take into account i am quite new to c# so although sometimes i get things quickly other times i feel quite blind about simple things , so any obvious thing you write or show might be a treasure for me hehe ;D

hope you understand the explanation i can make more examples if needed . thanks

Drag&Slide.zip (8.9 kB)
Drag_DictionaryBehaviour.zip (11.8 kB)

hi

i got the AliveTime Dictionary working not sure if it is too elegant so very welcome to received corrections or more efficient ways .

About the Drag animation i still do not know how to convert it to dictionary based , i thought that making a function of the friction part and put it in the dictionary as result will do it or probably a class of it but do not know how to get lastvalue into a function in the a class in the case this is the way.

so if you guys can guide me a bit on the drag to dictionary based would be great ;D .

AliveTime (ID).zip (11.7 kB)