Created
November 24, 2021 16:04
-
-
Save momvart/2f052ecdb2291b4a231cc295d7b9d0f0 to your computer and use it in GitHub Desktop.
A set of extensions functions written on object, by default available in Kotlin, ported for c#.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.CompilerServices; | |
using System.Threading.Tasks; | |
namespace Utilities | |
{ | |
public static class KotlinObjectExtensions | |
{ | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static T Also<T>(this T callee, Action<T> action) | |
{ | |
action(callee); | |
return callee; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static async Task<T> Also<T>(this T callee, Func<T, Task> action) | |
{ | |
await action(callee); | |
return callee; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static TResult Let<T, TResult>(this T callee, Func<T, TResult> func) => | |
func(callee); | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static async Task<TResult> Let<T, TResult>(this T callee, Func<T, Task<TResult>> func) => | |
await func(callee); | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static void Let<T>(this T callee, Action<T> func) => | |
func(callee); | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static async Task Let<T>(this T callee, Func<T, Task> func) => | |
await func(callee); | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static T? TakeIf<T>(this T callee, Func<T, bool> predicate) => | |
predicate(callee) ? callee : default; | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static async Task<T?> TakeIf<T>(this T callee, Func<T, Task<bool>> predicate) => | |
await predicate(callee) ? callee : default; | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
public static T? TakeIf<T>(this T callee, bool condition) => | |
condition ? callee : default; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment