Hello!
I’m a C# developer and I have following algorithm
var processA = List<A>()
var processB = List<B>()
var processC = List<C>()
var itens = loadFromDataBase();
foreach(var item in itens)
{
if(item.SomoeEnum == SomoeEnum.StatusA)
{
//after some code
processA.Add(new A);
}
else if(item.SomoeEnum == SomoeEnum.StatusB)
{
//after some code
processB.Add(new B);
}
else if(item.SomoeEnum == SomoeEnum.StatusC)
{
//after some code
processC.Add(new C);
}
}
foreach(var item in processA)
{
ProcessA(item);
}
foreach(var item in processB)
{
ProcessB(item);
}
foreach(var item in processC)
{
ProcessC(item);
}
Which is the best way to do it using functional programming?
Thanks in advance
type Item = A of int | B of float | C of string
let items : Item list = [A 6; B 3.2; C "woah"] //loadFromDatabase() might go here.
let process item =
match item with
| A i -> A (i * 3)
| B f -> B (f + 3.2)
| C s -> C (s + " complete")
let processedItems = items |> List.map process
It might look something like this…
1 Like
Thank you for responding to me
But how the methods do add items to lists and that change the list state, I think I can’t do this.
And after that It must iterate each list to process some another function.
Instead of changing the list state you make a new list. The code I wrote does the same thing that yours does. substitute (i*3) with a function for ProcessA, (f + 3.2) with a function for ProcessB, (s + " complete") with a function for ProcessC. Does that help?
I think yes, it Helps.
I have to study more about it.
Thank you!