Last active
February 23, 2017 12:05
-
-
Save dadhi/8bc48756639b0cfaa6753cae0dadb099 to your computer and use it in GitHub Desktop.
Sample of Clojure Transducers in C# (LINQPad snippet)
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
Func<Func<T,T,T>, Func<T,T,T>> Mapping<T>(Func<T,T> map) | |
{ | |
return reduce => (sum, item) => reduce(sum, map(item)); | |
} | |
Func<Func<T,T,T>, Func<T,T,T>> Filtering<T>(Func<T,bool> condition) | |
{ | |
return reduce => (sum, item) => condition(item) ? reduce(sum, item) : sum; | |
} | |
void Main() | |
{ | |
List<int> ns = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
Func<int,int> half = n => n / 2; | |
Func<int,bool> isEven = n => n % 2 == 0; | |
Func<int,int,int> Plus = (a, b) => a + b; | |
ns.Where(isEven).Select(half).Aggregate(0, Plus).Dump("LINQ"); | |
ns.Aggregate(0,Filtering(isEven)(Mapping(half)(Plus))).Dump("Clojure Reducer"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment