Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
VikingofRock
Aug 24, 2008




Presto posted:

You already lost me.

They mean a type like the List in List<T> or the Optional in Optional<T>.

Adbot
ADBOT LOVES YOU

poemdexter
Feb 18, 2005

Hooray Indie Games!

College Slice

sunaurus posted:

Nobody can beat this guy

This is his best work, probably

https://www.linkedin.com/in/jonschlinkert/

His linkedin profile is the best. president and founder of so many things, gives talks about entrepreneurship, and also somehow the #1 maintainer of npm with over 400 modules. Watch out Elon Musk!

JawnV6
Jul 4, 2004

So hot ...

Carbon dioxide posted:

I mean, I do some functional programming and I'm not a theory nerd, and as a result monads are simple for me. They're literally a box you can put things of any type in and there's a way to change what's in the box without changing the box itself. That's all (and everything >95% of functional programmers ever need to know about them).

And anyone who disagrees with this definition can go stick their sensitive bits in an endofunctor.

i'm still wondering what box metaphor we're using here and how it compares to procedural mutable boxes that humans have interacted with

mekkanare
Sep 12, 2008
We have detected you are using ad blocking software.

Please add us to your whitelist to view this content.

JawnV6 posted:

i'm still wondering what box metaphor we're using here and how it compares to procedural mutable boxes that humans have interacted with

After a box's contents have been emptied, it can be repurposed for a space ship :eng101:

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

poemdexter posted:

https://www.linkedin.com/in/jonschlinkert/

His linkedin profile is the best. president and founder of so many things, gives talks about entrepreneurship, and also somehow the #1 maintainer of npm with over 400 modules. Watch out Elon Musk!

His 'ansi-cyan' library is used in 223,321 other repositories. It's enough to make me quit programming.

Ruggan
Feb 20, 2007
WHAT THAT SMELL LIKE?!


Nolgthorn posted:

His 'ansi-cyan' library is used in 223,321 other repositories. It's enough to make me quit programming.

How many of those libraries is he an owner or contributor to?

I dunno that I believe that stat by the way.

Doom Mathematic
Sep 2, 2008
I believe it, his stuff is like an infestation. He has myriad atom-sized modules, which are used in slightly less utterly useless modules, which get used in slightly useful modules, which get used in middlingly important webpack plugins, and suddenly a single line of code which checks whether a string has a space in it is being used by NASA to serve one of NASA's less important websites and "my code is used by NASA every day, don't forget to watch and star!"

spiritual bypass
Feb 19, 2008

Grimey Drawer

Nolgthorn posted:

His 'ansi-cyan' library is used in 223,321 other repositories. It's enough to make me quit programming.

Maybe it should just be enough to make you quit programming JS

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Or stop importing packages to do built-in language capabilities

ryde
Sep 9, 2011

God I love young girls
The problem is the garbage is a transitive dependency pulled in by actually useful packages.

Those actually-useful packages shouldn’t be pulling them and should be actively minimizing their dependency chains but at some point developers decided that pulling in a library for every trivial case was frictionless and meant you were a good and smart.

redleader
Aug 18, 2005

Engage according to operational parameters
Burritos

Phobeste
Apr 9, 2006

never, like, count out Touchdown Tom, man
The only law of monads is that if somebody mentions the word then 10 others must appear to explain them, all slightly differently

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
I had heard of functors when I worked at a place that did C programming. Until this page I thought they were just a shortened way of saying "function pointers". I know what those are, but not functors, apparently. I don't know what "monads" are but I'm guessing they do not show up in JavaScript because I don't hear about them around what I do now.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem

ryde posted:

The problem is the garbage is a transitive dependency pulled in by actually useful packages.

Those actually-useful packages shouldn’t be pulling them and should be actively minimizing their dependency chains but at some point developers decided that pulling in a library for every trivial case was frictionless and meant you were a good and smart.

I think what usually happens is the author of one of the garbo-utils sends a pull request to an actually useful library to make it use their piece of poo poo one-liner utility, and the maintainer of the useful library doesn't really have time to look into it so just approves it.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Ah gently caress that is so malicious, and explains a lot about npm

Bongo Bill
Jan 17, 2012

Dumb Lowtax posted:

