Created
January 9, 2018 06:29
-
-
Save 20chan/786bc06ac51d616c13f6eda78f023ef8 to your computer and use it in GitHub Desktop.
Stream based programming DSL in 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.Linq; | |
using System.IO; | |
namespace Streem | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
object _; | |
object Reverse(object str) | |
{ | |
return string.Join("", str.ToString().Reverse()); | |
} | |
_ = std.Out < "Print This!!!"; | |
// ReadLine | |
_ = std.In > _; | |
// Read and Print that | |
_ = std.In > std.Out; | |
// Print Upper string | |
_ = std.In > ((str) => str.ToString().ToUpper()) > std.Out; | |
// Print reversed string | |
_ = std.Out < (new Func<object, object>(Reverse) < std.In); | |
Console.Read(); | |
} | |
} | |
class std | |
{ | |
public static Streem In = new ReadingStreem(Console.In); | |
public static Streem Out = new WritingStreem(Console.Out); | |
} | |
abstract class Streem | |
{ | |
public abstract object Read(); | |
public abstract void Write(object data); | |
public static Streem operator >(Streem from, Streem to) | |
{ | |
to.Write(from.Read()); | |
return from; | |
} | |
public static Streem operator <(Streem to, Streem from) | |
{ | |
return from < to; | |
} | |
public static object operator >(Streem from, Func<object, object> function) | |
{ | |
return function(from.Read()); | |
} | |
public static object operator <(Streem to, Func<object, object> function) | |
{ | |
return function > to; | |
} | |
public static object operator <(Func<object, object> function, Streem from) | |
{ | |
return from > function; | |
} | |
public static object operator >(Func<object, object> function, Streem to) | |
{ | |
return (function as object) > to; | |
} | |
public static Streem operator >(object data, Streem to) | |
{ | |
to.Write(data); | |
return to; | |
} | |
public static Streem operator <(object data, Streem to) | |
{ | |
throw new Exception(); | |
} | |
public static Streem operator <(Streem to, object data) | |
{ | |
to.Write(data); | |
return to; | |
} | |
public static Streem operator >(Streem to, object data) | |
{ | |
to.Read(); | |
return to; | |
} | |
} | |
class ReadingStreem : Streem | |
{ | |
TextReader reader; | |
public ReadingStreem(TextReader r) | |
{ | |
reader = r; | |
} | |
public override object Read() | |
{ | |
return reader.ReadLine(); | |
} | |
public override void Write(object data) | |
{ | |
throw new Exception(); | |
} | |
} | |
class WritingStreem : Streem | |
{ | |
TextWriter writer; | |
public WritingStreem(TextWriter w) | |
{ | |
writer = w; | |
} | |
public override object Read() | |
{ | |
throw new Exception(); | |
} | |
public override void Write(object data) | |
{ | |
writer.WriteLine(data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment