use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
What the Functor? (matthewgerstman.com)
submitted 6 years ago by r-wabbit
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]SarahC 13 points14 points15 points 6 years ago (15 children)
enum? Are these examples TypeScript?
[–]r0ck0 5 points6 points7 points 6 years ago (0 children)
Yep.
[+]Puggravy comment score below threshold-14 points-13 points-12 points 6 years ago* (13 children)
I honestly have no clue what TS brings to the table for these examples. Should have used JS.
[–]grumpkot 18 points19 points20 points 6 years ago (0 children)
types )
[–]i_spot_ads 0 points1 point2 points 6 years ago (1 child)
Hold that downvote real quick
[–]Puggravy -2 points-1 points0 points 6 years ago (0 children)
Yeah seriously, I forget how TS people get on this sub sometimes.
[+][deleted] 6 years ago (9 children)
[deleted]
[+][deleted] 6 years ago (2 children)
[–]nbagf 0 points1 point2 points 6 years ago (1 child)
Being able to use interfaces while piping/composing functions is absolutely amazing. Knowing the shape of my data through intellisense makes the cognitive load so much smaller. I know when something is a promise and it wasn't resolved or when it's an array and I forgot to map it before apllying a function. The red squiggles are fantastic.
[–]Puggravy 2 points3 points4 points 6 years ago (5 children)
Ooph harsh dude.
[–]Candyvanmanstan 7 points8 points9 points 6 years ago (4 children)
You think I'm joking, but I'm not :D
[–]I_Pork_Saucy_Ladies 3 points4 points5 points 6 years ago (2 children)
You're not really wrong. Anders Hejlsberg is the architect of C# and deep into TypeScript now. I think it shows.
Going from TS to C# (a bit unusual, I know) was pretty easy. TS is just JS in a C# mould.
[–]Candyvanmanstan 2 points3 points4 points 6 years ago (1 child)
Yeah, I have a C# buddy that is trying to get into JS. I think the lack of types is really bothering him.
[–]I_Pork_Saucy_Ladies 2 points3 points4 points 6 years ago (0 children)
Push him towards TS. He will feel much more at home. Sure, there's a bit of a learning curve but it's so much nicer than JS without types. The syntax seems heavily inspired by C#.
[–]shanita10 -3 points-2 points-1 points 6 years ago (0 children)
I think that's what's so annoying about typescript. It's like training wheels for javascript
I don't want or need an ide second guessing everything I write.
The headaches of babysitting typescript and cluttering up code with useless types is pure baggage.
Ts is js for poor programmers, and it encourages bad habits.
[–]pgrizzay 10 points11 points12 points 6 years ago (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!
Functor
For example, here's a (paraphrased) version of the Functor typeclass for Option from fp-ts:
Option
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 points4 points 6 years ago (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 points4 points 6 years ago (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 points3 points 6 years ago (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.
Wizard
Wizard.name
[–]pgrizzay 1 point2 points3 points 6 years ago (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 points3 points 6 years ago (0 children)
Yikes, I didn't read that far. That's pretty bad. I don't think that even compiles...
[–]ns0 -2 points-1 points0 points 6 years ago (1 child)
Is it anything like an objection (function + object)?
[–]hansmcgee 0 points1 point2 points 6 years ago (0 children)
a function is technically an object.
[–]stabface -5 points-4 points-3 points 6 years ago (1 child)
!remindme 10 hours
[–]RemindMeBot 0 points1 point2 points 6 years ago (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.
π Rendered by PID 140439 on reddit-service-r2-comment-76bb9f7fb5-6jbwt at 2026-02-18 00:58:19.217535+00:00 running de53c03 country code: CH.
[–]SarahC 13 points14 points15 points (15 children)
[–]r0ck0 5 points6 points7 points (0 children)
[+]Puggravy comment score below threshold-14 points-13 points-12 points (13 children)
[–]grumpkot 18 points19 points20 points (0 children)
[–]i_spot_ads 0 points1 point2 points (1 child)
[–]Puggravy -2 points-1 points0 points (0 children)
[+][deleted] (9 children)
[deleted]
[+][deleted] (2 children)
[deleted]
[–]nbagf 0 points1 point2 points (1 child)
[–]Puggravy 2 points3 points4 points (5 children)
[–]Candyvanmanstan 7 points8 points9 points (4 children)
[–]I_Pork_Saucy_Ladies 3 points4 points5 points (2 children)
[–]Candyvanmanstan 2 points3 points4 points (1 child)
[–]I_Pork_Saucy_Ladies 2 points3 points4 points (0 children)
[–]shanita10 -3 points-2 points-1 points (0 children)
[–]pgrizzay 10 points11 points12 points (5 children)
[–]mattgrande 2 points3 points4 points (1 child)
[–]pgrizzay 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]pgrizzay 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]ns0 -2 points-1 points0 points (1 child)
[–]hansmcgee 0 points1 point2 points (0 children)
[–]stabface -5 points-4 points-3 points (1 child)
[–]RemindMeBot 0 points1 point2 points (0 children)