by Otto » Tue Jun 25, 2024 2:23 pm
add) METHOD FILTER in frankDB
The macro #define COMPILAR(x) &( "{ || " + x + " }" ) is a definition that transforms the parameter x into a code block (known as "Codeblock Evaluation").
In Harbour and related dialects (such as xHarbour and FiveWin), code blocks can be evaluated at runtime.
Let's break down what happens when COMPILAR(x) is used:
Macro Definition: #define COMPILAR(x) &( "{ || " + x + " }" )
x is the input parameter.
The expression is converted into a string "{ || " + x + " }".
&() is a metaprogramming technique used in Harbour to treat strings as executable code.
When COMPILAR(::cFilter) is used, the following happens:
Suppose ::cFilter has the value "::topic == 'heute'".
COMPILAR(::cFilter) expands to &( "{ || ::topic == 'heute' }" ).
In Harbour and related dialects like xHarbour and FiveWin, code blocks (often referred to as "codeblocks") are a powerful feature. They allow you to create anonymous functions or chunks of code that can be passed around and executed later. Let's discuss their performance characteristics and behavior at runtime.
Code Blocks Performance
Execution Speed:
Fast Execution: Once created, the execution of code blocks is quite fast. They are similar to inline functions or lambdas in other programming languages.
Overhead: The initial creation of a code block can have some overhead, especially if it involves complex expressions. However, this overhead is usually minimal compared to the speed of execution.
Memory Usage:
Efficient Use: Code blocks are typically small in terms of memory footprint. They store the instructions to be executed and any local variables captured when the block is created.
Garbage Collection: Harbour has garbage collection mechanisms to manage memory used by code blocks. Unreferenced code blocks are cleaned up automatically.
Flexibility:
Dynamic Behavior: Code blocks provide a high degree of flexibility, allowing you to create dynamic and reusable pieces of code. They are particularly useful for callbacks, event handlers, and iterative operations.
Maintainability: Code blocks can make code more concise and maintainable, reducing boilerplate code and enhancing readability.
Use Case Example
Here is a practical example to illustrate the use of code blocks and their performance characteristics:
harbour
Code kopieren
// Define a code block to filter items
cFilter := "nAge > 18"
bFilter := COMPILAR(cFilter) // Create the code block using COMPILAR macro
// Example data
aData := { { "John", 20 }, { "Jane", 17 }, { "Doe", 22 } }
// Apply filter using AEVAL and code block
AEVAL(aData, {|aItem| IF(EVAL(bFilter), QOut(aItem[1]), NIL ) })
In this example:
COMPILAR(cFilter) creates a code block from the filter string cFilter.
EVAL(bFilter) evaluates the code block for each item in the array aData.
QOut(aItem[1]) outputs the names of people who are older than 18.
Performance Considerations
Speed: The execution of EVAL(bFilter) is very fast because the code block is precompiled. It does not involve parsing or compiling the filter string at each execution.
Overhead: The overhead of creating the code block COMPILAR(cFilter) is minimal and occurs only once.
Conclusion
Code blocks in Harbour are efficient and powerful constructs. They provide fast execution once created and are flexible enough to handle dynamic behavior in your applications.
While there is some initial overhead in creating them, this is generally outweighed by the benefits they offer in terms of performance and maintainability. Thus, they are a valuable tool for optimizing and organizing code in Harbour-based applications.
& interprets "{ || ::topic == 'heute' }" as a code block and evaluates it.
This means that COMPILAR(::cFilter) creates a code block at runtime containing the condition ::topic == 'heute' and evaluates it.
Example usage:
harbour
Code kopieren
::cFilter := "::topic == 'heute'"
oDlg:SetFilter(COMPILAR(::cFilter))
Here, a filter is set that only considers records where ::topic equals 'heute'.
In summary, COMPILAR(x) dynamically creates a code block from the string x at runtime,
which can then be evaluated. This enables flexible and dynamic code generation and execution in Harbour and related dialects.
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club********************************************************************