I had heard of functors when I worked at a place that did C programming. Until this page I thought they were just a shortened way of saying "function pointers". I know what those are, but not functors, apparently. I don't know what "monads" are but I'm guessing they do not show up in JavaScript because I don't hear about them around what I do now.

Monads are everywhere. Arrays are monads, for instance. It's just only in academic languages are they called monads.

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
Oh son of a bitch, I had never noticed the flat() method in the JS Array spec. Thank you!

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


arrays are not monads

array is a monad

:reject:

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Elegant error handling with the JavaScript Either Monad

This is a good introduction because it doesn't spend a lot of time explaining what monads are in general, and it shows you how to use one to solve a real problem.

The error handling methods that you get out of functional programming are good and people should be paying attention to them.

zergstain
Dec 15, 2005

So std::list<T> would be a monad, but types like std::map<K, T> would not?

xtal
Jan 9, 2011

by Fluffdaddy

zergstain posted:

So std::list<T> would be a monad, but types like std::map<K, T> would not?

edited post after research: It is in Scala, but not in Haskell. It's possible to implement it yourself, but it depends on the semantics you want it to have.

xtal fucked around with this message at 05:26 on Jun 20, 2019

VikingofRock
Aug 24, 2008




xtal posted:

edited post after research: It is in Scala, but not in Haskell. It's possible to implement it yourself, but it depends on the semantics you want it to have.

That's interesting, what are the differences in semantics that make it possible in Scala?

Carbon dioxide
Oct 9, 2012

VikingofRock posted:

That's interesting, what are the differences in semantics that make it possible in Scala?

The map() function on a Map in scala expects a function from (K, V), that is a tuple, to a new (key, value) tuple. I hardly ever need this because there's a convenience mapValues() function available that leaves the keys as they are.

I don't know from the top of my head what happens if map() generates a duplicate key.

Flatmap works similarly btw. There's even a special notation available for these map entries-as-tuples: (key -> value)

Carbon dioxide fucked around with this message at 05:59 on Jun 20, 2019

Athas
Aug 6, 2007

fuck that joker

zergstain posted:

So std::list<T> would be a monad, but types like std::map<K, T> would not?

There are two answers, depending on what you were getting at:

1) This is where the ad-hoc notation of conventional languages become a problem. The type std::map<K, T> would in Haskell be written as 'Map k t', which cannot be a monad. The partially applied type 'Map k' could be, though. In Haskell, a monad instance is defined for a type constructor (a type of kind '* -> *'), not a fully applied type. This is less natural to talk about with a C++-ish notation for parametric polymorphism. You can write 'std::map<K>', but I don't think the compiler will accept it.

2) You cannot define a monad for something like std::map<K, T> specifically, since the 'return' method is only called with a T. You'd have to conjure up a K out of nowhere.

Vanadium
Jan 8, 2005

Dumb Lowtax posted:

I had heard of functors when I worked at a place that did C programming. Until this page I thought they were just a shortened way of saying "function pointers". I know what those are, but not functors, apparently.

Nah, that's just, C++ people use functor to mean anything that can be called like a function, so also function pointers and objects that define operator(), whereas haskell people use functor to mean something utterly unrelated.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

Carbon dioxide posted:

Wow the coding horror is in the thread. While technically correct this is the kind of long winded stuff that chases good devs away from functional programming. And there's absolutely no need for an explanation like this. ...

