Skip to content

Instantly share code, notes, and snippets.

@ZacharyPatten
Last active March 9, 2020 12:25
Show Gist options
  • Save ZacharyPatten/c6e46acd39d3e1d1d19e8da9a482d303 to your computer and use it in GitHub Desktop.
Save ZacharyPatten/c6e46acd39d3e1d1d19e8da9a482d303 to your computer and use it in GitHub Desktop.
An example of custom For syntax... Pretty dumb... Just messing around...
using System;
namespace Example
{
class Program
{
static void Main()
{
For (10..5)
(
Console.WriteLine
);
For (5..10, Console.WriteLine);
}
public static Action<Action<int>> For(Range range) =>
x =>
{
int a = range.Start.Value;
int b = range.End.Value;
if (a > b) for (int i = a; i >= b; i--) x(i);
else for (int i = a; i <= b; i++) x(i);
};
public static void For(Range range, Action<int> action)
{
int a = range.Start.Value;
int b = range.End.Value;
if (a > b) for (int i = a; i >= b; i--) action(i);
else for (int i = a; i <= b; i++) action(i);
}
}
}
using System;
using static FOR;
public class Program
{
public static int Main() =>
For (1..4) > Console.WriteLine;
}
public struct FOR
{
public static FOR For(Range range) => new FOR(range);
public Range Value;
public FOR(Range range) => Value = range;
public static int operator >(FOR @for, Action<int> action)
{
for (int i = @for.Value.Start.Value; i < @for.Value.End.Value; i++)
action(i);
return default;
}
public static int operator <(FOR @for, Action<int> action) =>
throw new NotImplementedException();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment