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
zergstain
Dec 15, 2005

ratbert90 posted:

Yep, after the last page or so, I am now 100% in the impostor syndrome camp again. :smith:

This is one of the few times where I don't feel hopelessly incompetent. The biggest offenders for me are probably discussions involving Haskell, or advanced C++ templates. You mention monads, you've probably lost me.

Adbot
ADBOT LOVES YOU

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


The number of people who really understand monads is far smaller than the number of people who sound like they do.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
it's like a burrito in the category of endofunctors

zergstain
Dec 15, 2005

Jabor posted:

endofunctors

Is that even a real word?

Phobeste
Apr 9, 2006

never, like, count out Touchdown Tom, man
Yeah it’s a ska band

1337JiveTurkey
Feb 17, 2005

Imagine having to read this bad boy before having the slightest idea what the codebase you're working on even does: https://bartoszmilewski.com/2017/01/02/comonads/

Don't worry it follows trivially from the definition of monad. I'm sorta convinced that the principal benefit of functional programming is weeding out all the people who aren't dedicated enough to figure it all out.

xtal
Jan 9, 2011

by Fluffdaddy

ultrafilter posted:

The number of people who really understand monads is far smaller than the number of people who sound like they do.

Monads are really simple, that's part of the reason they're so hard to understand. Also, the fact that they're an interface and not an object.

Absurd Alhazred
Mar 27, 2010

by Athanatos
Wanna punch those monads RIGHT INTERFACE!

OddObserver
Apr 3, 2009

1337JiveTurkey posted:

Imagine having to read this bad boy before having the slightest idea what the codebase you're working on even does: https://bartoszmilewski.com/2017/01/02/comonads/

Don't worry it follows trivially from the definition of monad. I'm sorta convinced that the principal benefit of functional programming is weeding out all the people who aren't dedicated enough to figure it all out.

Well, or one can just use an eager programming language (like the LISP and ML families), and just do stuff with side effects directly. Monads are more of a Haskell and functional purist thing rather than straight up functional thing, I think

Doc Hawkins
Jun 15, 2010

Dashing? But I'm not even moving!


ultrafilter posted:

The category of people who really understand monads is a subcategory of people who sound like they do.

there

Jazerus
May 24, 2011



but are "people who sound like they do" endofunctors??

zergstain
Dec 15, 2005

Phobeste posted:

Yeah it’s a ska band

For real, I've worked a bit with <algorithm>, so I have some idea what a functor is. I'm not even going to try to guess what you get when you add the endo- prefix though. Fortunately Google exists.

OddObserver
Apr 3, 2009

zergstain posted:

For real, I've worked a bit with <algorithm>, so I have some idea what a functor is. I'm not even going to try to guess what you get when you add the endo- prefix though. Fortunately Google exists.

C++ functors are totally different from category theory functors (which are just abstract nonsense).

megalodong
Mar 11, 2008

you don't really need to understand the category theory definitions of functors and monads to understand them in haskell/c# etc. They were inspired by the theory but they aren't the same.

zergstain
Dec 15, 2005

OddObserver posted:

C++ functors are totally different from category theory functors (which are just abstract nonsense).

I realized I actually had no idea what a functor was when I tried to read up on what an endofunctor was. I was just thinking Haskell had a similar concept. Maybe I shouldn't have, since it's not really an OO language, is it?

I don't think I'm going to dive into the category theory rabbit hole today.

xtal
Jan 9, 2011

by Fluffdaddy

zergstain posted:

I realized I actually had no idea what a functor was when I tried to read up on what an endofunctor was. I was just thinking Haskell had a similar concept. Maybe I shouldn't have, since it's not really an OO language, is it?

I don't think I'm going to dive into the category theory rabbit hole today.

In Haskell all functors are endofunctors because endofunctors are functors from a category to itself, and there's only one category

xtal fucked around with this message at 06:46 on Jun 19, 2019

The MUMPSorceress
Jan 6, 2012


^SHTPSTS

Gary’s Answer
Functional programming seems less like math and more like trying to express the Parmenides in psuedomathematical terms the more I hear people try to explain it

megalodong
Mar 11, 2008

Functors as a concept are simple.

