Skip to content

Translating from Haskell to C# : IO action on Map values #1482

Answered by louthy
augyg asked this question in Q&A
Discussion options

You must be logged in to vote

(a -> m b) is obviously bind (monad bind) and as you demonstrate, by calling toList, Map k a isn't a monad. And neither is it a monad in language-ext either. In language-ext, if you use Map<K, A> or HashMap<K, A> then you can call:

  map.ToSeq();      // Lazy memoising sequence
  map.ToIterable(); // Lazy non-memoising sequence
  map.ToLst();      // Non-lazy sequence

With regards to mapM_ and forM_ that sequence foldable monadic actions. You can use this:

public static class YourPrelude
{
    public static K<M, Unit> forM<T, M, A, B>(K<T, A> ta, Func<A, K<M, B>> f)
        where M : Monad<M>
        where T : Foldable<T> =>
        ta.Fold(pure<M, Unit>(unit), x => k => f(x).Bind(_ => k));

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@augyg
Comment options

@louthy
Comment options

Answer selected by augyg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants