-
Hello, First of all, I want to say that LanguageExt is a fantastic library I'm trying to adapt my code to use LanguageExt's functional programming constructs, but I'm not sure how to replace my foreach loop with a more functional approach using LanguageExt. My code involves asynchronous operations that may add data to a database. Here's my current code: foreach (var member in group.GetMembers(true))
{
await _packLicenseRepository.GetCollaborateurAsync(member.Guid.Value, cancellationToken)
.BindAsync(collaborateur => Person.CreatePerson(collaborateur.CollaborateurPk))
.BindAsync(person => serviceStore.AddPerson(pack.PackName, person));
} I would like to refactor this code to eliminate the foreach loop and use LanguageExt's functional methods instead, considering that I'm adding entries to a database. Additionally, I want the processing to continue even if some operations fail, and I wish to collect a list of any errors that occur during the process. Could someone please guide me on how to achieve this? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You will need to do a bit of reading on Traversables; but also you should understand applicatives, foldables, and functors. Traversables can work with foldable-sequences of effects and leverage their applicative behaviours to run operations concurrently, but also collect multiple errors. You're not going to get that functionality from the |
Beta Was this translation helpful? Give feedback.
-
Thx for your response, There's a lot to read also for the Eff part. Do you have a code example with async calls to a database? |
Beta Was this translation helpful? Give feedback.
First off, make sure you have the latest beta, it needed some changes to support this for
IO
-based operations.No, don't use
Map
, useTraverse
:I note you've changed the requirements to "retrieve both errors and successes."; the above will only return either…