ericelliott / rtype

Intuitive structural type notation for JavaScript.
MIT License
1.13k stars 38 forks source link

Reducer built-ins #143

Open ericelliott opened 5 years ago

ericelliott commented 5 years ago

I use reducers all the time, and frequently use type parameters in documentation named "reducer", and "foldable". It would probably be a good idea to formally define them.

interface Reducible {
  reduce?: ((a?, c?, i: Number, Foldable) => a, initial: Any) => Any
}

interface Foldable = Reducible | Iterable

interface Reducer = (a?, c?, i: Number, Foldable) => a

Example

Transducer = Reducer => Reducer

createTransducer = ({
  step: Reducer,
  init?: Reducer,
  result?: Reducer,
  next?: Reducer
}) => Reducer => Reducer

transduce = (Reducer, initial: Any, xform: Transducer, Foldable) => Any

Mappable

interface Mappable {
  map: Functor(a) ~> (a => b) => Functor(b)
}

Change IterableObject to Iterable

I know it's a breaking change, but AFAIK, there's not a lot of software relying on this, yet.

Mouvedia commented 5 years ago
  1. where's mappable?
  2. shouldn't it be capitalized?
ericelliott commented 5 years ago

Yes, I think we should also add mappable/functor (aliased).

This proposal defines "foldable", "reducer", "iterator" and "iterable" (just in case the last two aren't defined yet — they're ES2015 built-in interfaces.

ericelliott commented 5 years ago

I'm on the fence. The ECMAScript spec does not capitalize these kinds of types, though they do capitalize all the built-in constructors. Should we follow the same convention? I have been lowercasing all types that are NOT built-in constructors, except for Any and Void, I think.

actually, I think the idea to alias "Functor" is probably a bad one. I'd rather reserve that for the higher-kinded type constructor, e.g.:

map = (a => b) => Functor(a) => Functor(b)
Mouvedia commented 5 years ago

I don't have an opinion on this new builtin but it should match our builtin types if its addition is warranted.

Any, Void, Iterable, Predicate, TypedArray are all capitalized.

ericelliott commented 5 years ago

OK, I'm on board. Capitalize all the things!

Mouvedia commented 5 years ago

I think the idea to alias

You should propose these aliases separately; keep it simple for now.

interface Foldable = Reducible | Iterable;

I find this confusing; a lot of people think of fold as an alias of reduce.

ericelliott commented 5 years ago

Anything that can be iterated can also be reduced. You just have to bring your own reduce() to the party if the method is missing.

But you're right. I should leave that off.

Oops. Can't leave it off, or it won't work for transducers or a pure reduce() definition.

ericelliott commented 5 years ago

Related: #120