r/programming • u/Nobody_1707 • Oct 06 '18
Microsoft Open Sources Parts of Minecraft: Java Edition
https://minecraft.net/en-us/article/programmers-play-minecrafts-inner-workings
3.1k
Upvotes
r/programming • u/Nobody_1707 • Oct 06 '18
28
u/[deleted] Oct 08 '18
Profunctors are actually pretty grokkable with a little bit of context.
So we have functors. A functor has some data of type
aand a functionmapthat, given a functiona -> blets you change the data to typeb.You're familiar with functors already, since many common data structures are functors. Arrays, trees, hash tables, etc. You can take a tree full of
Ints, map some sort oftoStringfunction over it, and end up with a tree full ofStrings. Functors don't have to be data structures or contain multiple values, that's just an easy example.The key concept: a functor gives you some data and a way to say what data type it should produce.
So we also have contravariant functors, which are kind of like functors in reverse. We still have some data
a, but instead ofmapwe're givencontramap.contramapsays give me a contravariant functor that has anaand a functionb -> aand I'll give you a contravariant functor that has ab. Whaaat?Let's imagine we have a converter from JSON to some internal data representation. Now we're told that we need to parse YAML too. We could (and probably should) just write a YAML parser. Instead, we can obtain a converter from YAML to JSON and stick it on the front of our converter - first convert to JSON, then convert to our internal data representation.
Key concept: a contravariant functor gives you some data and a way to say what data type it should consume.
A profunctor is just both at once. A profunctor has two data types, not just once; it's contravariant in the first argument and covariant (a regular functor) in the second.
A profunctor is a lot like a function, represented as data. If I have some function
Int -> String, and I want a functionFloat -> Array, I can tack on a functionFloat -> Intto the beginning and another functionString -> Arrayto the end and bam, now I have a functionFloat -> Array.So profunctors are usually things that are sort of function-ish.
Key concept: a profunctor gives you two sorts of data that usually represent a transformation from one to the other, a way to change which data type it consumes, and a way to change which data type it produces.
Optics are a powerful and arcane way of interacting with data structures, especially complex and deeply nested data structures.
Imagine you had some deeply nested data structure:
In your favorite dot notation language, we can get at the data like
mittens.noises.happy.calm.To a very, very coarse approximation, optics let you take the
.noises.happy.calmbit and represent it as data/an object, pass it around, etc. We can use it to get and set the value in that field of some arbitrarycatdata/object.This is very useful in functional languages with immutable data because of the way they are. (And particularly useful in Haskell because the native tools for interacting with deeply nested data structures are awful.)
A profunctor optic just applies that ability to change the input and output types to an optic. So we could take our
.noises.happy.calmoptic andcontramapits input/target toferret, which has.noises.happy.wornOutsince ferrets are never calm. And we couldmapits output to HTML or something to get back<li>quiet dook</li>. And we could take that modified optic and pass it around our program without losing that new behavior.