Skip to content

Instantly share code, notes, and snippets.

View dimitris-papadimitriou-chr's full-sized avatar
📖
Cloud Design Patterns

Dimitris Papadimitriou dimitris-papadimitriou-chr

📖
Cloud Design Patterns
View GitHub Profile
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace FunctionalExperimentation.SimpleRefactorings.Medium1.ExtensionsDecorator
{
public record struct Item(int Id, string Name);
public delegate Task<R> Operation<T, R>(T t);
public static partial class FuntionalExtensions
{
public static Operation<T, R> AddDebugLogging<T, R>(this Operation<T, R> @this) =>
(T t) =>
{
var result = @this(t);
Debug.WriteLine(result);
return result;
public interface IOperation<T, R>
{
Task<R> OperationAsync(T t);
}
public record class OperationManager<T, R>(IOperation<T, R> operation)
{
public R ExecuteActivity(T t)
{
//do some stuff1
//...
return operation.OperationAsync(t).Result;
}
}
using System;
using System.Threading.Tasks;
namespace FunctionalExperimentation.FuncTaskCovarinat.Minimal.DelegateAdapter
{
public static partial class F
{
public static FuncTask<T> ToFuncTask<T>(this Func<Task<T>> @this) =>
new FuncTask<T>(
()=>@this().GetAwaiter().GetResult()
using System;
using System.Threading.Tasks;
namespace FunctionalExperimentation.SimpleRefactorings.Medium.FuncTaskCovarinat.Minimal
{
public static partial class F
{
public static FuncTask<T> ToFuncTask<T>(this Func<Task<T>> @this) =>
new FuncTask<T>(
() => @this().GetAwaiter().GetResult()
@dimitris-papadimitriou-chr
dimitris-papadimitriou-chr / .cs
Last active June 25, 2022 17:58
C# Covarinace and what has to do with Functional C#
using System;
using System.Threading.Tasks;
namespace FunctionalExperimentation.Practical.FuncTaskCovarinat
{
public static partial class F
{
public static IFuncTask<T> ToWithResult<T>(this Func<Task<T>> @this) => new FuncTaskAdapter<T>(@this);
public static T GetOrThrowIfNull<T>(this IFuncTask<T> @this, string message)
{
Func<(int OrderNum, string ProductName),(int orderPlacementId, int orderStatus)>
printOrderDetails = command => { ... };
public class Printer
{
public (int orderPlacementId, int orderStatus) PrintOrderDetails(
(int OrderNum, string ProductName) command)
{
...
}
}
public class Order
{
IList<Product> Products { get; }
public Order(IList<Product> products) => Products = products;
public double GetTotalPrice(IDiscountStrategy activeDiscount) =>
Products
.Select(activeDiscount.GetDiscountedPrice)
.Sum();