You have a type that's polymorphic in a single argument, and you have a function 'fmap' that lets you apply another function to the value "contained" in that type, without being able to know anything about the type or change anything about it - so fmap over a list can't change the number or order of items in the list, fmap over an optional can't change the presence or lack of a value, fmap over a function can't change the input type of that function and so on.

Notably, there's no way to "put a value into" or "take a value out of" a functor. They only allow you to apply functions to the value they contain, for whatever meaning of "contain" makes sense for that particular functor.

I put "contain" in quotes because the container analogy is more limiting imo than thinking of them in terms of "structure" or "context".

sunaurus
Feb 13, 2012

Oh great, another bookah.

qntm posted:

Oh hey, let's trade dreadful JavaScript "utility functions".

Nobody can beat this guy

This is his best work, probably

dick traceroute
Feb 24, 2010

Open the pod bay doors, Hal.
Grimey Drawer

sunaurus posted:

Nobody can beat this guy

This is his best work, probably

Surely that is a war crime

megalodong
Mar 11, 2008

quote:

NASA, Microsoft, Target, IBM, Optimizely, Apple, Facebook, Airbus, Salesforce.com, and hundreds of thousands of other organizations depend on code I wrote to power their developer tools and consumer applications.

that's why he has so many packages, he uses NPM as advertising for his company where he is of course the CEO and spends all day retweeting other javascript company "founders" who also retweet each other.

sunaurus
Feb 13, 2012

Oh great, another bookah.
I've read some of his posts online where he defends what he's doing. He has this amazing skill of making you sympathize with him - he can make it look like he's just an innocent naive person who loves coding and is constantly under attack by incompetent elitist developers.

I still can't decide if he's straight up malicious and wants to have tons of unnoticed potential backdoors all over the web (who the hell can keep up with reviewing updates of hundreds of dependencies?), or if he's just sarcastically trying to show how stupid "npm culture" is.

Jazerus
May 24, 2011


sunaurus posted:

I've read some of his posts online where he defends what he's doing. He has this amazing skill of making you sympathize with him - he can make it look like he's just an innocent naive person who loves coding and is constantly under attack by incompetent elitist developers.

I still can't decide if he's straight up malicious and wants to have tons of unnoticed potential backdoors all over the web (who the hell can keep up with reviewing updates of hundreds of dependencies?), or if he's just sarcastically trying to show how stupid "npm culture" is.

one day he'll destroy it all and every node program on the planet will collapse simultaneously

NtotheTC
Dec 31, 2007


Jazerus posted:

one day he'll destroy it all and every node program on the planet will collapse simultaneously

the tragedy is it will go unnoticed for months "yeah don't worry about the build server errors it always does that"

Clanpot Shake
Aug 10, 2006
shake shake!

Jazerus posted:

one day he'll destroy it all and every node program on the planet will collapse simultaneously

That already happened

Xarn
Jun 26, 2015

sunaurus posted:

I've read some of his posts online where he defends what he's doing. He has this amazing skill of making you sympathize with him - he can make it look like he's just an innocent naive person who loves coding and is constantly under attack by incompetent elitist developers.

I still can't decide if he's straight up malicious and wants to have tons of unnoticed potential backdoors all over the web (who the hell can keep up with reviewing updates of hundreds of dependencies?), or if he's just sarcastically trying to show how stupid "npm culture" is.

My interactions with him have me convinced that he is just a narcissist and derives his self worth from the popularity. There is no way he is ironic.

Sagacity
May 2, 2003
Hopefully my epitaph will be funnier than my custom title.
I'm surprised he needed 57 commits and 4 major versions in https://github.com/jonschlinkert/isobject for this:

code:
export default function isObject(val) {
  return val != null && typeof val === 'object' && Array.isArray(val) === false;
};

MononcQc
May 29, 2007

jit bull transpile posted:

Functional programming seems less like math and more like trying to express the Parmenides in psuedomathematical terms the more I hear people try to explain it

A bunch of functional programmers wrap themselves in the holy shroud of logic, with the idea that because their tools are somewhat spiritually closer to the work of mathematicians, their own work, through some kind of mystical osmosis, is inheriting the correctness of the mathematicians that influenced the development of their tools. In practice, that specific breed of functional programmers will often write software that is really really strict in perfectly implementing the solution to a problem that was not well understood because it was more important to model it in types than actually understand the demands of people who will use it day to day. Elegance can't be bowed down to the needs of mere users, you see.

You do have the full spectrum, though, with one end being the militants unwilling to depart from theoretical purity, to the other end of pragmatists-to-a-fault who purely focus on value, won't care for their tech and "who gives a poo poo just do what the user asks no matter the cost."

It's just that by virtue of being mostly unpopular tech used by "passionate" folks, you far more often end up with a glut of users on the militant part than the overly-pragmatists you see in business-oriented software.

CPColin
Sep 9, 2003

Big ol' smile.
Last time that JavaScript guy came up, megalodong noticed an obvious bug:

megalodong posted:

code:
function isSafeInteger(n) {
  if (typeof Number.isSafeInteger === 'function') {
    return Number.isInteger(n) && (n <= Number.MAX_SAFE_INTEGER);
  }
  return Number.isSafeInteger(n);
}
uhh isn't that wrong. Shouldn't it be !== in the comparison to check if isSafeInteger is defined?

The repository is read-only now, probably so he can focus on dozens of his other repositories. Maybe we'll find another bug?

Carbon dioxide
Oct 9, 2012

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.

Nolgthorn
Jan 30, 2001

The pendulum of the mind alternates between sense and nonsense

sunaurus posted:

Nobody can beat this guy

This is his best work, probably

Yeah that's the type of bullshit I was talking about. I'm pretty sure these people have brain damage, they certainly aren't self aware enough to avoid getting hired.

Doom Mathematic
Sep 2, 2008
My favourite is his module is-odd, which is somehow twelve lines long and cheerfully accepts non-numbers like '4567', '', true, false and null, while throwing exceptions when passed such numbers as 3.5 and 1e22. It's so flaky.

And is-odd's primary consumer? is-even.

VikingofRock
Aug 24, 2008




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.

Bongo Bill
Jan 17, 2012

Ever used "flatMap"? Congratulations, you understand monads.

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.
my informal intuition for monads is that they're the most specific class that includes both "a list of T" and "a random generator for T"

JawnV6
Jul 4, 2004

So hot ...

Carbon dioxide posted:

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.
begging the metaphor

1337JiveTurkey
Feb 17, 2005

Monads are design patterns for people who are offended by the idea that they're using design patterns. Otherwise they're a tool for turning your utterly hideous problem into one only 90% as hideous except this time it's on Galois modules.

Carbon dioxide
Oct 9, 2012


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. Several people in this thread have given oneliners that explain the concept pretty well (and no not the endofunctor meme where each word takes a book to explain)

Jeb Bush 2012 posted:

my informal intuition for monads is that they're the most specific class that includes both "a list of T" and "a random generator for T"

Not entirely correct. Most importantly you don't need a random generator. All you need is a class constructor (just a standard java one). If your list implementation has a way to put stuff in the list you're already there.

A java list is a monad cuz you can put stuff in and also you can iterate over the list and generate a new list where all values are changed in some way compared to the original. (Although functional programmers would say you need a dedicated function within the list class that takes a lambda as argument before it 'counts').

An Optional is just a list of 0 or 1 elements so it's also a list so it's also a monad.

Jeb Bush 2012
Apr 4, 2007

A mathematician, like a painter or poet, is a maker of patterns. If his patterns are more permanent than theirs, it is because they are made with ideas.

Carbon dioxide posted:

Not entirely correct. Most importantly you don't need a random generator. All you need is a class constructor (just a standard java one). If your list implementation has a way to put stuff in the list you're already there.

A java list is a monad cuz you can put stuff in and also you can iterate over the list and generate a new list where all values are changed in some way compared to the original. (Although functional programmers would say you need a dedicated function within the list class that takes a lambda as argument before it 'counts').

An Optional is just a list of 0 or 1 elements so it's also a list so it's also a monad.

you're misunderstanding what I was saying. I'm not saying a monad is a random generator + a list, I'm saying it characterises the properties that a random generator for T and a list of T share in common

Adbot
ADBOT LOVES YOU

Presto
Nov 22, 2002

Keep calm and Harry on.

megalodong posted:

Functors as a concept are simple.

You have a type that's polymorphic in a single argument,
You already lost me.

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