Skip to content

Instantly share code, notes, and snippets.

@azunyuuuuuuu
Last active October 30, 2024 05:08
Show Gist options
  • Select an option

  • Save azunyuuuuuuu/235a556b434acb4dc571cdb2b7ca612f to your computer and use it in GitHub Desktop.

Select an option

Save azunyuuuuuuu/235a556b434acb4dc571cdb2b7ca612f to your computer and use it in GitHub Desktop.
personal c# snippet collection
public sealed class DisposableAction(Action action) : IDisposable
{
private readonly Action _action
= action ?? throw new ArgumentNullException(nameof(action));
public void Dispose()
=> _action();
}
// source: https://stackoverflow.com/a/39997157
public static class ForeachWithIndex
{
// use .NET 9's .Index()
public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source)
=> source.Select((item, index) => (item, index));
public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> source)
=> source?.Select((item, index) => (item, index)) ?? Enumerable.Empty<(T, int)>();
}
using System.Numerics;
namespace dummycodes;
// source: https://youtu.be/NzjF1pdlK7Y?t=1745
public static class Maths
{
// replace the Lerp with <type>.Lerp
public static T InvLerp<T>(T a, T b, T value)
where T : IFloatingPointIeee754<T>
=> (value - a) / (b - a);
public static T Remap<T>(T inputmin, T inputmax, T outputmin, T outputmax, T value)
where T : IFloatingPointIeee754<T>
=> T.Lerp(outputmin, outputmax, InvLerp(inputmin, inputmax, value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment