JSFiddle - React, Tailwind, and code Playground

by dimitrs_papadimitriou

JavaScript

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Functors.IO;
namespace BaseTree
{

    public static partial class ƒ
    {


        public static Id<T1> App<T, T1>(this Id<Func<T, T1>> @this, Id<T> fa) => @this.Map(f => f(fa.source));
        // public static List<T1> App<T, T1>(this List<Func<T, T1>> @this, List<T> fa) => fa.Select(a=> @this.Select(g =>g(a) )).ToList();

        public static async Task<V> SelectMany<T, U, V>(
    this Task<T> source, Func<T, Task<U>> selector, Func<T, U, V> resultSelector)
        {
            T t = await source;
            U u = await selector(t);
            return resultSelector(t, u);
        }

        public static async Task<U> Select<T, U>(
            this Task<T> source, Func<T, U> selector)
        {
            T t = await source;
            return selector(t);
        }

        public static async Task<T> Where<T>(
            this Task<T> source, Func<T, bool> predicate)
        {
            T t = await source;
            if (!predicate(t)) throw new OperationCanceledException();
            return t;
        }


        public static Id<T> Join<T>(this Id<Id<T>> @this) => @this.source;
    }

    public interface IMonad<T>
    {
        IMonad<T1> Bind<T1>(Func<T, IMonad<T1>> bind);
    }
    public class Id<T> : IMonad<T>
    {
        public T source;
        [Pure]
        public Id(T v) => source = v;

        public Id<T1> Map<T1>(Func<T, T1> map) => new Id<T1>(map(source));
        public IMonad<T1> Bind<T1>(Func<T, IMonad<T1>> bind) => bind(source);


    }
    public abstract class Tree<T> //: IEnumerable<T>
    {


        public abstract Tree<T1> Map<T1>(Func<T, T1> f);
        public abstract T1 MatchWith<T1>((Func<Leaf<T>, T1> Leaf, Func<Tree<T>, Tree<T>, T1> Node) pattern);


    }


    public class Node<T> : Tree<T>
    {
        public override...