Created
October 23, 2012 15:36
-
-
Save hodzanassredin/3939517 to your computer and use it in GitHub Desktop.
ExpressionThread from clojure to 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.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| public static class ThreadExpression | |
| { | |
| public static T Thread<T>(this T obj, params Action<T>[] argsRest) | |
| { | |
| foreach (var item in argsRest) | |
| { | |
| item(obj); | |
| } | |
| return obj; | |
| } | |
| public static T Thread<T>(this T obj, params Func<T,T>[] argsRest) | |
| { | |
| foreach (var item in argsRest) | |
| { | |
| obj = item(obj); | |
| } | |
| return obj; | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var s = new StringBuilder("").Thread( | |
| x => x.Append("1"), | |
| x => x.Append(",2"), | |
| x => x.Append(",3") | |
| ); | |
| var ReplaceAllCommasAddComma = new Action<StringBuilder>[] { | |
| x => x.Replace(',', ' '), | |
| x => x.Append(',') | |
| }; | |
| var AddCommaAndReplaceAllCommas = ReplaceAllCommasAddComma.Reverse().ToArray();//reverse is like a macro | |
| var s2 = s.Thread(AddCommaAndReplaceAllCommas); | |
| Console.WriteLine(s); | |
| Console.ReadKey(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment