Notifications
Clear all

[Closed] Lambdas in Maxscript – First Attempt

Hello Andres,
thanks for ur answers.

But still not what I am after.

3.)
for each call of the generator u produce one return. In these cases passing a function pointer is not needed.
I want to call the functionpointer for each call of the function ‘a’ 0 times or n-times

  1. and 2.)
    maybe I dont want to append but call an other function
    2.) now I need to modify the generator. The generator shouldn’t have knowledge about what happens with the numbers.
    1.) I don’t want to pass the parameters to the generator
		
	fn Generate5RandomNumbers onEachNumber: =
	(
		for i = 1 to 5 do
		(
			rnd = random 0 100
			onEachNumber rnd
		)
	)

        fn FuncWithAllLotOfParameters a b c = (print (a+b+c) -- whatever) 
	
	fn main a b c  =
	(

     -- I dont want to pass a b and c to the generator func,
     -- maybe I use the generator somewhere else where I don't need a b c there iside the lambda

		Generate5RandomNumbers \ 
			onEachNumber:(fn λ rndNumber = (
                                FuncWithAllLotOfParameters a b c -- error :  no closure so no access to main arguments here
				print ("Generated: " + rndNumber as string)
			))	

                Generate5RandomNumbers \ 
                      onEachNumber:(fn λ rndNumber = (
                                 print ("Generated: " + rndNumber as string)
        ))	
				
				
	)

main 1 2 3

Maybe a global dictionary whould be a solution where I store the variables that I need inside a lambda body.
But I am not sure if that is a better solution than using globals for every single variable.

Thanks and Happy New Year!

I really don’t understand what is the relationship between your ‘generator function’ and your ‘function with a lot of parameters’, I mean, what do you want to get with all that. You apply a function to a random number that you don’t use in this function.
Perhaps a real example could help.

But nevermind, it’s been funny to examine these possibilities of maxscript.

One more try… not what you want but perhaps useful.


(
	struct LAMBDA 
	(
		params,
		v1, v2, v3, v4, v5,
		vars = #(&v1, &v2, &v3, &v4, &v5),
		action,
		on create do
		(
			for v = 1 to params.count do *vars[v] = params[v]
		)
	)
	
	fn Generate5RandomNumbers LambdaStruct onEachNumber: =
	(
		for i = 1 to 5 do
		(
			rnd = random 0 100
			format "rnd= %
" rnd
			onEachNumber LambdaStruct rnd
		)
	)
	
	fn main a b c =
	(
		lambda1 = LAMBDA params:#(a,b,c) 	
		lambda2 = LAMBDA params:#(a,b)  	
			
		Generate5RandomNumbers lambda1 onEachNumber:(fn whatToDo lambda1 rndNumber = (
								lambda1.action = (fn FuncWith3Parameters a b c = (format "Sum parameters= %
" (a+b+c)))
								lambda1.action lambda1.v1 lambda1.v2 lambda1.v3 
								print ("Generated: " + rndNumber as string)
							))
		Generate5RandomNumbers lambda2 onEachNumber:(fn whatToDo lambda2 rndNumber = (
								lambda2.action = (fn FuncWith3Parameters a b = (format "Product parameters= %
" (a*b)))
								lambda2.action lambda2.v1 lambda2.v2 
								print ("Generated: " + rndNumber as string)
							))
		ok
	)
		
	main 1 2 3
	ok
)
	

Simplified version:

Still don’t like that I need to access the variables by v1 v2 v3 . a way to give them names would be great.
Thats why i thought about dictionaries.

(
	struct CLOSURE 
	(
		v1, v2, v3, v4, v5
	)
	
	fn Generate5RandomNumbers closure onEachNumber: =
	(
		for i = 1 to 5 do
		(
			rnd = random 0 100
			format "rnd= %
" rnd
			onEachNumber closure rnd
		)
	)
    
        fn FuncWith3Parameters a b c = (format "Sum parameters= %
" (a+b+c))

        fn FuncWith2Parameters a b = (format "Product parameters= %
" (a*b))
	
	fn main a b c =
	(
			
		Generate5RandomNumbers (CLOSURE a b c) \
                        onEachNumber:(fn whatToDo closure rndNumber = (
                                FuncWith3Parameters closure.v1 closure.v2 closure.v3 
                                print ("Generated: " + rndNumber as string)
		    ))
                
		Generate5RandomNumbers (CLOSURE a b) \
                        onEachNumber:(fn whatToDo closure rndNumber = (
                                FuncWith2Parameters closure.v1 closure.v2 
                                print ("Generated: " + rndNumber as string)
		    ))

		ok
	)
		
	main 1 2 3

	ok
)

OK. Last try:


(
	local a, b, c, d, f, g, h, k, m, theString	--	Variable names
		
	-- Functions to extend with lambda expressions
	fn Generate5RandomNumbers onEachNumber: =
	(
		for i = 1 to 5 do
		(
			rnd = random 0 100
			onEachNumber  rnd
		)
	)
	fn GenerateRandomWord onEachChar: =
	(
		for i = 1 to 3 do
		(
			rnd = random 65 90
			char = bit.intAsChar rnd
			onEachChar  char
		)
	)
	----------------------------------------
	
	-- Predefined lambda expressions
	fn FuncWith3Parameters = (format "SUM parameters= %
" (a+b+c))
	fn FuncWith2Parameters = (format "PRODUCT parameters= %
" (a*b))
	fn FuncString = (format "% =>" theString)
	----------------------------------------
		
	-- Main function receiving variables 
	-- The '&' is just to allow changing variables values inside the 'main function'
	-- If not needed, you can avoid it
	fn main &a &b &c &theString=
	(
		Generate5RandomNumbers  \
                        onEachNumber:(fn whatToDo  rndNumber = (
								FuncWith3Parameters()
                                print ("Generated1: " + rndNumber as string)
		    ))
			
		print "
******
"	
		a = 3; b = 25	--	You can assign new values to variables inside 'main function'
			
		Generate5RandomNumbers  \
                        onEachNumber:(fn whatToDo  rndNumber = (
								FuncWith2Parameters()
                                print ("Generated2: " + rndNumber as string)
		    ))
			
		print "
******
"	
		theString = "HELLO BIG"	--	You can assign new values to variables inside 'main function'

		GenerateRandomWord  \
                        onEachChar:(fn whatToDo  rndChar = (
								FuncString()
                                print ("Generated Char: " + rndChar)
		    ))
		ok
	)
	
	a = 1; b = 2; c = 3; theString = "Hello"	--	Initialize needed variables only
	main &a &b &c &theString 
	ok
)

What you guys trying to do is not ‘lambda’ expressions (functions).

It doesn’t look like a lambda and doesn’t work like a lambda.
MaxScript doesn’t have lambdas, but it’s robust enough to provide a work around lambdas.

It would be more helpful and it will make more sense if we change the way of discussion to –

“How to implement a solution made with lambda via MAX Script?

and put a working code on another language with using of lambda

All code above is just bad and weird examples of mxs coding.

I would say it’s some sort of obfuscation

You’ve discovered my plan!

I allways use lambda expressions C# for query.
Some posts below there’s a proposal for array query using lambda expressions in maxscript.

Page 3 / 4