Skip to content

Instantly share code, notes, and snippets.

@ZacharyPatten
Last active March 9, 2020 12:25
Show Gist options
  • Select an option

  • Save ZacharyPatten/c6e46acd39d3e1d1d19e8da9a482d303 to your computer and use it in GitHub Desktop.

Select an option

Save ZacharyPatten/c6e46acd39d3e1d1d19e8da9a482d303 to your computer and use it in GitHub Desktop.

Revisions

  1. ZacharyPatten revised this gist Feb 5, 2020. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion ForSyntax2.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    For when you are too lazy to type out an entire for loop.
    using System;
    using static FOR;
    public class Program
  2. ZacharyPatten revised this gist Feb 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ForSyntax2.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    For when you are too lazy to type out an entire for loop...
    For when you are too lazy to type out an entire for loop.
    using System;
    using static FOR;
    public class Program
  3. ZacharyPatten revised this gist Feb 5, 2020. No changes.
  4. ZacharyPatten revised this gist Feb 5, 2020. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions ForSyntax2.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    For when you are too lazy to type out an entire for loop...
    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();
    }
  5. ZacharyPatten revised this gist Nov 27, 2019. 1 changed file with 29 additions and 18 deletions.
    47 changes: 29 additions & 18 deletions ForSyntax.cs
    Original file line number Diff line number Diff line change
    @@ -2,22 +2,33 @@

    namespace Example
    {
    class Program
    {
    static void Main()
    {
    For (10, To: 5)
    (
    Console.WriteLine
    );
    }

    public static Action<Action<int>> For(int from, int? To = null) =>
    x =>
    {
    if (!To.HasValue) throw new ArgumentNullException(nameof(To));
    else if (from > To) for (int i = from; i >= To; i--) x(i);
    else for (int i = from; i <= To; i++) x(i);
    };
    }
    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);
    }
    }
    }
  6. ZacharyPatten revised this gist Nov 27, 2019. No changes.
  7. ZacharyPatten revised this gist Nov 27, 2019. 1 changed file with 16 additions and 13 deletions.
    29 changes: 16 additions & 13 deletions ForSyntax.cs
    Original file line number Diff line number Diff line change
    @@ -2,19 +2,22 @@

    namespace Example
    {
    static void Main()
    class Program
    {
    For (10, To: 5)
    (
    Console.WriteLine
    );
    }

    public static Action<Action<int>> For(int from, int? To = null) =>
    x =>
    static void Main()
    {
    if (!To.HasValue) throw new ArgumentNullException(nameof(To));
    else if (from > To) for (int i = from; i >= To; i--) x(i);
    else for (int i = from; i <= To; i++) x(i);
    };
    For (10, To: 5)
    (
    Console.WriteLine
    );
    }

    public static Action<Action<int>> For(int from, int? To = null) =>
    x =>
    {
    if (!To.HasValue) throw new ArgumentNullException(nameof(To));
    else if (from > To) for (int i = from; i >= To; i--) x(i);
    else for (int i = from; i <= To; i++) x(i);
    };
    }
    }
  8. ZacharyPatten revised this gist Nov 27, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ForSyntax.cs
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ public static Action<Action<int>> For(int from, int? To = null) =>
    x =>
    {
    if (!To.HasValue) throw new ArgumentNullException(nameof(To));
    else if (from > to) for (int i = from; i >= to; i--) x(i);
    else for (int i = from; i <= to; i++) x(i);
    else if (from > To) for (int i = from; i >= To; i--) x(i);
    else for (int i = from; i <= To; i++) x(i);
    };
    }
  9. ZacharyPatten created this gist Nov 27, 2019.
    20 changes: 20 additions & 0 deletions ForSyntax.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    using System;

    namespace Example
    {
    static void Main()
    {
    For (10, To: 5)
    (
    Console.WriteLine
    );
    }

    public static Action<Action<int>> For(int from, int? To = null) =>
    x =>
    {
    if (!To.HasValue) throw new ArgumentNullException(nameof(To));
    else if (from > to) for (int i = from; i >= to; i--) x(i);
    else for (int i = from; i <= to; i++) x(i);
    };
    }