Skip to main content

RSCG_Utils_Memo by Ignat Andrei

NuGet / site data​

Nuget GitHub last commit GitHub Repo stars

Details​

Info​

Original Readme​

note

RSCG_Utils

Roslyn Source Code Generators Utils

pack to nuget

pack to nuget

Usage

Additional Files​

Allow you to see additional files directly as C# const. For this, please add some .gen. files to the project.

In your csproj

<ItemGroup>
<PackageReference Include="rscgutils" Version="2023.502.835" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="Second.gen.txt" />
<AdditionalFiles Include="first.gen.txt" />
<AdditionalFiles Include="test\Afirst.gen.txt" />
<AdditionalFiles Include="sql/**/*" />
</ItemGroup>

In the code

//see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/raw-string
string x= MyAdditionalFiles.Second_gen_txt;

To debug, you can add into the .csproj

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

More details at http://msprogrammer.serviciipeweb.ro/2023/05/08/file-to-csharp-const/

Json2Class​

If you have an additional json file, you can have a file from this

<ItemGroup>
<AdditionalFiles Include="my.gen.json" />
</ItemGroup>

And you can have from the code


var json = System.Text.Json.JsonSerializer
.Deserialize<GeneratedJson.my_gen_json>(MyAdditionalFiles.my_gen_json);

ArgumentNullException.ThrowIfNull( json );
Console.WriteLine( ":hosts"+json.AllowedHosts );

Memoization of function returns​

Put _MemoPure and the return of the function will be memo-ized

public long Test_MemoPure()
{
Console.WriteLine("calculating type");
return this.GetType().ToString().GetHashCode();
}
public async Task<long> fib(long nr)
{
await Task.Delay(1000);
//Console.WriteLine("calculated value for " + nr);
if (nr <= 1) return 1;
if (nr == 2) return 2;
return await fib(nr - 1) + await fib(nr - 1);
}

public async Task<long> fibonacci_MemoPure(long nr)
{
if (nr <= 1) return 1;
if (nr == 2) return 2;
await Task.Delay(1000);
return await fibonacci(nr - 1) + await fibonacci(nr - 1);

}

And call

fibTest f = new();
Console.WriteLine("first time :" + f.Test());
Console.WriteLine("second time :" + f.Test());

Console.WriteLine(DateTime.Now.ToString("mm_ss"));
Console.WriteLine("no memo :"+await f.fib(5));
Console.WriteLine(DateTime.Now.ToString("mm_ss"));
Console.WriteLine("memo :" + await f.fibonacci(5));
Console.WriteLine(DateTime.Now.ToString("mm_ss"));
Console.WriteLine("FAST memo :" + await f.fibonacci(5));
Console.WriteLine(DateTime.Now.ToString("mm_ss"));


More Roslyn Source Code Generators

You can find more RSCG with examples at Roslyn Source Code Generators

About​

note

Memo the function result

How to use​

Example (source csproj, source files)​

This is the CSharp Project that references RSCG_Utils_Memo

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="rscgutils" Version="2023.914.2016" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files​

Those are taken from $(BaseIntermediateOutputPath)\GX

using System.Collections.Concurrent;

//this is auto-generated by a tool
namespace DemoRSCG_UtilsMemo;
partial class fibTest
{

System.Collections.Concurrent.ConcurrentDictionary<Tuple<long > , long> __cache_DemoRSCG_UtilsMemo_fibTest_fibonacci_MemoPure =new System.Collections.Concurrent.ConcurrentDictionary<Tuple<long >, long>();
//True
public async Task<long> fibonacci (long nr ){
var key= Tuple.Create(nr);
if (__cache_DemoRSCG_UtilsMemo_fibTest_fibonacci_MemoPure.TryGetValue(key, out var result)) return result;
//Console.WriteLine($"not in cache, calculating {key}");
var data= await __wrap_fibonacci(key);
return __cache_DemoRSCG_UtilsMemo_fibTest_fibonacci_MemoPure.GetOrAdd(key,data);
}
public async Task<long> __wrap_fibonacci (Tuple<long > args){
return await fibonacci_MemoPure (args.Item1);
}

}

Useful​

Download Example (.NET C#)​

Share RSCG_Utils_Memo​

https://ignatandrei.github.io/RSCG_Examples/v2/docs/RSCG_Utils_Memo

aaa

Category "FunctionalProgramming" has the following generators:​

1 cachesourcegenerator Nuget GitHub Repo stars 2024-02-14

2 dunet Nuget GitHub Repo stars 2023-04-16

3 Dusharp Nuget GitHub Repo stars 2024-09-19

4 Funcky.DiscriminatedUnion Nuget GitHub Repo stars 2024-01-18

5 FunicularSwitch NugetNuget GitHub Repo stars 2024-02-12

6 N.SourceGenerators.UnionTypes Nuget GitHub Repo stars 2023-10-29

7 OneOf NugetNuget GitHub Repo stars 2023-08-21

8 PartiallyApplied Nuget GitHub Repo stars 2023-04-16

9 polytype Nuget GitHub Repo stars 2024-11-04

10 rscg_demeter Nuget GitHub Repo stars 2025-03-26

11 rscg_queryables NugetNuget GitHub Repo stars 2024-11-02

12 RSCG_Utils_Memo Nuget GitHub Repo stars 2023-08-27

13 ScottEncodingGenerator Nuget GitHub Repo stars 2026-06-30

14 Sera.Union Nuget GitHub Repo stars 2024-08-26

15 Sundew.DiscriminatedUnions Nuget GitHub Repo stars 2026-02-14

16 TypeUtilities Nuget GitHub Repo stars 2024-03-05

17 UnionGen Nuget GitHub Repo stars 2024-04-05

18 UnionsGenerator Nuget GitHub Repo stars 2024-02-18

See category​

FunctionalProgramming