Merge two codeblocks

Merge two codeblocks

Postby frose » Wed Oct 12, 2011 9:13 am

Hi to all,

I'm looking for a code snippet to merge two codeblocks, something like:

Code: Select all  Expand view

FUNCTION Codeblock_Merge( cCodeblock1, cCodeblock2 )

   // Returns a codeblock with the parameters and functions of both codeblocks <cCodeblock1> and <cCodeblock2>

   // Example: cCodeBlock1 := "{ | parameter1| function1( parameter1) }
   //          cCodeBlock2 := "{ | parameter2| function2( parameter2) }
   //          cCodeBlock  := "{ | parameter1, parameter2| function1( parameter1), function2( parameter2) }

   LOCAL cCodeblock := cCodeblock1 + cCodeblock2

RETURN cCodeblock
 

Has anybody done this or a similar thing before?
Windows 11 Pro 22H2 22621.1848
Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
Harbour 3.2.0dev (r2008190002)
FWH 23.10 x86
User avatar
frose
 
Posts: 392
Joined: Tue Mar 10, 2009 11:54 am
Location: Germany, Rietberg

Re: Merge two codeblocks

Postby James Bott » Wed Oct 12, 2011 12:47 pm

Maybe:

FUNCTION Codeblock_Merge( codeblock1, codeblock2 )
return {| parameter1, parameter2 | eval(codeblock1, parameter1 ), eval(codeblock2, parameter2) }
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Merge two codeblocks

Postby frose » Thu Oct 13, 2011 12:21 pm

James,

thank you for your tip.

After some tries it works even with detached LOCAL's, by omitting the parameter(s) in the RETURN statement:

Code: Select all  Expand view
FUNCTION Main()
   
   LOCAL oDlg
   LOCAL oGet1
   LOCAL oGet2
   LOCAL cInfo1 := [This is the first codeblock!]
   LOCAL cInfo2 := [This is the second codeblock!]
   LOCAL cCodeBlock1 := "{ | cInfo1 | MsgInfo( cInfo1 ) }"
   LOCAL cCodeBlock2 := "{ | cInfo2 | MsgInfo( cInfo2 ) }"

   DEFINE DIALOG oDlg size 400, 400 TITLE "Merging two code blocks"

   @ 1,  2 SAY "Codeblock 1:" OF oDlg
   @ 1,  6 GET oGet1 VAR cCodeblock1 OF oDlg SIZE 120, 10
   @ 2,  2 SAY "Codeblock 2:" OF oDlg
   @ 2,  6 GET oGet2 VAR cCodeblock2 OF oDlg SIZE 120, 10
   
   @ 3,  2 BUTTON "&End" SIZE 30, 12 OF oDlg ACTION oDlg:End() CANCEL
   @ 3, 08 BUTTON "&Eval 1" OF oDlg SIZE 30, 12 ACTION Eval( Detached_Local( &( cCodeblock1 ), cInfo1 ) )
   @ 3, 14 BUTTON "&Eval 2" OF oDlg SIZE 30, 12 ACTION Eval( Detached_Local( &( cCodeblock2 ), cInfo2 ) )
   @ 3, 20 BUTTON "&Eval 1+2" OF oDlg SIZE 30, 12 ACTION Eval( Codeblock_Merge( &( cCodeblock1 ), &( cCodeblock2 ), cInfo1, cInfo2 ) )

   ACTIVATE DIALOG oDlg

RETURN NIL

FUNCTION Detached_Local( bCodeblock, cInfo )
RETURN { || Eval( bCodeblock, cInfo ) }

FUNCTION Codeblock_Merge( bCodeblock1, bCodeblock2, cInfo1, cInfo2 )
RETURN { || Eval( bCodeblock1, cInfo1 ), Eval( bCodeblock2, cInfo2 ) }

Now there is still some work to be done to make it more generic, but the basic problem is solved :D
Windows 11 Pro 22H2 22621.1848
Microsoft (R) Windows (R) Resource Compiler Version 10.0.10011.16384
Harbour 3.2.0dev (r2008190002)
FWH 23.10 x86
User avatar
frose
 
Posts: 392
Joined: Tue Mar 10, 2009 11:54 am
Location: Germany, Rietberg


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: cmsoft and 84 guests