Functional Programming in an Imperative World. Maybe

Video in TIB AV-Portal: Functional Programming in an Imperative World. Maybe

Formal Metadata

Functional Programming in an Imperative World. Maybe
Title of Series
Part Number
Number of Parts
CC Attribution - ShareAlike 3.0 Unported:
You are free to use, adapt and copy, distribute and transmit the work or content in adapted or unchanged form for any legal and non-commercial purpose as long as the work is attributed to the author in the manner specified by the author or licensor and the work or content is shared also in adapted form only under the conditions of this license.
Release Date

Content Metadata

Subject Area
The pillars of FP Let’s start by looking at the core concepts that differentiate FP from the OO / imperative style most programmers are familiar with. Along the way I’ll introduce you to: Immutable data structures. Having data structures that don’t change makes your code safer, especially when dealing with concurrency and parallelism, but they require you to approach solutions in a different way than you would with mutable data. “Pure” functions. Pure, or idempotent, functions do not mutate state or cause other kinds of side effects. As a result, you are guaranteed that every time you call a function with the same parameters, you will always get the same value. Recursion: While recursion is something most of us know about, it’s not something we tend to use often in imperative programming, and with good reason. Nonetheless, it’s a worth knowing about it’s various forms. Function composition. When you have pure functions that handle only one task, you can build larger, more complex and more beneficial programs by composing functions together to form new functions. First class functions: passing around functions as parameters and return values, just like any other object. The holy trinity: map, reduce, filter. These three functions are the work horses of FP, helping us manipulate and transform data quickly and elegantly. FP in python Now, let’s take a look at how we can or cannot apply these concepts in python. While most data structures in python are mutable, tuples are a built in immutable data structure that we have at our disposal. We’ll see that tuples have a solid place in python, but they’re not as easy to work with as we might like. Recursion isn’t really well developed in python (on purpose) so let’s take a look at it’s pitfalls and how to avoid them. Function composition is something you probably already do some in python and perhaps don’t even know it. The trinity: Filter is easy, we just call it “list comprehension” Reduce. Let’s try to get beyond flattening nested lists and doing tricks with math. Map. You probably don’t use this enough in python so let’s see if we can change that. FP is great! Maybe. Now that we’ve seen how FP can be used, we really need to decide if it should be used. Python is not a functional programming language, despite the tools it has. We’ve talked about some of the technical drawbacks to these tools, but we also need to decide if working in an FP paradigm is right for our work environment. We’ll look at some examples of where running into FP can be jarring and talk about the additional cognitive load on co-workers who aren’t used to seeing these tools in place.
Email Functional programming Presentation of a group System call Code View (database) Multiplication sign Set (mathematics) Database Insertion loss Parameter (computer programming) Open set Mereology Computer programming Software bug Mathematics Different (Kate Ryan album) Core dump Query language Functional programming Social class Programming language Programming paradigm Email Touchscreen Concurrency (computer science) Moment (mathematics) Sound effect Bit Perturbation theory Variable (mathematics) Entire function Connected space Type theory Data model Order (biology) Right angle Quicksort Resultant Recursion Row (database) Laptop Point (geometry) Slide rule Chemical equation Real number Letterpress printing Control flow Parallel computing Twitter Object-oriented programming Energy level Boundary value problem Computer worm Maize Functional programming Code Database Total S.A. Line (geometry) Cursor (computers) Cartesian coordinate system Computer programming System call Pell's equation Uniform resource locator Word Loop (music) Computer animation Query language Logic Function (mathematics) Blog Object (grammar)
Polymorphism (materials science) Functional programming Complex (psychology) Equals sign Multiplication sign Source code Set (mathematics) Database Solid geometry Mereology Total S.A. Mathematics Semiconductor memory Different (Kate Ryan album) Object (grammar) Query language Acoustic coupler Functional programming Descriptive statistics Personal identification number Source code Programming language Wrapper (data mining) Electronic mailing list Bit Instance (computer science) Entire function Annulus (mathematics) Type theory Data stream Order (biology) Natural number Hill differential equation Pattern language Summierbarkeit Right angle Quicksort Cycle (graph theory) Data structure Recursion Row (database) Trail Link (knot theory) Sequel 3 (number) Cursor (computers) Product (business) Object-oriented programming Integer Data structure Standard deviation Key (cryptography) Autocovariance Code Database Limit (category theory) Cursor (computers) System call Word Pointer (computer programming) Computer animation Personal digital assistant Function (mathematics) Pressure
Point (geometry) Functional programming Trail Context awareness Ising-Modell State of matter Multiplication sign Data dictionary Stack (abstract data type) Product (business) Revision control Programmschleife Type theory Single-precision floating-point format Data structure Functional programming Recursion Mathematical optimization Programming language Moment (mathematics) Electronic mailing list Code Line (geometry) Statute System call Frame problem Pointer (computer programming) Loop (music) Computer animation Personal digital assistant Right angle Summierbarkeit Quicksort Data structure Programmschleife Tuple Resultant Local ring Recursion
Functional programming Group action Manufacturing execution system State of matter Code Multiplication sign Source code Set (mathematics) Function (mathematics) Parameter (computer programming) Stack (abstract data type) Disk read-and-write head Semantics (computer science) Optical disc drive Mathematics Semiconductor memory Different (Kate Ryan album) Computer configuration Functional programming Recursion Error message Modulo (jargon) Programming language Email Channel capacity Mapping Moment (mathematics) Interior (topology) Electronic mailing list Menu (computing) Bit Variable (mathematics) Flow separation Entire function Demoscene Type theory Arithmetic mean Exterior algebra Right angle Summierbarkeit Quicksort Figurate number Arithmetic progression Resultant Sinc function Recursion Point (geometry) Trail Transformation (genetics) Maxima and minima Mass Field (computer science) Number Revision control Programmschleife Energy level Computer worm Integer Data structure Normal (geometry) Mathematical optimization Initial value problem Default (computer science) Execution unit Surface Lemma (mathematics) Electronic program guide Code Planning Line (geometry) Binary file Limit (category theory) System call Word Loop (music) Computer animation Personal digital assistant Normed vector space Iteration Buffer overflow
Functional programming Group action Building Code Multiplication sign Set (mathematics) Function (mathematics) Parameter (computer programming) Mereology Computer programming Mathematics Strategy game Different (Kate Ryan album) Core dump Functional programming Office suite Recursion Lambda calculus Programming language Curve Programming paradigm Link (knot theory) Mapping Electronic mailing list Database transaction Bit Sequence Type theory Website Summierbarkeit Right angle Resultant Electronic mailing list Goodness of fit Object-oriented programming String (computer science) Touch typing Integer Data structure Initial value problem Default (computer science) Code Counting System call Performance appraisal Cache (computing) Causality Maize Personal digital assistant Iteration Document Type Definition Square number Library (computing)
and huh and 0
and the issue of
and a ready
thinks I'm I appreciate it so my name is Derek Bell and engineer MIT e-mail marketing of that's me on I get held in Twitter and actually go out to make it come if view of to the URL to the general con 2017 this is my entire presentation committee is being done in in in Jupiter notebook and I've uploaded it to get help so if you can't see this slide you can pull it up in the get up and follow along and it will run the coding gain of the gem look at at all cloning rendered spike and 3 of put your hands heavy people showed up hoping I was going to explain known moments stars I know the soldiers breeders so you know about about really and anyway so what I wanna do is talk a little bit about functional programming and fragile parameters has become sort of the hot new buzzword right in the coming from an object-oriented language like Python sometimes we might see functional programming something mysterious of our cannot break that level bit 14 introduce you to some of the tools that help functional programming you know do its job and recognize that most of these tools exist in Python and not always To the best of there there are always the best tool to use but they're all there and so I just wanna sort sort of give everybody just as an overview of what functional programming is other that's why would we use functional programming why would we stop talking about object-oriented programming and look at this new style of programming and will function programming is very expressive as we will see the Son the code examples you can do a lot with very little bit of code but it can you can just very much you can shrink a codebase pretty considerably in the right places on it to be very efficient things run really quickly when your language and actively supports functional programming paradigm and we'll see some places in in Python regret that where does that very well so this is where it doesn't quite so well or it can be safer hyphenated dental purposes sort of the emphasize the fact that it's still not safe like there's still problems but it's still a does it can be safer can overcome a lot of problems along the bugs that we see in day-to-day object-oriented programming end up it's easier to work with concurrent parallel programming we will actually directly begin to that but naturally the saw the reason that frontal programming has become so popular in the last several years as we start getting a core processes I can go faster and faster instead using more and more quarters on being able to effectively take up our code and spread across a couple different cost is is the next step in making our our applications run faster functional programming can help lead into that of but what is functional programming but it's really just the style of programming that you know that utilizes pure functions which are functions that don't have side effects of any uses those functions uh to transform data so fungal permit actually thinks about data in a very different way then of object-oriented programming auditory programming we hide our data way and we're very the particular about how we use and and whatever and who can access and that sort of thing functional programming is a more open data models like we know like there's no need to hide your data way let's you know take it and work on it in a more mathematical sense that's where we have the we try to have pure functions things that don't have the side effects because they don't really they don't always make sense to have side effects like in the middle of the code on the on the Isle boundaries cost you have to have a side effects at right to a database print to screen read from a database that type of thing but in the middle of the code there's a lot of places where we do side effects were we don't necessarily have to and functional programming is a way of thinking that culture reduce those because a lot of time so side effects are places where you have blogs so what I will do that we start looking through some of the tools that help you get to functional parameters like functional programming is not the it's like saying you know the word explain object-oriented programming you might start with OK this is how you build an object but an object in building an object class is not the totality of object-oriented programming in the same way there's not 1 part of functional programming it's really just a set of tools and the mind set on how you use them so all with applets are looking at the 2 the first one is a higher order functions and if you've never come across this phrase before a lot of this stuff you guys and we may have seen you just may not realize that it is sort of like a hand in hand with with more functional thinking but on higher order functions just means of functions give treated like 1st class citizens but they can be passed as they can be passed to other functions as parameters that can be returned from other functions as values and they can be stored in variables so you know they're just like any other piece of data in your application so let's take a coupled with a couple of examples so here it is something that sort of real world which we do this kind of thing quite a bit of work we're in and we need a pet we we will make a function that knows how to talk to our database knows how to make the initialization the database or maybe it's just point connection off a pool or something or other but at the end of the day we have 1 function that knows how to grab the cursor to our database then we have all these other types of functions that need that person so we may have inserts that will return a row t right but that's gonna be different than using a query that will return you an entire result set right so instead of trying to make some in this so trying to make to each individual we either make into each individual query like make its own connection to the database or make some function that connects the database in a way that's versatile enough to like return a reliably in this situation the return an entire rows and this results in this other what we do is we make of it a function that knows how to connect the database grabs the cursor and make others functions that use their cursor and beach individual function all it needs to know is how to use the cursor it doesn't need to know how to connect to the database so all we do is we have something along lines call database which takes a function somewhere in there makes the cursor and then as its return value it calls the function that we pass the in giving it the cursor right so uh 1 example might be where you query database we the query database of function takes a person and then it knows how to loop over the results said a red results and pass it back or whatever it needs to be it it just depends on your business logic but the idea is that were passing when we do this the we're going like this next line down here will take the results were would get them by calling directly to the call database functions and passing it in the query database functions right so we can see here call database is doing its
thing to initialize the database that it gets a cursor in it passes a cursor into whatever function we call it with which in this case is query database so it's querying the database with our courses right but we also need to do something slightly different we can do something like returning a new of the new row value right so we we can reuse the same called database function is all it does is 1 thing work on the on the cursor and it just passes a cursory and whenever we have call whenever we pass it a function he calls a function passes in the culture right so that's something that that's a place where we use quite often by passing function into another function and you can also returned functions so this is a little more contrived example this is probably not something that would actually happen quite a bit but if for instance let's say you a you had some data coming in somewhere and you had it could potentially coming from several different sources right it could in from a source that needs to read off a story on a resource the needs read out of a single database you can create these functions that I do whatever they need to do like you have S 3 function that knows how to prolog history and retrieve the data or another function that like we just saw the calls of the database retrieves data from the database which you need to dynamically choose which of those functions based on the end of the source covariance what's this is this essentially become like like a performance polymorphism but it's a a pressure for example what we do is we have a source a comes in this case S story we ask for a new function by calling out to this give me a function in a function that takes the source and says OK what their story and will return this complex history function to you right it doesn't call it doesn't do anything with it it's just passing the function back to us and then it's up to us which we get this new function except us to actually colored with whatever we need to call it right so we get this and you know we essentially saying here's et where we're coming from S 3 I do is give me some function that I know how to handle that will know how to handle a story or give me some function that will know how to handle sequel and because are actually was passing back or functions and we don't actually have to even do this were reassigned to a value and then call you can just call it straight away this way right so we're calling into an sorry going to give me a function as in our source and whatever comes back we immediately called it just like we would any other function possess solid it's it's packages patterns by function now if anyone who's and like I said you may have seen this sort of stuff especially this in this type of thing we use a lot when written in JavaScript and if you have to do that but we actually use it a lot In decorators the who has worked with created looked at decorators a lot of people you hole and the entire room right so so that that's all a decorator really you're passing in function to add decorator functions in this case decorated that has a nested function which we could we call wrapper for the and then it does whatever kind of work you need to do before calling a function where work immediately after calling a function and so yes this should be pretty straightforward everybody who's ever used decorators before and that that's all this so without out higher order functions I think decorators so we have to be able to access functions around in so you see it's not just usable in functional programming we use it all the time in object-oriented programming in certain languages in that sort keys like functional programming is a really industry it's really just a standard set of tools that she is in a slightly different way I the next tools are only getting is our mutable data mutable data structures that this is something that we don't use as much in Python because mutable data is on the bread and butter of object-oriented programming but for immutable data it's a structure that never changes itself so like you have a list you don't actually add something new to the list and instead the you list can be changed you sorry about that I use a source sentence stone and look in a little bit about how you overcome because that seemed like a limitation like you create a list you can actually you can delete from at what is it how do you make any changes of words and you have to think about this in a completely different way because whenever you make some update were used to pass in a list and something to it in that list back with mutable data you don't do that you pass in a list if you add something to it you get a brand new list back it's that's a completely different data structure of saying it is the same idea that if you take something off the list in the past in a list of function if take something off the list what you get back a completely different list that's that's a very safe way of handling things that that means that this is change out from under you right and the balcony and we all done this track product again about where because we pass off a list we don't realize the Weber function were passing into is mutating that with us before and back to us well with the mutable data structures you don't have to worry about that you can possibly list they can do whatever they want to do with it your list never changes their list it's adjusted to a new list nice we don't do this a lot because historically they have been very inefficient which have to do is take that list it's sitting in memory and operate the way language has to then copy the entire list to a new part of memory you essentially have to list it's very slow to do with their been new data structures called persistent data structures that make as much more efficient than if you download this I got a link to persistent data structures to the description in Wikipedia but a dive into them but they're much more efficient at this sort of thing so it let you use an immutable data structure without paying the additional cost of like all that copying over in memory then for not familiar with the problem of mutable data structures so what happened and probably almost all of you have seen something like this this is like almost every language of ever looked at has this sort of warning of this kind of thing where you create a list and then the you create a 2nd list you point a cycle is the 1st list if any change at 1st list you've automatically change that 2nd catalyst right so that's not a safe way of making sure your data stream your your list doesn't change you can do that typically but you can see here this 1 was to that they are the same thing they're basically the same list but what we have are 2 pointers to the same piece of memory and and this can be a problem here and that sort of situation you can also create some really interesting problems so if you not thinking about it and so this is just a quick function the goes through takes a list of integers and then sums up that strike you should read the result so if you pass list 1 2 3 3 the summation of that is 6 but somewhere in there you accidentally sort pin to your list what you're going get so really
weird results right I you're in the middle of working your way through a list in a way that we do every day just a for loop but somehow we appended to that list in the middle that live in our this is going to continue to grow added this if clause in there because if I don't catch for a list of a certain size this will grow into the to and so that's 1 of the things that we end up having to track today on sometimes when we work with mutable data but you work with the mutable data all of these situations disappear so right here we do the same thing that we did above the list right we're creating a tuple work radialis recruiting a tuple adding some items to it and then pretty another another tuple pointer to the same tuple product so that 2 border to the same tuple just like we did with the list above but now when update the 1 to it does not update the 2nd 1 because in Python whenever you try to update whenever you try to concatenate of an item into a tuple and you get back an entirely new data structure right so before we do the concatenation we
look at these 2 people want people to they are the same after we do the concatenation they're no longer the same because Python is created a brand new data structure for us to safe way to do it but it's slow because these were not created as persistent data structures i and you can do it on typically if we want an immutable data structure we go to a tuple or you can do something along the same lines with pretty much any with listed dictionaries you can use a deep copy instead of actually just making 2 point in Jamaica deep copy of the of the list that way when you currently change 1 it does not change the other because you start off with 2 different list but again it's slow because has to take that list copied over and remembering so those some the problems that we can see with the mutable data we don't we solve with more mutable data structures then but what live good it mutable data structures means we have to start thinking about things slightly differently often like we don't want to do a for loop on an immutable list because it doesn't look quite a the as well well interleukin impact on on a lot of uncial programming languages don't just pull the loop out of there because if you got maintaining your list you don't necessarily need loops you can't come up with other ways of loopy of getting from everything in the list and 1 of those ways is recursion the big ways recursion if you've never worked recursion and most of you probably Ising seen it by recursion is when you call a function from within itself I it takes aplikacijo takes the place of loops in many programming languages and we see that here just a moment how we can use recursion similar the problem of recursion is that every time you make a call back to the function you add a frame to the call stack and call 2nd grown grown grow in languages that embrace recursion that they often use the this single tail call optimization which lets you more efficiently than me do recursion without adding to the call stack but Python does I have Telco optimization of Guido has if you've ever looked at is sort of famously come out against the local optimization in Python because it really does it hide your stack-trace what Telco optimization does is it instead of building up every stack time you call back if you if you do your recursive call properly it just overwrites the existing frame under statute you don't build the sauteed reusing the same frame it's much more memory efficient but you do lose your culture if something happens you know 5 or 6 calls B you don't know exactly where it happened you don't have all the context for it so we probably will never get Telco optimizations but regardless acidic with with recursion so this is a recursive version of the summation list that we get about since about about were getting at the list and looking for the list here origins with any of the list and then use of looking for each item we send the list back into the function itself so the function in this case has to pick a list but it also has to take the summation because we all are recursively calling back into itself it's harder to keep track of the state you have to pass this you typically depend a the state back into the function along with whatever like well in this case for passing the list in we also are passing in the cumulative sum their ways depending on whatever the mere using there'll getting through the ways to do this without having the past the state back in but in this case we are for recursive functions you almost always have to have a base case and the base case is the point at this tells the recursive function stop recursing were done just return a value don't call back into the function again so in this case we're done when the list is empty when there's no when when it's just an empty list we have summed up everything in the list just returned summation if that's not the case what we do is we pop the last item on the list added to the current some as the new sound return everything back into the loop again right so that her son last function in here calling it were calling with list 1 2 3 we get a
result 6 now we still have a saying issue that we had with the iterative approach which is if this list gets changed while we're doing summation it's going to you know mass of our critical success not fix that problem that a mutable data structure problem on but this if you were to try to solve the over the you try to implement this in a more functional language this is the kind of thing you would do you would loop through it using a recursive call rather than an actual fort with no 1 of the problems with recursion inside of Python is E if you Canada too big to try to recurse too many times in a recursion here now in language is you don't have something called recursion there python has built in and a limit on the number 4 and you can put the stack which have by default is a thousand all you can up that if you need to but in it sooner or later you're going to run out of memory if you before this is big enough if you recurse deep enough you're gonna run out of memory because you know the language has to keep track of everything on the stack and that's where you get a stack overflow so in most other languages if they don't limit your recursion depth like Python does they notice recover and keep going and going to do you run out memory stack overflow so without tail call optimization recursion is actually really dangerous to do so you don't see it a lot but I've never seen it in the wild in Python and there's really good reason so if you don't know how the pure euro recursions gonna go come up with a different solution you can always use an iterative solution is there's there's no problem you can solve recursion that you can solve with just a loop in some capacity so in Python deathly tend to favor loops of recursion I don't like that and judges the ways that recursion get used and that's how we can get around some the problems of mutable data in and the point that out well that essentially what you're doing is you take that list and sending a new version of the list back through recursive loop it's time in this case we're popping the that last item off but in other situations you a of a lot of function languages have this concept of the head and the tail of the list the head of the list is the 1st and entails everything else lot recursive calls will use the head and the tailback along so that's sort of essentially what we're doing here that in so yeah those or if you've looked into functional programming to sort a surface level those are probably the minor players that we've gone over so far the big story the holy trinity of functional programming are matched filter and reduce the In this is we're more functional style really shines out well and in progress in Python if you've never used any these in Python if he did nothing else out of this talk I hope you walk away with a sense of I need to check in the MAP filter reduce and figure out how to use them because they make your code what you get used to how they work there the beautiful of so a lot of times so to really use these tools metal reduced realize hands in abusing Landis lenders or anonymous functions of and that really just means it's a function that doesn't mean that we don't name right insane death you know called the the we just have a we well this is the this is how it all falls out in Python you use you were aligned that you pass it whatever variables you need in this case x and y and then there's a colon the seperates variables from the body right a use for typically one-liners if you're lender gets much more complex than you can fit on 1 line you're probably better off just making a named function and going to the to the war out there but the land is a really good for these in line 1 OS were doesn't even make sense to make an entire function just add 2 numbers together right distilled into a land that In this case were assigning that landed to a variable name song we call some just like we would any other function and it sums up whatever we give my you don't usually use is this particular way that you do use them quite a bit in that filtering rebates so let's start looking at some at those very tools filter does exactly what you think it would be and it is you pass in a filtering function which will remain in the collection to operate on the filter of the actual filter function of takes whatever filtering function you passing and and applies it to every item in the list 1 at a time to take the 1st item right of Figure filtering function the filtering function needs to evaluate the either true or false so if it evaluates to true that filter will then take that item on the list and added to renew it evaluates to false that item does not get added to your newest technically that filtering reduce all return iterators may have to they would return a list but now the return iterators which is much better that way you can kind of evaluate that as you want it doesn't build the entire list of 1 time if the yields back 1 item at a time you can live through it however you need to but essentially if you take something like that list if we look at our filter right here we're going to create a new list iterator using filter we Pasadena lander it's basically just saying I'm looking to whether or not the value that we're looking at is it even or odd right if it's even then we assume modulo 2 is equal to 0 and then it gets added into the iterator modulo 2 doesn't go 0 it doesn't get added to the iterator or passing it list 1 as the collection everyone we want to iterate over and so it's filtered out all of the whole word we assume states filtered out all the non other on the integers filtered in the even integers animal semantics but in any case that were filtered out you can pass a delay and like this or you can do something much more complex as long as it the field as long as the filtering function in the evaluated true false so filter knows whether or not they had that value into the newest same thing is true for map map takes a mapping function in a collection operate on and then applies that function to every item in the collection 1 at a time it does not filter out any items is not you know if you pass in a 10 item list you will get back at out through you should get back to my list so but what it does is it says you know I have this whole list of users and I wanna transform them in this particular way you can use mapping give it whatever trans whatever of a function that's going to transform your list of users and then give the list and it will go through and do the transformation for you so you it's a simpler way of you just after called all brightly normally if you're not using MAP you might just for every item in this list call at this function manually map visible you mappers action a lot more efficient at it it will run a lot faster so map is if you have 1 of source simple situations where you just need to pass every item in a list 1 at a time to a function map is a really good alternative to using for in same kind of thing here recalling that we were using list 1 only recalling there were passing at this land this anonymous function at all it does is take whatever I don't I give it and multiplied by 3 and then put it back in the newest right so they're
vocalist 1 we multiply that by 2 every item and thereby to get back a new list notice even though we did of of filtering on the fertile list 1 earlier within actually in a just list 1 all we got a brand new list back again the because this is happening because these functions were built Mathilda and reduce were built to work this way they're much more efficient then if we tried to reproduce them ourselves so this is actually a safe and efficient way to go through a list of items and do something with it rather than just sort of manually would be through at risk and the possibility of adjusting a list In line or anything like that so this is a really safe way of doing this type of thing only and the last of these is reduced produces a little bit different than the other 2 of them it so why reduced as but it starts off saying it takes a reducing function right it takes a collection to reduce with reducing functions a little bit different it doesn't just act on 1 item in the list it acts on 2 items in the list at the time you can actually send an optional initial initial value so the reducing function instead of acting on the 1st and 2nd item it'll start off acting on the initial value that you send it and then the 1st item and the way it works is this you're reducing function takes these 2 items does something to it so that the output is 1 item of the same type that comes in and not were not strictly using as you know like integer versus strain vs whatever but essentially what's gonna happen is it will take a reducing function the reducing functional take 2 values do something with it spit back out a new value that new value then becomes the 1st the parameter In that when it when it calls a reducing function again and the next item in the list becomes the 2nd parameter and so let's say take a quick look here I this reduces the British was the 1 that for whatever reasons hottest Fermi-Dirac header out exactly what it's doing math and filter pretty straightforward from irreducible ordered of comes with a couple of examples that might help solidify but in any case this first one is the sort of standard reducing examples and we were calling reduce the synonymous function that just takes 2 variables and adds them together right so just like we did above and were looking at plan is initially were just taking x and y and adding them together returning that value what this is gonna do those is gonna start with 1 and 2 of the list that's gonna be the 1st X and Y so and then together limited free free is then passed back even as the x the next item in the list which is also 3 gets passed in his wife those to get added together just to get 6 6 gets passed back in as the x and the next item on the list is for its customers y and at the end of the list you end up with your son of 10 right only now this is the same kind of thing that we saw above right we sauntered we saw an iterative approach in recursive approach to taking a list of numbers and adding them all together this is using reduce shortest do the same thing and it has it's so much better if this is the kind of thing you need to do you need to start looking at reduced for 1 thing before we we tried that use the recursive approach and we didn't we added a thousand do the set right where had that recursive error here because the way it built behind the scenes it adds the 1st thousand numbers altogether without going through the stack knows what it's doing but it also doesn't really fast as the 1st thousand numbers so 1st 10 thousand members its 1st 100 thousand 1 numbers minutes superspeed is incredibly efficient this is probably much faster than we could manually it is much faster because when we're doing the recursive approach above I started off and had to wait for a moment for it to flow through this fact this is going through it just lightning-fast these things were made to do this type of work very efficient on the back you really need to start looking at the map filter reduce as viable options for whatever kind of like where everything you can find you can but then it's probably a good place to put them because they really do make things so much easier in so much faster um although the 1 more quick example there's a couple examples you people then we get on to what I like this example I essentially what this is going to do is it's going to go through encounter go sum up
all of the eighties in all 3 of these strange right so this is we imagine this is a DNA sequences that 3 days here the next sequence sequences to analyze sequences none the the reducing function here is taking a in x In this case but we're starting with initial value 0 so the 1st time he goes through that a is 0 and the x is of whatever the next item is in this particular case X is that particular string with reducing function does is it takes that string this account of the EIS adds that count back into whatever the a whatever the a value it is income sums it up that way and so this is the 2nd problem you're trying to solve the above more less were the welfare which 1 house but I'm in case this is a really efficient way of like you can give this huge the US list of DNA strands and it will go through and tell you all these markers in it and just by using a reduced so you can also mean that mimic mapping filter inside Python I'm using this subregions we list of integers something I'm sure everyone of us done they in and day out and you can for a land inside a list comprehension of lenders who the can pull it out you can also so the filtering uh function more less inside a full insider and the list comprehension and doing so we were told that already built and applied in the Python that we use day in and day out but we don't think of them as being functional programming perhaps but they really are the and at the core they do this and other thing that we do and functional programming so that leads us to the question of what should we bring frontal parameter code and because it is really different paradigm of what I think of course you ship function like I think I've shown a couple different places functional programming can be less verbose it can be much more efficient and it can help you think the problems in a way that maybe 1 of you were able to think through before so maybe solutions become more obvious when using a different set of tools but there are some very real drawbacks as we saw also seen of working with me immutable data can be slower especially in languages like Python again we have there's a persistent data structure library that's in Python male on building I don't know anything about it I haven't had a chance to look at it it turns out that it works well then I will start trying to use of more in my code because it's much safer but he needed but the mutable data is still slow and pipeline as it's at the built-in convergence and recursion can both the Sagri quickly as we've seen so recursion is not a good solution in most cases and functional code looks sort of weird 11 co-worker who loves so functional code into our Python and every time I hit it even though I like looking at functional code every time I hit it it's a little bit of the speed but when I 1st started hitting and it was a brick wall so that like totally slowed me down and stop me but now that I understand what's going on with it I can scan through it more easily and understand it so if you're going to bring functional programming in your code probably something you need to discuss retain your management and if you're working on gene and the site is attained high is this something we really 1 approach because if you if you just start falling in in their your teammates don't know anything about it they're gonna quite a learning curve but you can help them learn and that's kind of fun so I think so etc. speed through of functional programming in Python compare that they carry around then 1 any questions have few I think you for the talk so you mentioned that you know in some situations maybe you to bring them from her going to code but but was honoring it sounds like you are saying a can be possible to does have a bit sprinkled in here and there is there is a kind of do you recommend it you know and having it it basically be D paradigm like choosing 1 or the other DTD can succeed if it's half and half a sprinkle the he what definitely in Python it's better to work with like a Python what support all these tools and and a whole slew of others there's the default tools library by phone which is really great for doing things efficiently on item is at heart an object-oriented language and so making a full transaction transition to a functional paradigm probably isn't really gonna work out there are a lot of good places where you can put it you might think you can I 1 thing I have been a gentle touch on it is on the idea of data pipelines to so what the things you can do that is composed functions which is sort of like if you ever done any Unix programming were you about pipe output from 1 function call to another or if you've done like maybe node working promises were you can then 1 function the result of 1 function call into that in the next best opposing functions and you can build a really great data pipelines doing that and you can do that and type in Python I found really great with like data science can things so there are a lot of places where you can use a more functional mind-set but it's probably not the paradigm run run with completely inside you might be able to that you can let me know because I wanna that works out but I know I know I've done everything it could be that I entirely agree tactic you come emissions emulating our map with as a list of region then I I was 1 the other other considerations of Wilson's reading the code without the familiar with functional programming are the technical differences differences between the 2 it how should you decide which 1 to use uh I don't know the technical differences I was there there or I'm just going to assume that there are some I know the the the difference that you're going to run up against would be efficiency and speed and my guess and I again this is just a guess but my guess is now maps are probably the not only use lower the doing comprehension I can I can almost safely say that they may be a little more efficient little faster because of the fog to those that have been brought in mapid filter silk or part of the I find that reduce has to be brought has to be imported from that I will I will I go out on a limb and say it's a coin toss probably more or less like a little more efficiency with on the other it you're probably not going on is a huge difference if I'm wrong about that actually know 11 has incorporating functional programming in your code changed your caching strategies and all of a and B is not change our caching strategies I can see how would we just we haven't had that place shit really and there's there's only 1 in the 2 other people at my office who we start implementing functional programming so we're still sort of like can I minus the like 9 as a country we're building up you know like it is something that some of us know about and like earlier this is this is the only place to you know those filtered through a mapping enter or whatever I professor architect loved functional programming is the ones for sure and putting it in his dad weird functional stuff all over the place and that's right sometimes a brick wall of looking at functional code but I can definitely see how it can change our of about catching strategies especially since it has a lot of these like and filter returning iterators now so you know they're lazy evaluations we don't have to like cash this whole huge you know change of a list or whatever we can sort of slowly build those changes and use them as needed in coming years right group they didn't watch the 5th and then use this
mean b if you were