public void TestTransducers() { var filter = new FilterTransducer<int>(x => x % 3 == 0); var square = new MapTransducer<int,int>(x => x * x); var addone = new MapTransducer<int,int>(x => x + 1); var toString = new MapTransducer<int,string>(Convert.ToString); var reducer = filter.Transduce( square.Transduce( addone.Transduce( toString.Transduce( new ListReducer<string>())))); var input = this.GetInput(); var output = new List<string>(); foreach (var x in input) { output = reducer.Apply(output, x); } this.AssertOutput(output); }
public void TestTransducerDocumentationA() { Func<List<string>, string, List<string>> appender = (result, value) => { result.Add(value); return result; }; var filter = new FilterTransducer<int>(x => x % 3 == 0); var square = new MapTransducer<int,int>(x => x * x); var addOne = new MapTransducer<int,int>(x => x + 1); var toString = new MapTransducer<int,string>(Convert.ToString); var xform = Composer .Compose(filter) .Compose(square) .Compose(addOne) .Compose(toString) .Compose(); var input = Enumerable.Range(0,10); var output = Transducer.Transduce(xform, appender, new List<string>(), input); this.AssertOutput(output); }