Hyperbee.Expressions 1.0.0-develop.241121204357

This is a prerelease version of Hyperbee.Expressions.
dotnet add package Hyperbee.Expressions --version 1.0.0-develop.241121204357                
NuGet\Install-Package Hyperbee.Expressions -Version 1.0.0-develop.241121204357                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hyperbee.Expressions" Version="1.0.0-develop.241121204357" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hyperbee.Expressions --version 1.0.0-develop.241121204357                
#r "nuget: Hyperbee.Expressions, 1.0.0-develop.241121204357"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hyperbee.Expressions as a Cake Addin
#addin nuget:?package=Hyperbee.Expressions&version=1.0.0-develop.241121204357&prerelease

// Install Hyperbee.Expressions as a Cake Tool
#tool nuget:?package=Hyperbee.Expressions&version=1.0.0-develop.241121204357&prerelease                

Welcome to Hyperbee Expressions

Hyperbee.Expressions is a C# library that extends the capabilities of expression trees to handle asynchronous workflows and other language constructs.

Features

  • Async Expressions

    • AwaitExpression: An expression that represents an await operation.
    • AsyncBlockExpression: An expression that represents an asynchronous code block.
  • Using Expression

    • UsingExpression: An expression that automatically disposes IDisposable resources.
  • Looping Expressions

    • WhileExpression: An expression that represents a while loop.
    • ForExpression: An expression that represents a for loop.
    • ForEachExpression: An expression that represents a foreach loop.
  • Expression Optimizers

    • OptimizerBuilder: An composite optimizer builder.
    • ConstantFoldingOptimizer: An optimizer that precomputes constant expressions.
    • OperatorReductionOptimizer: An optimizer that simplifies arithmetic and logical expressions.
    • StructuralReductionOptimizer: An optimizer that removes unreachable code and consolidates nested or redundant constructs.
    • ValueBindingOptimizer: An optimizer that simplifies and inlines, variable, constant, and member access operations.
    • SubExpressionCachingOptimizer: An optimizer that identifies and caches repeated subexpressions within a tree.
  • Supports Fast Expression Compiler (FEC) for improved performance.

Examples

Asynchronous Expressions

The following example demonstrates how to create an asynchronous expression tree.

When the expression tree is compiled, the BlockAsyncExpression will auto-generate a state machine that executes AwaitExpressions in the block asynchronously.

the `BlockAsync` expression will generate a state machine that executes the expressions in the block asynchronously.

```csharp

public class AsyncExample
{
    public async Task ExampleAsync()
    {
        // Create an async block that calls async methods and assigns their results

        var instance = Constant( this );
        var result1 = Variable( typeof(int), "result1" );
        var result2 = Variable( typeof(int), "result2" );

        var asyncBlock = BlockAsync(
            [result1, result2],
            Assign( result1, Await(
                Call( instance, nameof(FirstAsyncMethod), Type.EmptyTypes )
            ) ),
            Assign( result2, Await(
                Call( instance, nameof(SecondAsyncMethod), Type.EmptyTypes, result1 )
            ) )
        );

        // Compile and execute the async block
        var lambda = Lambda<Func<Task<int>>>( asyncBlock );
        var compiledLambda = lambda.Compile();
        var resultValue2 = await compiledLambda();

        Console.WriteLine( $"Second async method result: {resultValue2}" );
    }

    public static async Task<int> FirstAsyncMethod()
    {
        await Task.Delay( 1000 ); // Simulate async work
        return 42; // Example result
    }

    public static async Task<int> SecondAsyncMethod( int value )
    {
        await Task.Delay( 1000 ); // Simulate async work
        return value * 2; // Example result
    }
}

Using Expression

The following example demonstrates how to create a Using expression.

public class UsingExample
{
    private class DisposableResource : IDisposable
    {
        public bool IsDisposed { get; private set; }
        public void Dispose() => IsDisposed = true;
    }

    public void UsingExpression_ShouldDisposeResource_AfterUse()
    {
        var resource = new TestDisposableResource();

        var disposableExpression = Expression.Constant( resource, typeof( TestDisposableResource ) );
        var bodyExpression = Expression.Empty(); // Actual body isn't important

        var usingExpression = ExpressionExtensions.Using( 
            disposableExpression, 
            bodyExpression 
        );

        var compiledLambda = Expression.Lambda<Action>( reducedExpression ).Compile();

        compiledLambda();

        Console.WriteLine( $"Resource was disposed {resource.IsDisposed}." );
    }
}

Credits

Special thanks to:

Contributing

We welcome contributions! Please see our Contributing Guide for more details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.