junkcode
Check-in [1b76182634]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:An alternate implementation of the typeclass approach.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:1b7618263400ce5e9976557b076c70a0eaa1a92631f4056a81ee69ae6e2f1fe8
User & Date: jaccarmac 2018-01-29 05:41:15
Context
2018-01-29
05:45
Switching from Identity to Option solves the Bind type inference issue. check-in: ff0e1f8539 user: jaccarmac tags: trunk
05:41
An alternate implementation of the typeclass approach. check-in: 1b76182634 user: jaccarmac tags: trunk
04:07
Fix line endings. check-in: 976bae5ae0 user: jaccarmac tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to free-monad-vs-typeclass/Program.cs.

1

2
3
4
5
6
7
8
..
12
13
14
15
16
17
18
19

20


21
22
using LanguageExt;

using System;

using static LanguageExt.Prelude;

namespace free_monad_vs_typeclass
{
    public class Program
................................................................................
            var freeProgram = new ResourcePrinterFree<Unit>.Print("Hello, world!", () =>
                new ResourcePrinterFree<Unit>.Return(unit));
            freeProgram.Map(FreeInterpreter.Interpret);

            TypeclassProgram<ResourcePrinterTypeclassUnit, Unit>();
        }

        public static Unit TypeclassProgram<ResourcePrinterTypeclassT, T>() where ResourcePrinterTypeclassT: struct, ResourcePrinterTypeclass<T> =>

            use(default(ResourcePrinterTypeclassT).AcquireResource, r => default(ResourcePrinterTypeclassT).Print(r, "Hello, world!"));


    }
}

>







 







|
>
|
>
>


1
2
3
4
5
6
7
8
9
..
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using LanguageExt;
using LanguageExt.ClassInstances;
using System;

using static LanguageExt.Prelude;

namespace free_monad_vs_typeclass
{
    public class Program
................................................................................
            var freeProgram = new ResourcePrinterFree<Unit>.Print("Hello, world!", () =>
                new ResourcePrinterFree<Unit>.Return(unit));
            freeProgram.Map(FreeInterpreter.Interpret);

            TypeclassProgram<ResourcePrinterTypeclassUnit, Unit>();
        }

        public static Identity<Unit> TypeclassProgram<ResourcePrinterTypeclassT, T>() where ResourcePrinterTypeclassT: struct, ResourcePrinterTypeclass<Identity<T>, T> =>
            default(MIdentity<ResourceWrapper>).Bind<MIdentity<Unit>, Identity<Unit>, Unit>(
                default(ResourcePrinterTypeclassT).AcquireResource<MIdentity<ResourceWrapper>, Identity<ResourceWrapper>>(),
                r => use(r, _ => default(ResourcePrinterTypeclassT).Print<MIdentity<Unit>, Identity<Unit>>(r, "Hello, world!"))
            );
    }
}

Changes to free-monad-vs-typeclass/ResourcePrinterTypeclass.cs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using LanguageExt;
using LanguageExt.TypeClasses;

using static LanguageExt.Prelude;

namespace free_monad_vs_typeclass {
    public interface ResourcePrinterTypeclass<A> : Typeclass {
        Unit Print(ResourceWrapper resource, string output);
        ResourceWrapper AcquireResource();
    }

    public struct ResourcePrinterTypeclassUnit : ResourcePrinterTypeclass<Unit> {
        public Unit Print(ResourceWrapper resource, string output) =>
            fun(() => resource.Print(output))();
        
        public ResourceWrapper AcquireResource() =>
            new ResourceWrapper();
    }
}






|
|
|


|
|
|

|
|


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using LanguageExt;
using LanguageExt.TypeClasses;

using static LanguageExt.Prelude;

namespace free_monad_vs_typeclass {
    public interface ResourcePrinterTypeclass<MA, A> {
        MB Print<MonadB, MB>(ResourceWrapper resource, string output) where MonadB: struct, Monad<MB, Unit>;
        MB AcquireResource<MonadB, MB>() where MonadB: struct, Monad<MB, ResourceWrapper>;
    }

    public struct ResourcePrinterTypeclassUnit : ResourcePrinterTypeclass<Identity<Unit>, Unit> {
        public MB Print<MonadB, MB>(ResourceWrapper resource, string output) where MonadB: struct, Monad<MB, Unit> =>
            default(MonadB).Return(fun(() => resource.Print(output))());
        
        public MB AcquireResource<MonadB, MB>() where MonadB: struct, Monad<MB, ResourceWrapper> =>
            default(MonadB).Return(new ResourceWrapper());
    }
}