Skip to content

Instantly share code, notes, and snippets.

@hodzanassredin
Created October 23, 2012 15:36
Show Gist options
  • Save hodzanassredin/3939517 to your computer and use it in GitHub Desktop.
Save hodzanassredin/3939517 to your computer and use it in GitHub Desktop.
ExpressionThread from clojure to c#
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