-
Hello! I am C# noob, Haskell intermediate, relative to the cool concepts in this library, ie. no experience with Effect Systems in Haskell. I know that in Haskell I have said many times, I want to apply an (a -> m b) action on a (Map k a) and I've done it like this: forM_ (Map.toList byUser) $ \(user, value :: Msg)) -> do
f value
where
f :: a -> m b I typically use this as a crappy one size fits all solution to a number of problems I come across and perhaps it is not even the way to go? I can't seem to find a ForM equivalent. Should I go with an fmap then sequence? Or am I just way off? Perhaps my difficulty stems from a lack of understanding the relationship between 'Task' and 'Eff'. The C# code in question that is needing to change is: private static async Task<Map<(string, string, string), Lst<AttachmentContents>>> ScanNewMessagesForAttachments(string accessToken, Lst<string> mostRecent50)
{
Lst<MailMessage> unread = await GetUnread(mostRecent50, accessToken, countBy_config);
Lst<MailMessage> unreadsWithAttachment = unread.Filter(u => u.HasAttachments);
return await GetUnreadAttachments(accessToken, unreadsWithAttachment);
}
In my attempt to be functional in C# I used Filter (again: C# noob) and realized that I'm not returning the information about emails with no attachments and thus I never track elsewhere that emails with no attachment have been read. I can see a band-aid solution to do another filter |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
map.ToSeq(); // Lazy memoising sequence
map.ToIterable(); // Lazy non-memoising sequence
map.ToLst(); // Non-lazy sequence With regards to 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));
} Or, create an extension method: public static class FoldableMonadExtensions
{
public static K<M, Unit> ForM<T, M, A, B>(this K<T, A> ta, Func<A, K<M, B>> f)
where M : Monad<M>
where T : Foldable<T> =>
YourPrelude.forM(ta, f);
}
In terms of
Just to be clear Also the whole
First, I'd advise using If you're just learning C# and language-ext, I'd suggest taking a look at some of the samples:
One major difference in how you write functional-C# compared to Haskell is that you'll lean on LINQ in C# much more than you lean on LINQ is a bit more forgiving than the regular function invocation in C#, so it's easier to write out LINQ expressions (when leveraging monadic effects) than it is to write overly terse operator leveraging expressions. |
Beta Was this translation helpful? Give feedback.
(a -> m b)
is obviously bind (monad bind) and as you demonstrate, by callingtoList
,Map k a
isn't a monad. And neither is it a monad in language-ext either. In language-ext, if you useMap<K, A>
orHashMap<K, A>
then you can call:With regards to
mapM_
andforM_
that sequence foldable monadic actions. You can use this: