Add to Watchlist

Functional Python with Mochi


Citation of segment
Embed Code
Purchasing a DVD Cite video

Formal Metadata

Title Functional Python with Mochi
Title of Series EuroPython 2015
Part Number 160
Number of Parts 173
Author Müller, Mike
License CC Attribution - NonCommercial - 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.
DOI 10.5446/20167
Publisher EuroPython
Release Date 2015
Language English
Production Place Bilbao, Euskadi, Spain

Content Metadata

Subject Area Computer Science
Abstract Mike Müller - Functional Python with Mochi While Python supports procedural, object-oriented, and functional programming, its functional features are not fully developed. Mochi is a Python-like functional language that compiles to Python 3 and PyPy 3 bytecode. It can use Python libraries and can be used from Python. Mochi adds functional features such as tail recursion optimization, no re-assignments in function definitions, persistent data structures, pattern matching, algebraic data types, a pipeline operator, better anonymous functions, Erlang-style actors, Lisp-style macros as well as many useful builtin functions. This talk presents what Mochi is, how it works, and what you can do with it. Functional programming can help to solve certain kind of problems elegantly. Done right, functional programs can be easily tested and provide more confidence that you program is really doing what you want. Mochi could be another tool in your toolbox. Functional programming can expand your horizon and can be a lot of fun. Mochi offers easy access to this new world because you can leverage your existing Python knowledge and libraries whenever needed.
Keywords EuroPython Conference
EP 2015
EuroPython 2015
good afternoon everybody last talk of the day before the reading of certain those
that would like to introduce myself my name is mike middle and I've been
using pricing more than 16 years now
and I make my living teaching Python and so comprising Academy and at each and courses but standard like it's all about the different topics about functional fighting this presents much to much use range written price itself and rocks close to that this despite and has some very interesting I think very interesting functional features OK what this type just like Python and it supports functional programming and also supports some kind to start programming and reproductive about this 1 today and it needs prices streets to actually because also also works this supplied by the new year the written news release pipeline industry psoriasis frequent 2 of us possible passes through point 4 and actually watch translates to price history ht abstract syntax trees government had been if we talked about this 1 and that was monster that certain texts written translate the white coat and the new feature which I was end myself actually you can trace the Python 3 codes so really bicycle to conceal it fully functional yet there's a few things missing about a 3rd of example sentences that OK and this makes up the nice that some features so the features of language of the syntax is very price indentation members for instance and then the features that are interesting for for functional programming in general field coordination so you don't have recursion limit you would have an outcome which is interesting of course you don't have to you don't have any loops you you you we still happen to be different and programming you get reassigned and every and uh inside a function use persistent data types and these persistent used by our system which is
a different price library might know so much use quite a few price libraries you try to implement this that's possible uh itself use of a libraries some something I wanted to show his pattern matching so you might be maybe a familiar with sky Haskell style pattern matching so just a few things that come from Moscow and then you data types like
a different data types uh I'm not talking about this topic too much here and then this type and operate on can assure the pipe break and there's some the interesting thing for anonymous functions so the length function price different here just style things like how long you can write macros like this and you have a few buildings that come from a from from Tyson like to tools and some tools models operate via the events of course you have ejected from the records of like the price in the front and it works as I personal notebook that has some basic implementation you can run the 9 parts which is pretty nice the closest and most of the time and to something and I present OK now I wanna show you some of those features air in a little bit more detail and show some called OK you have the rapid so once you have much installed much it's just you can't have it installed and so if there was any price if you have questions for you but there was this report for you can't get solid installs this this is a bunch of dependencies and then at command then just have much you see that the terminal and interactive prompt and then you can do something you can solve for a difficult mathematical problems like 1 plus 1 it was announced that looks very much like a Python and you can define a function looks very much like phone and you see the only difference here there's no return to the last expression in a function is actually but current and about to hear and see that defined it always get back the function object itself and unequal function you get 2 7 which is for any topics at you up to this today in that promise similar to the 2 parts of and from you can also write scripts when you write a script when you write a module so I I write a file called at much here and adjust put my function file and I can important so invented from which is important and I can call its over similar to price and so not too much difference which is nice you don't but them too many things you just be reuse over you can and this is 1st a you can generate Tyson fight code and command-line the like so that's what I do 4 tests for instance I use type of test run tests and this is just generating a cheap price and price and by could then run evident you can use this part of testified to run the test so you don't have to develop it testing the can just use whatever its there's right so and then once you compile command of the p y c option complex bytecode and always output file and which comprises the same name of confinement important pure Python by coat and it is important general was entitled as a set can also generate Tyson source code that looks a bit more massive here that if the source code that comes out of course in inputs quite a few boilerplate digital need in this case for the special case if it's in no way optimizing the old execu you unnecessary nuns stuff in here and at there but in general it generates bicycle now try to different things and sometimes generates quite a bit of Python called there and some things not exceeded which would be maybe if you run some optimizer which has get rid of subsets but generally works as can use Python code which is nice because then you begged questionable to can middle class includes OK you can also use
Python modules in watching so if you just port number by which is a very commonly used module in the scientific question world from numerical arrays multidimensional arrays and that you see what I'm doing here is every everything I do it because it's fun with this node values makes a slight and samogon marginal so you can all the slides this makes what he was saying she got its act as proposed by the itself see on what cells and then see if if I say import number and I can define this a number 5 is always returns of this this is this is the representation of the brain which has a range of like range and then I multiply times to you get elementwise multiplication you get yourself so you can reuse everything you have uh comprising all the extensions would work processes and if is just part which is pretty interesting yeah so you can use also human right of last that you can write class of interest to suggest that examples of sample of the trees 1 of them is to the right of the OK so the 1st thing this this is true because of course so you this is this is the example uh Recursive Functions of material so if I did N and if N this 1 I respond and otherwise I can call myself and this is a vectorial and typically you would do this in Python and then if you have more than 2 thousand persons again sections and you you could retrieve theory of human life parts here you don't and if you want you can look at a bicycle degenerates search interesting says the wife of a tree in the background the coarse translates to which is interesting concept and here so if you look at time that it's not totally very fast so that we create so speed is not as concern so that's the optimization of the objective can made make it work in the 1st place and then if you look at it to see that's a very long numbered inference and almost long to just converted to strain and bring the length of 45 2 digits of the number of you don't see number of interest OK you can do this just this is kind of crisis that function but now if you want to be a bit more function you can to better match exactly the same function but now written this pattern matching so pattern matching similar to Haskell stop dimensions so this is how I'm actually if if I get in and then I will call the vector this and 1 otherwise I divide is 0 I just written this 0 this accumulated you read from the cumulative otherwise if I get n and accumulate I just called picture this and is execution thing of 4 but written in a different style this this is pattern matching so if you know the task of troops from somehow power and you could write something that response to get used to the style that might be more readable than the other 1 it takes a lot of course is a new concept of analyzing this course you students but now we have this pattern matching you can write pattern matching and source quoted the general as the different runtimes cancel sort the runtime before was . 0 4 seconds and this is uh . 1 5 seconds about uniform principles so it's not optimized look at this as a source code generator generates bunch of intermediate variables and stuff like this is going to be optimized and maybe make a philosophy of but this would be something so if you if you you like this that player hostile on this and then you can use the same set of DNA at the end to end up with a positive and you can use the rest of the present so this is not a matching so if you have a list this number of 3 and then you see that's that's actually all of them is done in the book that's notebook 7 spits out down here you see the treated in a minute lots of classes system that structural personal assistant that just a list that you can annotate so so you have my list and then I I matched the the header you see and I have this if this list here this is X is built from the extra because and match this 1 x can be anything that has to be 1 and 2 and anything that returns my x everything else goes from this is a typical pattern matching in DB decimal look at this there's no real fragmentation of imagined works is that something you want to write something mentation that we can useful so it works but you have to commence out works the same thing here you see happens at operator at this ampersands here is very similar to the price of stocks is matching all the rest so if it's a 1 and then no matter how many of our elements you want and then if you want the rest of this season gives you vector of the of the 2 industry so you have to specify different styles of matching just as an example because as mentioned here and then you have this much of its OK what mention for instance you can match something like this response function depending on the arguments get for and the argument for and I do something different so if if if and is evenly divisible by by 3 and 5 and that his boss is only that Sony by really I get this is going 5 by 5 the past and then the menu and choose see values the and differently use 1 it's not evenly divisible market the 1 because that's it's this case down here the last case here the other cases you use 3 5 we get the corresponding 1 15 is divisible by 3 and 5 african lost to that's how pattern matching works in countries like this that's pretty interesting there are no loops so again and we can use pattern matching this is just some of the prices you would use that functions on this for example but it's see how that works it's difficult for functional programming so you have the head and the tail which quality of sleep you can call something else but makes sense so this is the 1st and this all the rest and he just said if you have this month and you just take the had plus sum of the recursive calls itself does entail and if it's and just return 0 yeah so this is the same thing as sum function because of the limited in you put it if you like to consider right groups and enterprise model imported from private some but this this this is in the presence of and inside effects here then that function to no loops that we have a pipeline each so I reuse URI is the same function the before this this this this loss function you see and now I can put this this is this is a python operate the pipe and the greatest time so a great generator range which is which is everything's lazy like comprising 3 range here so it's a range object from from 1 to 13 and then I'm in this range of the maps and and that this discussed function on the range object of his response functions 30 times for each number and then this is still lazy and then I will to the Bubeck talk to me to make it into a list of the speech just persistence and then I moved to the printer print it out and the result of this this is the 1st line of affected definition then this is result of this theoretical results study number very clever functions everything you want by stream by 5 . 3 5 consistent function good results but this is a private operator which is pretty interesting so as just this operator would be something else but but this is unique to produce anything else that wasn't by some of them to be something that democratic system present itself so next time we can it's a pattern so you use persistent data structures so this would be a talking itself if you know by our system which is the Python library which is used in which the still so everything is neutral and then you do something like independent the I make a make a list oversee the return that you are making history which is a few victory or make them which a mapping looks like a dictionary and then in fact I see it can't it doesn't actually you take it gives me back a new object to in this case which has 2 for standard and that be itself states and not yeah and there's more than 5 of them with more than its own library that's the use use of all the data structure should be persistent which is the feature of functional programming you can write
functions so that the this would be 1 dataset that function that adds X plus Y so that X and Y the arguments and then we just use this error kind of simple set of number to return x and y the sum of x and y and equality get sleep that's you can write it like this form this would be exactly the same thing he I define a function that they use a sensible and then use of all signed this argument 1 argument to we don't have a name them and then you just get back in accordance with the sum of the money to but this 1 frightens functions which can be handy was did you said that the stores science has meaning so you can do something like this and so for instance you could do something like this I met and I define a function here this is takes the 1st argument and multiplied by 2 the method to find history which is just like this comprehension X times 2 for X in this pretty much space but it something else and this would be a p vector of persistent activity belong to the p-vector to scan of object and scene because but that's how we can write and should FIL it also as a sense it runs in IPython notebook yes you quite a bit of room to improve so that's the basic uh frontal and actually limited by by the US was in assessing his name prices co-developer and interesting character project it has some basic functionalities it doesn't help you things missing on Caltech symmetric functions and that's also some kind of problem and some of Oracle's consultancy notebook you resolve this 1 but in general you just type in itself you get that result which is nice to play play around it has in the book and you can use and interest for the 3 minutes for questions and the status right now this this is still very early approaches it has it's wrong it's hadn't history works as DOS pretty much further but the futurists say but still the on face but needs more tests are a few tests actions pi test which is not so you can use all the right person can read through to make the test because you need more tests doesn't have or ever see that it's not augmentations right now does not much documentation that's just the readme file which but shows when I have here a few more things In that indirectly by there's no imitation of course just know where we using it yet but might be interesting to if you want to get a functional programming so it's so it's a new language to some degree but still this is so much of the present you can use all the prices system and you don't have to invent and thing uh and so it's committee for their for work and so I'm going to have a string here that's python if interested so I put it up and the the page so if you want to spread and the interesting functional programming in general might be an idea was to develop do something so this is the only still can't form the project gets printed by Congress and they're for people to have some idea of what it would the rights more formal things the form language uh the things and have some more testing and so there's something going on there but if you interested in functional programming and it would be nice to go and look at this source code and something is there and I would like to thank you thank you the developers that and developer this of this thing I'm just got into projected on its developed by the Japanese kind used a because that's what he told and he's just on the sky and probably would take part the sprint remotely it's like it online also announced difference something like this uh but he's the 1 who came up this idea of fun pretty interesting and there are few other people like to contribute to the to the to source code and maybe it could be later integrated somehow surprising because have some functional approach to certain types of type thousands of what you have invested right OK I would like to thank you for your attention and look the questions and if you want to reach me that's my e-mail address and also that there something of a and strange page interested you stay with weekend to come to some friends and for the more what withouth
so where do you see the project in let's say 3 years because he was following from all the all the and and education project all where would you see it in the was totally successful in the years with the part of Python language only the pattern matching move there are better than the rest of the year so right now the stress is just a very interesting project I don't have a real use cases for that I needed for something else so it's just it's more like an interesting hobby project to do something just because I'm playing function program for a while and but I like small cylinders would be maybe it would be interesting approach to see if it works you have some function stop-signal couple Python that that's what I perceive use of Haskell's information which be different ecosystems and you have to do to the different things here can we use a lot of your price and knowledge but if you more interesting ideas that might be useful for certain types of of problems but you decide I want to only 5 was only Haskell something it's very easy to go from 1 to the other so that's 1 thing right now is just for interest but maybe some somebody has some people really like that some people come from functional programming come to Python and the music things there might be interested in this so that's that's that's that's that's a culture of consumption the next fall is it yeah go to make everybody have um but the matching between measuring if you if I want to buy brought lecture don't that and that's the point the In by on much more easily at an African and it wasn't a question if you if you want to put the pattern to Python on how much more would be appealing to the question I don't know how much work it would be this is this is a totally different also you you pass a source code and generate platform it's a very different things so you sit on company generate 500 then instead of writing all these prices and just generated from a much more convenient source but then Justice price is a steel pipe total uh go to to say that I don't think you double the any chance that emerged depriving people places for all kinds of other but In the last few years there were various overlays it is a functional programming languages new languages like Scala closure if shown there how how do you see in MoKi on looking a along then all all kinds in a way that will be you you thing in a in comparison with they all those that most of them are based on the John any yeah that's a strong involvement but the other ones all Java-based yet but it seems like it's it's a company at its job in which we can so this open problem and probably aware approaches which brought JVM and this is to some degree use similar so called working very new and a dozen of the many features yeah but it's it's in pipe and you might consider all sorts of different types of problems with piosson might not be the best you don't like it or something like that so that's fallacy right now so and is reverted to predict the future of the rope 5 6 similar to this imposes used quite a bit of problem there pretty successful and other doctrine think much we I don't know I think so haven't checked the from I think differently than others you have multiple degrees so something is missing when it was really about the it's it's that should be there so yeah thank you for the really interesting but it's like it's the 1st project this year where people compiled something in the Python bytecode and so on and thank you for opening the floodgates of like having a bunch of languages in the simplest from which I think would be useful and because many people in missing the features and now they don't need to go to with them but they can't roll their own and they have a small question so you may example was the pipeline it seems like you partially applying map and I was wondering how the figure out uh whether you partially applied function are not because in Python like you will not be read here do you think the number arguments can be varying and so how do you how do you know how this is how this works because this is normal lighten up 100 yeah views of the of 1 argument you have and then we apply the function this past all this to everything in the list yeah so basically erased iterates through the this is just 1 argument for every this is just like this convention yet but what what stands in the 2nd pipeline and and and the person 2nd pipeline you it's looks to me like partial application of map and then it will be applied to something that goes through the paper by so I was wondering how it figures out of securities must apply it to us in some other places you get is easy like likely to take some of the 1st argument and then gets this is a part it's or the of arguments in the but now that we applied to list in this case means applying that list of of the climate arranged met I understand how this works I just and what they don't understand is how all the parts understands that in this case maps should be partially applied but if you for example I wrote this is because in the middle and it would probably need like you would need to fully apply print perhaps is something that will maybe also and so I was wondering how all this Morton practice made up of the I and I had only needed at the end of the source said my thanks for the interesting don't on 1 of the information for the name of the language and then this is Japanese means right scale so this is where you get your this which is designed for the names of the name was but I got 2 questions that the 1st see persistent data structures which are really wonderful in other languages that have great multithreading in in this case it seems like a lot of that powers lost other plans to somehow magically support more spreading around these persistent data structures it once was so people have it it it is a little for the STN works and pipelines comprising high-pressure public figure this out itself and natural so that that's 1 thing to it's produced just by surviving virtues pi-pi that's right saving the price is 3 . 2 proposed by pronouncing what most modern versions 3 . 2 that would be probably the easiest solution just use whatever their because yourself would be right and in the 2nd question was about the actor stuff happens when you have these actors running what are they running in separate threads different processes as they say they use they use um event let's think about this that the user meant like and I have a look at these actors yet so that I can tell you much about of buddies you meant that to just give you nice since the moreover research which taking the used to optimize the notion His of optimizers just we wouldn't have to taking a look at the source code that you can you can translate it and this is a transcendent locus consists of while by bilateral just translated to the generators immediate function stuff like this when you have new you on regions in the EU comes let's say it's real to function in the rope or do you just and a regression we 1 function is just 1 function from Morrison both the NeuroMat thank you
Computer animation
Computer animation
Computer animation
Computer animation


  474 ms - page object


AV-Portal 3.8.0 (dec2fe8b0ce2e718d55d6f23ab68f0b2424a1f3f)