Skip to content

Instantly share code, notes, and snippets.

@auycro
Last active February 3, 2021 14:45
Show Gist options
  • Save auycro/38c416fa624eb64cdf4994b9dbf00d90 to your computer and use it in GitHub Desktop.
Save auycro/38c416fa624eb64cdf4994b9dbf00d90 to your computer and use it in GitHub Desktop.
Create Readonly Dict from Langext map
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<RootNamespace>lang_ext</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LanguageExt.Core" Version="3.4.15" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Linq;
using LanguageExt;
using static LanguageExt.Prelude;
namespace lang_ext
{
class Program
{
static void Main(string[] args)
{
var demo = new Demo<int>(new List<int>(){100,200});
var dict2 = demo.toDict3();
string text2 = dict2.Select(kvp => kvp.ToString()).Aggregate((a, b) => a + ", " + b);
Console.WriteLine(text2);
}
}
public class Demo<T> {
private T token;
private T lifetime;
public Demo(List<T> param){
token = param[0];
lifetime = param[1];
}
public IReadOnlyDictionary<string,T> toDict3() => Map(
(nameof(token),token),
(nameof(lifetime),lifetime)).ToDictionary();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment