Skip to content

Instantly share code, notes, and snippets.

@rarous
Forked from renestein/gist:988359
Created May 25, 2011 11:43
Show Gist options
  • Save rarous/990824 to your computer and use it in GitHub Desktop.
Save rarous/990824 to your computer and use it in GitHub Desktop.
BuildergenerovaniKolekceObjektu.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Builder_Algida_Row_Generator
{
class GeneratorExtensions
{
public LinkedList<TReturn> ApplyFunctionsWithHistory<TA1, TReturn>(TReturn arg1, TA1 arg2,
params Func<TReturn, TA1, TReturn>[] innerFunctions)
{
var linkedList = new LinkedList<TReturn>();
linkedList.AddFirst(arg1);
if (innerFunctions == null)
{
return linkedList;
}
innerFunctions.Aggregate(new {List = linkedList, LastValue = linkedList.Last.Value},
(currentValues, func) =>
{
var lastValue = currentValues.LastValue;
var newValue = func(lastValue, arg2);
if (!newValue.Equals(lastValue))
{
currentValues.List.AddLast(newValue);
}
return new {List = currentValues.List, LastValue = newValue};
},
lastValues => lastValues.LastValue
);
return linkedList;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Builder_Algida_Row_Generator
{
class Program
{
static void Main(string[] args)
{
const int TESTROWS = 200;
var rowGenerator = new RowGenerator();
//Zachycení proměnných pro generování řádku v lambdách
decimal x = 10;
decimal y = 20;
rowGenerator.Generate(TESTROWS, (row, index) => row.ChangeA(x / y),
(row, index) => row.ChangeB(x * y / (index + 1)),
(row, index) => row.ChangeC(index + 1),
new SpecialGeneratorFunctions().ChangeDValue);
Enumerable.Range(0, TESTROWS).Select(index => rowGenerator.GetRow(index))
.ToList().ForEach(row => Console.WriteLine(row.C));
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Builder_Algida_Row_Generator
{
class Row
{
public Row(int value1, decimal a, decimal b, decimal c, decimal d, decimal e, decimal f)
{
Value1 = value1;
A = a;
B = b;
C = c;
D = d;
E = e;
F = f;
}
public Row() : this(0, 0m, 0m, 0m, 0m, 0m, 0m)
{
}
public int Value1
{
get;
private set;
}
public decimal A
{
get;
private set;
}
public decimal B
{
get;
private set;
}
public decimal C
{
get;
private set;
}
public decimal D
{
get;
private set;
}
public decimal E
{
get;
private set;
}
public decimal F
{
get;
private set;
}
public Row ChangeValue1(int value1)
{
return new Row(value1, A, B, C, D, E, F);
}
public Row ChangeA(decimal a)
{
return new Row(Value1, a, B, C, D, E, F);
}
public Row ChangeB(decimal b)
{
return new Row(Value1, A, b, C, D, E, F);
}
public Row ChangeC(decimal c)
{
return new Row(Value1, A, B, c, D, E, F);
}
public Row ChangeD(decimal d)
{
return new Row(Value1, A, B, C, d, E, F);
}
public Row ChangeE(decimal e)
{
return new Row(Value1, A, B, C, D, e, F);
}
public Row ChangeF(decimal f)
{
return new Row(Value1, A, B, C, D, E, f);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Builder_Algida_Row_Generator
{
class RowGenerator
{
public RowGenerator()
{
m_rows = new List<Row>();
}
private List<Row> m_rows;
public Row GetRow(int index)
{
return m_rows[index];
}
public void Generate (int numberOfRows, params Func<Row, int, Row>[] rowGenerators)
{
var innerRowGenerator = rowGenerators ?? new Func<Row, int, Row>[] { (row, index) => new Row() };
var generatorExtensions = new GeneratorExtensions(); //I tato třída může být skryta za rozhraním a injektována
var rowsWithHistory = Enumerable.Range(0, numberOfRows)
.Select(index => new {Row = new Row(), Index = index})
.Aggregate(new List<LinkedList<Row>>(),
(result, rowData) =>
{
var rowHistory = generatorExtensions.ApplyFunctionsWithHistory(rowData.Row, rowData.Index, innerRowGenerator);
result.Add(rowHistory);
return result;
}
);
//V rowsWithHistory mám historii změn na objektu, pokud ji nepotřebuju, vyzvednu poslední objekt v každém listu
m_rows = rowsWithHistory.Select(linkedList => linkedList.Last.Value).ToList();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Builder_Algida_Row_Generator
{
class SpecialGeneratorFunctions
{
public SpecialGeneratorFunctions()
{
}
public Row ChangeDValue(Row row, int index)
{
if (row == null)
{
throw new ArgumentNullException("row");
}
var returnRow = row;
if (row.B > row.A)
{
returnRow = row.ChangeD(row.B - row.A);
}
return returnRow;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment