urbandrone / futils

Utilities for generic functional programming
MIT License
22 stars 0 forks source link

please support typescript definition file #2

Open renyaoxiang opened 6 years ago

renyaoxiang commented 6 years ago

I am using typescript and would like to use futils, It would be very grace to support typescript definition file

urbandrone commented 6 years ago

Hi renyaoxiang, thanks for your suggestion! Although I havn't used typescript alot, it appears to me this would make futils much more accessible to users of it. Since this is basically a one man show it won't happen over night, but I will do my best to support a definiton file as soon as possible.

In the meantime, I hope you're going to give futils a try anyway. 😄

renyaoxiang commented 6 years ago

I'm trying and waiting in hope it. Somehow confused with too many methods , I could not keep in mind of the method names. If it has some specific about monad implment , to make it easy for me to remember

urbandrone commented 6 years ago

Please also take a look into the examples provided, especially the "Basics" section. The concept of monads will be explained in these two:

  1. A formal introduction can be found in "Monads!"
  2. The State monad is explained in "Stateful transformations"

If monads are the thing you struggle with, a (incomplete) TypeScript interface for most of them would roughly look like this:

interface Monad {
    private MonadicValue: any;
    constructor(a: any);
    static of(a: any): Monad; /* this is not possible in TS interfaces as of now */
    map(f: (a: any) => any): Monad;
    flatten(): Monad;
    flatMap(f: (a: any) => Monad): Monad;
    fold(f: (a: any) => any, x: any): any;
    ap(m: Monad): Monad;
}

It doesn't tell alot just by itself and instead looks more confusing, because you cannot tell what a monad does simply by looking at the signatures of it's methods. For example, the signature of flatten looks like it doesn't do anything useful while the signature of fold can be misunderstood as "it applies 'f' to 'x'". And what is that weird ap doing? Such questions will be answered in the first tutorial "Monads!".

Conceptually, a monad is in fact much less than the interface above says – think of it as a container which allows to sequence computations which return monads themselves (that's what flatMap really does), as well as to unnest nested containers of the same type (that's the purpose of flatten). The reason why there are more methods available, is because the monadic datastructures in futils implement other "interfaces" as well, like Functor, Applicative and Foldable (to name a few). That said, a "monad" is any datastructure which a) implements certain methods that b) operate in a specific way. Therefor "interface" isn't really the correct term, because a TS interface usually describes just the shape of a structure – languages like Haskell say that a monad is a typeclass. You can think of a typeclass like some contract, which defines specific behaviours for specific methods/functions a structure must have to fulfill the contract.

Because all of this might be a bit confusing, here's a concrete example using arrays which are a monadic container given this ECMAScript proposal will be implemented on them. It uses TypeScript syntax for your convenience:

const list: Array<string> = [ 'a.b', 'c.d', 'e.f' ];

function splitOnDot (x: string): Array<string> {
    return x.split('.');
}

const nestedList: Array<Array<string>> = list.map(splitOnDot);

As you can see, just mapping over the array with splitOnDot returns us an array of arrays of strings – that's bad, because usually we don't want extra nesting. If arrays become monads, we can simply do either of these instead without nesting:

const flatList1: Array<string> = list.map(splitOnDot).flatten();

const flatList2: Array<string> = list.flatMap(splitOnDot);

Again, please take a look into the "Monads!" example.

renyaoxiang commented 6 years ago

Thank you very much :+1: , your replay given me more info for understanding of monads.