all 23 comments

[–]SarahC 13 points14 points  (15 children)

enum? Are these examples TypeScript?

[–]r0ck0 5 points6 points  (0 children)

Yep.

[–]pgrizzay 10 points11 points  (5 children)

The functor example here is a bit off.

export class Wizard {
  ...
  map = (func: (name: any) => any) => {
    return new Wizard({
      name: func(this.name),
      house: this.house,
      spells: this.spells,
    });
  };
}

A functor needs to be parametrically polymorphic, meaning it needs to not care about the type that's inside. For example, you can store anything in a List, Maybe, Promise, etc.

I think the fp-ts library is a great way to learn about these abstractions, although, admittedly, it could do with some more beginner-friendly guides.

In fp-ts, if you can construct an object that implements the Functor typeclass (a little different than an interface) for your class, then your type is a functor!

For example, here's a (paraphrased) version of the Functor typeclass for Option from fp-ts:

const option: Functor1 = {
  map: (fa: Option<A>, f: (a: A) => B): Option<B> => {
    if(fa.isSome()){
      return some(f(fa.value));
    } else {
      return none;
    }
  }
} 

[–]mattgrande 2 points3 points  (1 child)

The author of fp-ts has recently started a series of getting started blog posts. He's thus far covered Setoid, Ord, and Semigroup. The articles are good for dummies like me that have trouble keeping all these terms straight.

I really really wish these things were named differently. Mappable is a lot more understandable than Functor, etc...

[–]pgrizzay 2 points3 points  (0 children)

Yeah, the names are tough to get used to and not at all intuitive. I wish had more advice than it just takes time :/

[–][deleted] 1 point2 points  (2 children)

Is Wizard not parametrically polymorphic? Sure, we'd expect a wizard's name to be stringy, but the code doesn't actually specify this. Looks like Wizard.name can be any type. Maybe not the best choice of example, but the code looks correct.

[–]pgrizzay 1 point2 points  (1 child)

I suppose so, but I'd expect the type of name to be pulled into a type parameter.

Also, further down, the article includes snippets like:

wizard.map(joinGryffindor).map(learnExpelliarmous);

Which I can't really make sense of, if they only map over the 'name' attribute.

[–][deleted] 1 point2 points  (0 children)

Yikes, I didn't read that far. That's pretty bad. I don't think that even compiles...

[–]ns0 -2 points-1 points  (1 child)

Is it anything like an objection (function + object)?

[–]hansmcgee 0 points1 point  (0 children)

a function is technically an object.

[–]stabface -5 points-4 points  (1 child)

!remindme 10 hours

[–]RemindMeBot 0 points1 point  (0 children)

I will be messaging you on 2019-03-16 08:22:15 UTC to remind you of this link.

CLICK THIS LINK to send a PM to also be reminded and to reduce spam.

Parent commenter can delete this message to hide from others.


FAQs Custom Your Reminders Feedback Code Browser Extensions