... or there are multiple ways to explain concepts like "monad", some of which will work for some people and not others? What you're essentially saying here is: if you don't think like me (in particular, if you didn't find the explanation to be unhelpful like I did) then you aren't really a programmer. And the only appropriate response to that is to tell you to gently caress off.

VikingOfRock's explanation was the kind of concept-explanation I am used to reading, as someone who was a failed mathematician in a previous life. It makes sense while not necessarily being the kind of explanation that would be helpful for every possible reader.

redleader
Aug 18, 2005

Engage according to operational parameters
But then you could have gotten an equivalent mathematical introduction to monads via one of the hundred million other monad tutorials on the internet.

It's ok though, because we need to account for the Zeroth Monad Law:

Phobeste posted:

The only law of monads is that if somebody mentions the word then 10 others must appear to explain them, all slightly differently

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.

redleader posted:

But then you could have gotten an equivalent mathematical introduction to monads via one of the hundred million other monad tutorials on the internet.

It's ok though, because we need to account for the Zeroth Monad Law:

Zeroth Monad Law, more like Rashomonad.

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

redleader posted:

But then you could have gotten an equivalent mathematical introduction to monads via one of the hundred million other monad tutorials on the internet.

It's ok though, because we need to account for the Zeroth Monad Law:

it's almost as though vanishingly little that human beings ever do is truly original, and we are all confused animals with illusions of free will, trying to make sense of a chaotic and capricious world.

Ruggan
Feb 20, 2007
WHAT THAT SMELL LIKE?!


Knock knock.
Who’s there?
Monad
Monad who?

VikingofRock posted:

Monads (in the Haskell sense) are types which take at least one type parameter (e.g. List<T> or Optional<T>) which implement two specific functions. (For this post I'll be using non-Haskell notation and semi-traditional names for these functions, to make things more clear.)

The first of these functions, pure, has the signature pure(T) -> M<T> where M is the monad in question. So in the case of List, this would be pure(T) -> List<T>. You could imagine this being implemented as a function which when given a value, returns a list containing just that value.

The second of these functions, bind, has the signature bind(M<T>, Function<T> -> M<U>) -> M<U>. (That second argument is any function which takes a T and returns an M<U>, where T and U are generic arguments.). In the case of a List, this would be bind(List<T>, Function<T> -> List<U>) -> List<U>. You could imagine this being implemented as a function which, given a list of Ts and a function which turns a T into a list of Us, applies that function to each T in the original list and then concatenates the resulting List<U>s.

These functions have to obey a few rules, which are there to make sure you're not doing anything weird with your implementation. If these are confusing, it's IMO okay to just wholly skip over the details of these laws until you understand monads a little better, but I've listed them below for the curious.

  • Given any f which is a Function<T> -> M<U> and t which is a T, we have bind(pure(t), f) == f(t).
  • Given any concrete instance of our monad, m, we have bind(m, pure) == m. A concrete instance of the monad for List could be e.g. the empty list, [1, 2, 3], etc.
  • Given functions f and g with the above signature, and a concrete instance of our monad m, we have bind(bind(m, f), g) == bind(m, lambda x: bind(f(x), g)). This is the most confusing of these laws as written, but if you stare at it long enough it just means that bind is associative. And again, if you're confused, don't get hung up on these laws for now.

Now that we have the definition of monads written out, we can get to the more important question: why do we care? It turns out that this is a surprisingly useful abstraction.

In the case of Optionals, you can use it to chain lots of functions which each assume that their arguments are non-"null", and which produce potentially-"null" values. This chain can short-circuit if any of the produced values are null, so it can save you from writing a bunch of null checks.

In the case of Lists, this can be useful to model the list of potential states of a state machine, where at each state can go to one of multiple potential states.

Those are in a lot of ways the most classic monads (and I'm probably forgetting some other classic examples). But monads can also be used to model randomness, parsing, and I/O operations. This all really shines in a language like Haskell, with a lot of built-in machinery which makes using monads easy.

Btw, since someone asked about comonads earlier: comonads are just monads "in reverse", which define

  • copure, with the signature copure(M<T>) ->T
  • cobind, with the signature cobind(M<T>, Function<M<T>> -> U) -> M<U>

There's a deep sense in which monads are useful for modeling Producers and comonads are useful for modeling Consumers, which you can pick up as play around with them enough.

Ruggan
Feb 20, 2007
WHAT THAT SMELL LIKE?!


For the record I also thought the effort explanation was nice. :shrug:

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
monads and strife,

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense
I'm pretty sure npm has given me extremely serious ptsd, in line with what soldiers returning from Vietnam must have experienced. Or maybe worse. I think all the blue haired developers working there should pay for my therapy, while a replacement is finally made. I suggest namespaces for one thing. The ability to delete repos, mainly just to discourage overuse of modules, and to encourage people to make their own forks. I think if your super popular library needed to manually stay up to date with dependencies which may or may not still exist, you'd feel encouraged to limit your use of them.

Nolgthorn fucked around with this message at 14:20 on Jun 20, 2019

Volte
Oct 4, 2004

woosh woosh
My only comment is that the types that participate in monadic computations aren't necessarily monads themselves. A monad is like an adapter of a particular shape which lets an arbitrary type of object participate in monadic computations (where "monadic computation" has already been sufficiently defined in this thread). In Haskell, it's common to say that a type is a particular type class when there's an instance of it defined, but in other languages that might not be true. It might be more correct to say "there is a monad for lists", and when someone says "list monad", they (should) mean "the bundle of functions and identities that allows lists to operate monadically" and not a synonym for "list type". In some languages it may be possible or necessary to explicitly pass that bundle alongside the value you're operating on monadically, giving you the opportunity to choose one of many potential monads that satisfy the monad laws. Some types such as Either[L, R] may have multiple possible monads. In the case of Either, you can hold one parameter to be fixed, which gives rise to a Maybe-like monad in one side or the other.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Nolgthorn posted:

His 'ansi-cyan' library is used in 223,321 other repositories. It's enough to make me quit programming.

Makes me want to make an NPM clone that filters out anything tainted by his work.

zergstain
Dec 15, 2005

Vanadium posted:

Nah, that's just, C++ people use functor to mean anything that can be called like a function, so also function pointers and objects that define operator(), whereas haskell people use functor to mean something utterly unrelated.

I was only familiar with functor being used to mean "function object," i.e. a type that defines operator()().

Athas posted:

There are two answers, depending on what you were getting at:

1) This is where the ad-hoc notation of conventional languages become a problem. The type std::map<K, T> would in Haskell be written as 'Map k t', which cannot be a monad. The partially applied type 'Map k' could be, though. In Haskell, a monad instance is defined for a type constructor (a type of kind '* -> *'), not a fully applied type. This is less natural to talk about with a C++-ish notation for parametric polymorphism. You can write 'std::map<K>', but I don't think the compiler will accept it.

2) You cannot define a monad for something like std::map<K, T> specifically, since the 'return' method is only called with a T. You'd have to conjure up a K out of nowhere.

I was asking specifically about C++ STL containers. If std:: appears in other languages, my bad.

Does the STL provide equivalent methods to pure and bind that can be applied to monad types, or is that just a Haskell specific thing?

Happy Thread
Jul 10, 2005

by Fluffdaddy
Plaster Town Cop
I never understand when someone hates on another poster just for posting a lot of text. Like, whoever it is clearly took some effort to type it, and they expended it for free. With pure intentions. Tearing apart its mistakes is one thing but you're the rear end in a top hat if you hate on the length itself, like just don't read it if it bothers you that much obviously. It takes an incredible misanthrope to think that the inherent concept of effort is something to jump down someone's throat for, and to poo poo on it before even giving others their turn to engage it, knowing full well how it will feel for the author to have that immediate dismissal. At least those types get immediately banned in Lowtax threads when he announces something new and they try to pull that poo poo on him.

Athas
Aug 6, 2007

fuck that joker

zergstain posted:

Does the STL provide equivalent methods to pure and bind that can be applied to monad types, or is that just a Haskell specific thing?

You can easily define something like pure and bind for various C++ types, and now that C++ supports lambdas it might not even look too bad. What is more difficult is writing code that is generic over the monad that it is using. Actually, with templates it might not even be that difficult (since templates are untyped, the usual issues with higher-rank types do not crop up), but the experience will likely be painful.

zergstain
Dec 15, 2005

Athas posted:

You can easily define something like pure and bind for various C++ types, and now that C++ supports lambdas it might not even look too bad. What is more difficult is writing code that is generic over the monad that it is using. Actually, with templates it might not even be that difficult (since templates are untyped, the usual issues with higher-rank types do not crop up), but the experience will likely be painful.

In other words, it doesn't provide them, and if you want that functionality, you'll need to roll your own. I take it then, that the existence of those functions or equivalents aren't necessary to satisfy the definition of monad.

Adbot
ADBOT LOVES YOU

ryde
Sep 9, 2011

God I love young girls

Munkeymon posted:

Makes me want to make an NPM clone that filters out anything tainted by his work.

I honestly wonder if we need something akin to a cyclomatic complexity check for dependencies in package managers.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply