This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Computations with extensible environment, error handling, and asynchronicity | |
| // I recently reviewed some F# code that turned out to be using | |
| // | |
| // Dependency Interpretation | |
| // https://fsharpforfunandprofit.com/posts/dependencies-4/ | |
| // | |
| // and got thinking about whether one could construct a usable Zio like monad | |
| // | |
| // https://zio.dev/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Threading.Tasks; | |
| namespace System.Collections.Concurrent | |
| { | |
| public static class ConcurrentDictionaryExtensions | |
| { | |
| /// <summary> | |
| /// Provides an alternative to <see cref="ConcurrentDictionary{TKey, TValue}.GetOrAdd(TKey, Func{TKey, TValue})"/> that disposes values that implement <see cref="IDisposable"/>. | |
| /// </summary> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* | |
| WHAT'S GOING ON HERE?! | |
| Sometimes you don't care about a particular type, you're interested in one field only, let's say `EntityId`. | |
| Instead of using interface (which isn't even possible if don't own a type), | |
| we can do structural typing in F# using SRTP and Active Patterns. | |
| Active patterns are not required for this, but they do make code much easier to use. | |
| *) | |
| // So we have 2 types with field `EntityId: string`: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let cache (a:Async<'a>) : Async<'a> = | |
| let tcs = TaskCompletionSource<'a>() | |
| let state = ref 0 | |
| async { | |
| if (Interlocked.CompareExchange(state, 1, 0) = 0) then | |
| Async.StartWithContinuations( | |
| a, | |
| tcs.SetResult, | |
| tcs.SetException, | |
| (fun _ -> tcs.SetCanceled())) |