# How can i do this in a functional way using F#

**URL:** <https://forums.fsharp.org/t/how-can-i-do-this-in-a-functional-way-using-f/1092>\
**Category:** General\
**Tags:** beginners\
**Created:** [November 21, 2019, 12:23pm UTC](https://forums.fsharp.org/t/how-can-i-do-this-in-a-functional-way-using-f/1092 "2019-11-21T12:23:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![renato04](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@renato04](https://forums.fsharp.org/u/renato04)\
**Post date:** [November 21, 2019, 12:23pm UTC](https://forums.fsharp.org/t/how-can-i-do-this-in-a-functional-way-using-f/1092/1 "2019-11-21T12:23:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![voronoipotato](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/voronoipotato/32/401_2.png) [@voronoipotato](https://forums.fsharp.org/u/voronoipotato)\
**Post date:** [November 21, 2019, 2:26pm UTC](https://forums.fsharp.org/t/how-can-i-do-this-in-a-functional-way-using-f/1092/2 "2019-11-21T14:26:42Z")

</div>

```
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…

---

<div class="post-metadata">

**Author:** ![renato04](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@renato04](https://forums.fsharp.org/u/renato04)\
**Post date:** [November 21, 2019, 7:45pm UTC](https://forums.fsharp.org/t/how-can-i-do-this-in-a-functional-way-using-f/1092/3 "2019-11-21T19:45:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![voronoipotato](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/voronoipotato/32/401_2.png) [@voronoipotato](https://forums.fsharp.org/u/voronoipotato)\
**Post date:** [November 21, 2019, 10:42pm UTC](https://forums.fsharp.org/t/how-can-i-do-this-in-a-functional-way-using-f/1092/4 "2019-11-21T22:42:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![renato04](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@renato04](https://forums.fsharp.org/u/renato04)\
**Post date:** [November 22, 2019, 7:08pm UTC](https://forums.fsharp.org/t/how-can-i-do-this-in-a-functional-way-using-f/1092/5 "2019-11-22T19:08:22Z")

</div>

I think yes, it Helps.  
I have to study more about it.

Thank you!
