Purifying Typescript - Timothy Clifford
I'm here today to talk to you about purifying. Typescript. But. Before we start a little intro about myself so. As. I said my name is Tim I am, an engineering, lead at BC gdv. As. Reg said I'm originally, from Australia, currently living in Berlin, at the moment, and. So the reason I decided to, to kind of give this talk is we. We use a lot of functional programming languages. At, BC, gdb but I've. Never actually used it in, conjunction with typescript, so, the. Initial thinking was is. This, a good idea how how, easy is it does it work well is, it worthwhile. So. What are we trying to get out of today so, we're trying to understand, how. To apply functional. Programming, techniques, with typescript. So. What. That means is rather, than doing a deep dive into some. Of the more. Academic. Or highly, mathematical parts. Of functional, programming I want to talk more, on practical, terms so give you some hard, examples. That you can take away and then start using tomorrow and. So how are we going to achieve this mission. Here's. Our agenda we're. Going to start with a short. Overview of functional, programming so, then we're going to talk a little bit about typescript, and why. It's useful what we should do with it then. We're going to get right, into some code and look at some. Examples. Of functional. Programming with, typescript we'll, finish off with a summary of what, works what doesn't and. Then last of all we'll talk about our next steps so if any of this is interesting to you and you want to learn more where. Can you take this next. All. Right so. Functional. Programming quick show of hands who has, attempted. Some form of functional, programming before, and. Cool. That's good and hands. Up if you're using functional, programming everyday, in your in your day job yeah. A few hands nice nice, alright. So. Let's. Get started. This. Is a little bit of a tongue-in-cheek. Diagram. But it's basically showing you that if you're coming from the, parrot of object-oriented. World you're. Used to learning all of these patterns all, of these principles. In. Functional, programming it's it's, slightly, oversimplified. Here but you're essentially, working with functions. So you're breaking everything, up into these small, reusable, functions.
So. It's a change in mindset. Functional. What, so. We talk about functional programming we, also talk about functional languages, so it's, good to make a distinction, before, we start so if, you, look on the internet if you google what is functional. Programming you will see, a million, different definitions, and it really depends on what. Type of languages, people are using what, their backgrounds, are if they're really mathematical. Backgrounds they'll have a different interpretation. As, to someone who's a more practical finger but, one of the better definitions. I have found is that, functional. Programming is building, software by composing, pure, functions. Avoiding. Shared, state mutable. Data and, side-effects so. Functions. A good changing. State and data, bad, and what. Functional languages are. Enabling. Or enforcing, the principles, of functional programming so. The. Stricter, a language. Is in. Enforcing, these principles, then typically. It's considered, more functional, or more pure so we'll talk a little bit about what pure, functional, languages are a bit, later. So. Where does functional programming come from. It. Draws. A lot from the. Lambda calculus which, has been around since the 1920s 1930s. Does. Anyone know who this young. Man with an amazing beard, is. No. So this is this is John McCarthy, and if, you do one thing after this talk you should all go and look. At who this man is and some, of the achievements. He had in his life so he's influenced, people like Steve. Jobs a. Lot. Of the Apple guys but. He's really interesting so in 1960. He, came up with a. Programming. Language called Lisp and this. Is considered the first. First. Functional, programming language. And, a lot of the languages that we see today for example closure, these. Are also considered. Building. Upon this Lisp idea so it's Lisp is actually, standing for list programming. So, since, the 1960's, we've had a plethora. Of. New. Functional. Languages we've. Also had in the last couple of years a lot of buzz around. Functional, programming in general so we've. Got a million different blog posts telling you that you, should learn one thing this year and that's functional, programming we've got. Thousands. Of different people telling you or, trying to explain to you what, what's a monad how does a monad work and. We've, got all these sort of heated debates about. Object-oriented. Is better no functional, programming is better, there's. So, there's a lot of discussion, and if, you, basically, forget, all the buzz don't.
Version You'll see we get this little, read, compile time era which is basically saying you, can't assign a number. To a string so this this is kind of the just scratching the surface of the benefits of typescript brings you the. Next one recursion, so. We. Have some. Simple symbol. Objects here we've got a customer, type which has a name and a, list of beverages at that customer lacs we've also got an array of these customers, so then we're creating, a function and we're, going to get the the name of all the, customers, who enjoy drinking coffee so we. Can see if we did this in a iterative. Way so using, loops we've. Got a lot of nesting, it's. Pretty, difficult to find out what's going on here, and. We've got a lot of cyclic, complexity, if we try and do this instead. With. Recursion so we've broken it up into these three separate. Functions, the, first one is basically. Calling filter, and maps so it's grabbing these customers, first, it's filtering, them using this is coffee drinkers function and then. It's mapping them just to get the customer name so, the. Benefits, with this is for. Me reading this it's a lock Sarah I, can test each of those individually. And it's. Just nice occurred in general if, we look at the. Next one immutability. So. Typescript. Has some inbuilt. Immutable. Objects being the read-only. So. We can make our customer read-only so. This this type, will then make, the name and the beverages, both. Read-only. And, then we can look at the read-only, array, and, this also makes a or. Provides us with a read-only array, which we can't once we've created that array we, can't add any more objects to it it's, basically, locked and immutable so we, can see down the bottom here if we try and grab. One. Of our customers, and change his, name to Bob that's. Not going to happen that's that's being prevented, and if we try and push a new customer, into this array we're.
Also Getting prevented, there, all. Right our next one, function. Composition, so. Function. Composition, is the. Combination. Of two. Or more functions to provide, you with a new function so if. We look at these three, in. This example we're working with a bank, balance and we want to basically. Transform, that so we're adding some. Celery we're subtracting some, fees and we're subtracting some, bills so if, we did this in a, kind, of traditional, way we can have. Our update, bank balance function which takes a balance. Passes. It to each of these functions individually it gets back the result and assigns it so this is this is doing some sort of bad. No-no, things in terms of functional programming it's it's sharing state it's mutating, objects. And. It's, just. Generally. Messy so, we. Can also do this in. Here. In subtract, bills we can get. Rid of this state mutation. We. Can get rid of this State mutation, by nesting. Our functions. But. I think everyone would agree here that that's that's, not readable, code it's very difficult to understand what exactly is going you know going on, in this code so instead, we. Are using a. Library. Here we're using a functional library it's called lambda, if, you look at our imports, so. What this gives us is a pipe, function, so we can pipe our. Data, through. These three separate. Functions. So we basically are given. This update, bank balance composed, and that. Accepts. Our data passes. It one by one through these functions, and then returns. Us the results so there's no sharing, of state we, get this nice smooth data pipeline, that we can then work with so if, I wanted to for. Example add, another, function into that it would be super, easy and super clean. All. Right the next one curring so as, promised. We. Have a. Function. Here it's still working with bank balances, so, we have three. Arguments. The, current balance the celery and the fees and. What. We want to do is curry this function so, if we look again we're using Ram de, so. What does carrying a let us do it's transforming. A function, with. Many arguments into a sequence of functions each with one argument so, first. Off what do we do we curry our, original, function here that gives. Us back a curried, function and so, this expects, our arguments, to be passed in one at a time and each step, of the process is going to give us back another. Function which is expecting, the, remaining, arguments so if we look at the. First one update bank-balance curried, we're giving it the the.
Is A domain where a more a more pure language and with higher kind of types and other other things deliver. Some benefits so I don't know yeah I would say that this is if you're if you've never written sort of functional. Works. With functional languages before this is kind of a good good. Thing to put your toe in the water see how it is because it's not as strict as something like Haskell, or. Alum. For example so you can kind, of play around with these concepts, without getting, kind of slapped in the back of the head when you do something wrong fantastic. Folks what do you think of his talk. Like, thank you so much.