text
stringlengths
0
2.35k
[333.46 --> 335.70] And we put on shows like this as a result of that.
[335.98 --> 340.60] So you and I, along with Matt, were basically, we did that episode.
[340.94 --> 342.72] And we learned a lot during that time.
[342.82 --> 349.88] We learned how to use what we had back then, mainly things like the empty interface and things like even generators, right?
[349.88 --> 352.86] And things like that and even sort of using reflection, right?
[353.06 --> 363.84] Because those were some of the things that really sort of enabled or facilitated the creation of functional programming and sort of style libraries and things like that or approaches to Go.
[364.22 --> 369.38] But those were also some of the gripes, right, with trying to do functional programming in Go, right?
[369.48 --> 371.02] Too much use of the empty interface.
[371.56 --> 375.04] Too much use of, you know, sort of reflection on and on, right?
[375.04 --> 380.66] And even the thinking that, well, Go is really for imperative programming, right?
[380.78 --> 382.04] The verbose style, right?
[382.06 --> 383.72] Not really suitable for functional programming.
[383.84 --> 386.36] But I think we made a pretty good case for it, right?
[386.44 --> 387.84] And listening back to the show.
[388.14 --> 395.58] But now that we have generics, right, I wanted to bring you back so we can talk about what's easier now to do with Go.
[395.68 --> 398.34] Like what was hard to do then that is easier to do now?
[398.72 --> 398.92] Yeah.
[398.92 --> 404.56] I think maybe we can do like a little bit of a recap of what FP is.
[404.56 --> 415.80] That's actually a really good introduction into, or I should maybe say segue into why generics can help and kind of answer your question too.
[416.30 --> 423.24] Where did generics unlock new areas of functional programming awesomeness in Go, if you will?
[424.24 --> 427.48] That's a technical term, awesomeness, just for all the listeners.
[428.22 --> 433.54] So I'll give it a crack to start off, you know, the recap of functional programming.
[433.54 --> 440.46] Really, you can go from a math perspective and say, you know, everything in FP is based on the function.
[441.06 --> 446.32] And from a theoretical perspective, a function takes inputs and returns outputs.
[446.58 --> 454.28] From a math perspective, right, you can chart a graph from a function, for example, just to map the inputs to the outputs.
[454.62 --> 455.22] That's what it is.
[455.24 --> 455.76] It's a mapping.
[455.76 --> 460.14] In real life, of course, our functions don't just spit something back out.
[460.28 --> 467.58] They also do some I.O. or maybe talk to a file or use a timer or send on a channel or whatever else.
[468.16 --> 477.84] And so that's really important, actually, to remember that all of this stuff that we're going to talk about in functional programming comes from the theoretical.
[477.84 --> 482.28] But when we apply it, it looks like a design pattern.
[482.52 --> 487.58] It's not like adopting a framework or if you're in school, like doing an assignment.
[487.76 --> 494.10] There's not just a right answer and a bunch of wrong answers or a right answer that everything else is just wrong.
[494.70 --> 496.18] There are some gradients here.
[496.18 --> 504.12] And we kind of saw that with the there have been talks on FP before, including one that I did way back in 2017.
[505.02 --> 507.40] There's been talks at GopherCon UK.
[507.72 --> 510.58] There's another talk from GopherCon in 2020.
[511.38 --> 515.40] And obviously, like you said, Johnny, there's there's been previous go times.
[515.44 --> 516.52] There's been blog posts.
[516.88 --> 521.90] They all attack FP from a different perspective.
[521.90 --> 522.58] Right.
[523.16 --> 527.98] And it was all possible to do some FP things before generics.
[528.32 --> 528.46] Right.
[528.52 --> 532.24] So we were able to take some of the theory and apply it even before generics.
[532.62 --> 537.16] Obviously, now generics has unlocked more stuff and now we can unlock more things.
[538.14 --> 541.76] So FP, the theory is things are based on the function.
[541.96 --> 545.30] We can transform functions by putting two together.
[545.58 --> 545.74] Right.
[545.74 --> 556.16] So if the output of one gets passed into the input of a second one, now you essentially have a transitive property where you can input to the first one and get an output from the second one.
[556.16 --> 558.10] And it all looks like one function.
[558.74 --> 559.26] Composition.
[559.56 --> 559.66] Right.
[559.98 --> 561.28] You can curry functions.
[561.28 --> 570.76] So you can think of it as partially applying a function, meaning if a function takes in three parameters, pass one of the parameters.
[570.76 --> 573.82] And now you have a function that takes two parameters.
[574.14 --> 578.28] And that first parameter that you already passed in is already baked into it.
[578.94 --> 579.08] Right.
[579.68 --> 585.84] Then you can take those tools and go to what I kind of think of as the next higher level.
[585.84 --> 586.64] Right.
[586.72 --> 594.42] So moving into the programming world, not just the math world, because all that stuff I just mentioned is basically just the math world.
[594.70 --> 601.08] In the programming world, you can have sequences of things like a list or an array or in go a slice.
[601.78 --> 609.02] So what if you could apply a function on every item in the slice without having to write a for loop?
[609.66 --> 612.50] That's called in go and a lot of other languages.
[612.74 --> 613.48] It's called a map.
[613.88 --> 613.96] Right.
[613.96 --> 618.10] You just apply the function on each element in the list.
[618.16 --> 626.06] And then you get a new list out the other end that has the outputs of that function in the same indices as the respective inputs.
[626.46 --> 630.14] So now starting to bend the mind a little bit.
[630.70 --> 635.98] What if you had a function that took in an element of a list, but then output a list itself?
[636.80 --> 636.96] Right.
[637.02 --> 641.30] Now you can do something called a flat map where you take in an element.
[641.30 --> 644.02] So you apply the function on each element.
[644.46 --> 645.60] The output is a list.
[645.60 --> 645.88] Right.
[645.88 --> 651.22] So now you're taking a bunch of lists and combining them together to make a much bigger list.
[651.94 --> 652.06] Right.
[652.10 --> 656.14] So whereas before you were just dealing with input one element, output another element.
[656.52 --> 663.48] Now you're inputting one element, outputting n elements for each element in the original list, and you end up with a much bigger list.
[663.48 --> 669.34] Now you can go even further here and you can start doing things like filtering.
[669.34 --> 677.10] So if you have a function that takes in an element in the list and returns a Boolean, you can use that to decide, can I make a new list?
[677.24 --> 681.46] And can I decide which elements from the original list are going to end up in the new list?
[681.82 --> 693.66] You can also do things like zipping, which is using a function to determine the ordering of a final list given two or three or four or five or six or ten initial lists.
[693.66 --> 694.26] Right.
[694.30 --> 696.60] So you can determine how they're interspersed together.
[697.48 --> 706.84] And I'm going to stop there, but there are a lot of other ways that we can sort of apply this very basic but powerful concept of a pure function.
[707.16 --> 709.04] Just takes in an input and returns an output.
[709.42 --> 711.26] Doesn't do IO or anything else.
[711.74 --> 722.46] So this concept of a pure function and applying that into the programming world of maps and lists and other things, other data structures like trees and so forth.
[722.46 --> 728.98] So forgive the overloading of the term list, but this list of things we can do, you know, it goes on.
[729.22 --> 729.34] Right.
[729.40 --> 731.80] And I could spend another hour and a half talking about it.
[731.84 --> 733.42] Of course, I'm not going to do that.
[733.82 --> 744.30] But the reason that I went into all that is to kind of start to talk about what could we do before and what can we do now with generics, with the addition of generics.
[744.60 --> 748.90] And really, thinking about generics, this is another branch of math, right?
[748.90 --> 752.10] We're talking about types here, statically defined types.
[752.30 --> 761.24] And not only that, but now we're able to vary the type of the input of a function or the output of a function.
[761.68 --> 764.58] We're able to vary that type based on a parameter.
[765.10 --> 766.52] It's called type parameterization.
[766.52 --> 777.60] So if we can do that, now we can write one function in Go, but we can have an infinite number of actual implementations of it because the type can change.
[777.60 --> 784.26] So if you've got a function that takes in a type parameter or a parameter of type T, where T is a type parameter.