Skip to content

Instantly share code, notes, and snippets.

@dimitris-papadimitriou-chr
Created August 28, 2022 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitris-papadimitriou-chr/05015c87770dcb0b69857e3d57a63504 to your computer and use it in GitHub Desktop.
Save dimitris-papadimitriou-chr/05015c87770dcb0b69857e3d57a63504 to your computer and use it in GitHub Desktop.
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 static partial class FuntionalExtensions
{
public static Func<Item, bool> AsName(this string @name) =>
item => item.Name == @name;
public static Func<Item, bool> AsId(this int @id) =>
item => item.Id == @id;
public static Func<T, bool> And<T>(this Func<T, bool> @this, Func<T, bool> filter1) =>
item => @this(item) && filter1(item);
public static Func<Func<T, bool>, Func<T, bool>> And<T>(this Func<T, bool> @this) =>
filter1 => item => @this(item) && filter1(item);
public static Func<Item, bool> AsId(this (Func<Item, bool>, int) @this) =>
@this.Item1.And(@this.Item2.AsId());
public static (Func<Item, bool>, int) And(this Func<Item, bool> @this, int x) =>
(@this, x);
}
public class Example
{
public static async Task RunAsync()
{
List<Item> items = new List<Item> { new Item(1, "a"), new Item(2, "b") };
//IOperationManager<int, string> operationManager = new OperationManager<int, string>().AddLogger();
var filtered0 = items.Where("a".AsName());
var filtered1 = items.Where("a".AsName().And(1.AsId()));
var filtered2 = items.Where("a".AsName().And()(1.AsId()).And()(_ => true));
var filtered3 = items.Where("a".AsName().And(1).AsId());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment