junkcode  Check-in [8fbeebdd41]

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

Overview
Comment:Mess around with language-ext typeclasses and free monads.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:8fbeebdd418cc5c8c345fb5d65c1f234b79f060421851a75a29ef4e4257da683
User & Date: jaccarmac 2018-01-29 04:06:15
Context
2018-01-29
04:07
Fix line endings. check-in: 976bae5ae0 user: jaccarmac tags: trunk
04:06
Mess around with language-ext typeclasses and free monads. check-in: 8fbeebdd41 user: jaccarmac tags: trunk
2018-01-19
05:05
Link to Casey's video on IMGUIs. check-in: 86d77832a2 user: jaccarmac tags: trunk
Changes

Changes to .fossil-settings/clean-glob.


1

2
3
4

**/nimcache/

**/target/
**/*.pyc
polymorphic-recursion/nim/polymorphic_recursion
>

>



1
2
3
4
5
6
**/bin/
**/nimcache/
**/obj/
**/target/
**/*.pyc
polymorphic-recursion/nim/polymorphic_recursion

Added free-monad-vs-typeclass/Program.cs.













































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
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
    {
        public static void Main(string[] args)
        {
            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!"));
    }
}

Added free-monad-vs-typeclass/ResourcePrinterFree.cs.





































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using LanguageExt;
using System;

using static LanguageExt.Prelude;

namespace free_monad_vs_typeclass
{   
    public abstract class ResourcePrinterFree<T> {
        public class Return : ResourcePrinterFree<T> {
            public T Value;
            public Return(T value) {
                Value = value;
            }
        }

        public class Print : ResourcePrinterFree<T> {
            public string Output;
            public Func<ResourcePrinterFree<T>> Next;
            public Print(string output, Func<ResourcePrinterFree<T>> next) {
                Output = output;
                Next = next;
            }
        }

        public class AcquireResource : ResourcePrinterFree<T> {
            public Func<ResourcePrinterFree<T>> Next;
            public AcquireResource(Func<ResourcePrinterFree<T>> next) {
                Next = next;
            }
        }
    }

    public static class ResourcePrinterFreeExtensions {
        public static T Map<T>(this ResourcePrinterFree<T> op, Func<ResourcePrinterFree<T>, T> f) =>
            f(op);
    }

    public static class FreeInterpreter {
        public static Unit Interpret(ResourcePrinterFree<Unit> op) =>
            Interpret(None, op);

        public static Unit Interpret(Option<ResourceWrapper> resource, ResourcePrinterFree<Unit> op) =>
            resource.Map(_ => op).IfNone(new ResourcePrinterFree<Unit>.AcquireResource(() => op)).Apply(o =>
                  o is ResourcePrinterFree<Unit>.Return r ? r.Value
                : o is ResourcePrinterFree<Unit>.Print p ? resource.Map(fun((ResourceWrapper rs) => rs.Print(p.Output))).Apply(_ => Interpret(resource, p.Next()))
                : o is ResourcePrinterFree<Unit>.AcquireResource a ? use(new ResourceWrapper(), rw => Interpret(rw, a.Next()))
                : throw new InvalidOperationException()
            );
    }
}

Added 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();
    }
}

Added free-monad-vs-typeclass/ResourceWrapper.cs.



































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using LanguageExt;
using System;

using static LanguageExt.Prelude;

namespace free_monad_vs_typeclass
{
    public class ResourceWrapper : IDisposable {
        public void Dispose() {
            Console.WriteLine("Disposing");
        }

        public void Print(string output) {
            Console.WriteLine(output);
        }
    }
}

Added free-monad-vs-typeclass/free-monad-vs-typeclass.csproj.

























>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="LanguageExt.Core" Version="2.2.1-alpha" />
  </ItemGroup>

</Project>