 Good afternoon everybody last talk of the day Hopefully rating was worth it first. I would like to introduce myself my name is Mike Miller and I've been using Python more than 16 years now and I make my living teaching Python so I'm from Python Academy and I teach Python courses but this time I would like to talk about a little bit different topic about functional Python this Python is mochi. So mochi is a language that's actually written in Python itself and works closely together with Python and Has some very interesting. I think very interesting functional features Okay Mochi is synomically typed just like Python and It supports functional programming and also supports some kinds of actual style programming. I'm not really going to talk about this one today and It needs Python 3 3.2 actually because also works with PyPy. So PyPy The newest release of PyPy in the three series is 3.2. Otherwise, of course it works with Python 3.4 and Actually mochi translates to Python 3 AST so abstract syntax tree So there have been a few talks talking about this one and then of course once you're there abstract syntax tree You can translate the bytecode and The new feature which I recently added myself actually you can trade it translated to Python 3 code So really Python code you can see it. It's not fully functional yet. There's a few things missing, but The the examples are translated that work okay, and This makes up nice as some features so the features of language so the syntax is very Python like so indentation matters for instance And then there are a few features that are interesting for for functional programming In general tail call elimination. So you you don't have the recursion limit. You would have in Python. That's just interesting Of course, you don't have loops You don't have any loops you you be a we used to in Python There will be different kind of programming you cannot reassigned you cannot resign inside a function Use persistent beta types and these persistent data types just use pi or system, which is a different Python library You might know so much. He uses quite a few Python libraries. So you try to implement this as possible itself, but use other libraries Some something I want to show you show you this pattern matching So you might be maybe a familiar with the guy Haskell style pattern matching So there's a few things that come from Haskell and there are data types like algebraic data types I'm not talking about this topic too much here And then there's a pipeline operator I'm going to show you the pipeline operator and there's some interesting things for anonymous functions So the lambda function Python a little bit different here. There's this actor style thing like Alan You can write macros Like a list and you have a few built-ins that come from from from Python Like it or tools and fun tools and all those operator there Buildings and of course you have a interactive prom the repel So it's like the Python interactive prompt and it works with iPython notebook that has some basic implementation You can run it in iPython, which is pretty nice because I spent most of my time and I do something in iPython notebooks Okay, now I want to show you some of those features In a little bit more detail and show it actually some code Okay, you have the repel so once you have mochi installed mochi is just you can pip install it So if you have of course you need Python 3 if you have Python 3 Let's say Python 3.4. You can pip install it installs this pip There's a bunch of dependencies and then at the command line. It's just type mochi You see the well-known interactive prompt and then you can do something you can solve for Difficult mathematical problems like one plus one you get to as an answer Looks very much like Python and you can define a function looks very much like Python and you see the only difference here There's no return so the last expression in a function is actually what will be returned and that I do here and see even I define it I always get back the function object itself, and then I call the function and you get To 7 which is 3 plus 4 and you type exit you out so the the interactive prompt is very similar to Python Interactive prompt you can also write scripts when you write a script when you write a module So you are I write a file which is called add mochi here And I just to put my function inside a file and then I can import it So in the interactive prompt I just import it and I can call it so very similar to Python So not too much difference, which is nice. You don't want to learn too many things new Just reuse whatever you can and this is very similar how Python works You can generate Python bytecode and command line if you like so that's what I do for tests For instance, so I use Pi dot test to run the tests and this is just generating actually Python Python bytecode and then run the uses everything you can use this pie to test so pie test to run the test So you don't have to develop your own testing machinery just use whatever it's there with pie test So then once I you can compile it just a command line So the PYC option compiles the bytecode and also output file and which compiles the same name output file And you can import it and it's pure Python By code and you can import it and you can work with it from Python As I said, you can also generate Python source code. It looks a bit more messy here So that's a source code that comes out of course it inputs quite a few boilerplate things You actually don't need in this case for the special case That's in no way optimized and it also adds a few unnecessary nuns and stuff in here and put the add there But in general it generates Python code and I tried it is different things and sometimes it generates quite a bit of Python code And some of things I'm not actually needed which would be maybe if you run some optimizer, which is get rid of stuff That's not needed But a general works which is can produce Python code, which is nice because then you back in the Python world you can Use all the Python tools if you like Okay, you can also use Python modules in mochi So if it just import numpy, which is a very commonly used module in the scientific Python world for numerical Erase multi-dimensional race and you see what I'm doing here. This every everything I do it. It is done in this notebook I use the notebook to make the slides and this is a mode book in mochi mode So you can all the slides are just made as much if you want to say actually got make this iPython notebook But the cells here are mochi cells and then you see if if I say import numpy And I can define this a numpy and it's always returns it here This this is a this is a representation of the array which is a range just like a range And then I multiply eight times two you get element wise multiplication and you get result So you can reuse everything you have from Python all the extensions should work because it's just in the end everything's just Python Which is pretty interesting Yeah, so you can use also if you want to write a flask app you can write a flask app and just So there's some examples an example repository that one of them is using writing a flask app. Okay So the first thing this this This recursive calls so you this is this is the example Recursive functions of the factorial so if I get n and m if n is one I return m Otherwise, I call myself and this is a factorial and typically if you would do this in Python Then if you have more than thousand recursions again exceptions and you you could increase the recursion limit if you like but here you don't and Actually, if I want you you can look at the Python code it generates So it's pretty interesting. It's just the while loop actually in the background And of course translating it to a loop which is an interesting concept and here if you look at the timing it's not totally very fast Typically so speed is not it hasn't been a concern yet So there's no optimization, but therefore just a concern that makes make it work in the first place And then if you look at it, you see that's a very long number I didn't print the numbers bit long So just converted to a string and print the length so it's 35,000 digits of the number So you don't want to see this number. So it's not gonna Interesting Okay, you can do this just this is kind of Python style function But now if you want to be a bit more functional you can do pattern matching exactly the same function But now written this pattern matching so pattern matching similar to Haskell style pattern matching So this is a pattern I match So if if I get an N then I recall the factorial this N and one otherwise I If I get a zero I just return this zero and this accumulator here I return the accumulator Otherwise if I get N and accumulator, I just call it factorial this end It's exactly the same thing as we had before but written in a different style This is this is pattern matching. So if you know if Haskell elix looks familiar somehow and You could write something like this once you get used to this style that might be more readable than the other one It takes a while because it's a new concept if I've never seen this of course new concept But now you have this pattern matching you can write pattern matching and the source code that is that is generated It's a bit different and you see the runtime is bit slower You see the runtime before was point of four seconds and this is Point one Five seconds about nearly four times slower. So that's not optimized yet And if you look at this pysons source code generates generates a bunch of intermediate variables and stuff like this So this can certainly be optimized and maybe make it faster see all one But this would be something so if you if you if you like this Play a little with Haskell or something like this then then you can use the same pattern here And they end up this pyson and you can use the rest of the pyson world So this is pattern matching So if you have a list with the numbers one two three and then you see that's that's actually all of them It's not a notebook. That's a notebook cell and then you spits out this down here You see I will treat us in a minute p-vector. It's a pyosys and data structure Persistent vector just the list that you can imitate so to speak So you have my list and then I matched the pattern you see and I have this if this list here This is the access will turn the x because I match this one and x can be anything There has to be one and a two and anything so it returns my x Everything else will turn nine. This is a typical pattern matching thing There are some rules to it There's no real if you look at this there's no real documentation how the pattern matching works So that's something we want to do write some documentation that they actually can't use who so it works But you have to comment how it works the same thing here You see do you have this ad operator at this ampersand here is very similar to the pys and star so it's matching all the rest So if it's a one then no matter how many other elements you want and then you want their rest and you see it Gives you back down here the two and three So you have this just different styles of matching just as an example You can do this matching here and then we have this match you get something back Okay More pattern matching for instance you can match something like this a fist bus function Depending on the arguments you get for n the argument for n. I do something different So if if n is evenly invisible by by three and five I get this bus is only that it's only by three I get this and if only the five by five at bus and then even many runs as you see if I use them This different if you use a one it's not evenly divisible I get the one back because that's that's this case down here Then the last case yeah, and the other cases you see is three five you get the corresponding one and 15 is Divisible by three and five you get the last one so that's how this pattern matching works and you can do something like this That's pretty interesting. So no loops So again, you can use pattern matching. This is just some so of course in pys and z shape would use a bit in function Some that's a toy example, but let's see how it works And it's typical for functional programming so you have the head and the tail which I call it here So you can call somebody else but makes sense So this is the first and this is all the rest and you just say if you have this one Then you just take the head plus the sum so recursive called itself to the tail and if it's empty just return zero Yeah, so this is the same thing as a sum function recursively so it's a You first if you like loops you can still write loops in in Python module important and calculate some But this is the loop doesn't have any side effects here, then that's the function style to do it Okay, no loops You have a pipeline operator. So I reuse you you reuse the same function I used before this is the sys bus function You see and now I can put this because this is a pipeline operator the pipe and the greater sign So I can't generate the range which is which is a everything's lazy It's like in Python 3 range. Yeah, so it's a range object from from 1 to 30 And then I move this range up to the map and I map this fist bus function on this range object So that this fist bus function is called 30 times for each number and then this this is still lazy And then I move it to the p vector to to make it into a list So to speak and just persistent list and then I move it to print to print it out And the result is this this is the first line is the artifact of the definition Then this is a result. This is this p-vector with its 30 numbers where I apply the functions of everything Yep on by 3 by 5 and 3 and 5 things the same function and get the results So this is a pipeline operator, which is pretty interesting So it's chosen this operator could be something else, but this is unique. It's not used for anything else though Also in Python. I don't I don't think the operator exists in Python itself So it makes it unique as a pipeline operator So you use persistent data structures So this would be a talk in itself if you know Py Assistant, which is a Python library, which is use which Multi-users here. So everything is immutable and then you do something like an append So I make a I make a list you see you always see the return down here. I make a list V Which is a p-vector or make a mood a mapping It looks like a dictionary and then if I say append it doesn't actually mutate It gives me back a new object V2 in this case, which has a default painted and the the V itself stays as it was before Yeah, and there's more of them quite a pretty more of them. It's our own library That's the UCSO all the data structure should be persistent, which is a feature of function programming You can write an enormous function so lambda so this would be one bit This is the ad function that adds x plus y so x and y the arguments and then we just use this arrow kind of symbol instead of lambda to return X and y to some of x and y and if you call it you get a 3 back That's you can write it like this or this would be exactly the same thing You say I define any function add then use the same symbol and Then use a dollar sign this argument one argument two So you don't have to name them and then you just get back and I call it just the sum of the one and two is three So that's one way of writing anonymous functions, which can be handy once you get used to it That is dollar sign has its meaning So you can do something like this and so for instance you could do something like this I map and I define a function here that says takes the first argument and multiply it by Two and map it to the one-two-three that just just it's like list comprehension x times two for x in list pretty much in this case, but you could do something else and this would be P vectors or persistent vector if you don't do the P vector just get a map object You wouldn't see anything because it's lazy, but that's how you can write anonymous functions here So it also as I said It runs in an iPice in notebook. There's still quite a bit of room to improve. So there's a basic Colonel actually implemented by my dears person as I think his name He's a iPice and call developer and I just inherited the project. It has some basic functionalities It does there's still a few things missing. There are no call tips no magic functions And there's also some kind of problem and that standard error goes to the consoles instead of the notebook So I have to resolve this one But in general you just type in a cell you get back the result which is nice to play play around with it So it has a notebook mode and You can use it And the status right now, this is it's still a very early project it It's work It's what has a working information as I show you it works and does pretty much what the what the features say But it's still pretty early phase that needs more tests. So I wrote a few tests actually uses pie test which is nice so you can use all the pie test machinery to Automate the test if you test you need more tests. It doesn't cover everything It needs more documentation. So right now. There's not much documentation. That's just a read me file which But it shows what I have here a few more things in there in the read me But there's no real documentation of course and there's nobody using it yet But might be interesting if you want to get a functional programming So it's a it's a new language to some degree But still uses so much of Python so you can use all the Python ecosystem and you don't have to invent anything new Good, so it's a I made short mere short walk I think so I'm planning to have a sprint here at your Python if you're interested So I put it up and this wiki page So if you want to sprint and if you're interesting in function programming in general, there might be an idea It's just to gather and do something so it is very early. You still can form the project We had to sprint at PyCon us and there are the few people that had some ideas what to do to write some more formal things the formal language Things and have some more testing and so there's something going on there But if you're interested in functional programming and would be nice to sit together and look at this source code and do Something with it and I would like to thank you. Thank you the lead developers. I'm not the developer of this thing I'm just got into the project later on. It's developed by a Japanese guy used called Yasushi Ito and he's Japan obviously and probably he will take part in to sprint remotely He's always like eight or nine hours or ten hours difference or something like this But he's the one who came up with the idea and I find it pretty interesting and there are a few other people the likelihood you can do a little bit to the to the source code and Maybe it could be a way to integrate it somehow in the Python ecosystem to have some functional approach to certain type of Type of tasks without leaving what you have invested in Python already Okay, I would like thank you for your Attention I'm open for questions and if you want to reach me That's my email address and also there will be something up at a sprint page So if you're interested here stay over the weekend to come to the sprint and talk a bit more about it. Thank you very much So where do you see the project in let's say three years? Excuse me. I was just wondering is it more let's say a hobby An education project or where would you see it if it was totally successful in three years? Would it be part of Python language or maybe the pattern matching moves there? That's a good question I don't know yet. So right now. It's just it's just a very interesting project It's I don't have a real use case for that. I need it for something also It's just it's more like an interesting hobby project to do something just because I'm playing this function program for a while But I like Python also and as would be maybe a good interesting Approach to see if it works to have some functions stuff sitting on top of Python That that's what I like because if you use Haskell's different language totally different ecosystem And you have to do totally different things here can reuse a lot of your Python knowledge But you have a few more interesting ideas this might be useful for certain type of problems But you don't have to decide I want to do only Python or only Haskell or something It's very easy to go from one to the other that's but I think right now is just for interest But maybe some somebody has some people a real life from some people to come from function programming come to Python They miss a few things that might be interesting this way. So that's that's a that's just a status of project. Okay. Thank you How much work is it to be able to make everybody have pattern matching in between based on your work If if I want to backport what you have done on pattern matching for instance In Python How much work is it? I don't know if I can understand your question if you want to put the pattern matching into Python on how much work would be or is This is a question. Yes, I don't know how much work it would be This is a this is a totally different part. So you you pass the source code and generate Python That's a very different thing So you sit on top when we generate Python and then instead of writing all this Python by hand You just generated from a much more convenient source code But in the end it's just Python. It's the AST or bytecode or Python Code is the same. I don't think you there will be any chance with that pattern matching into Python before Python 4 or Python 4 I don't know In the last few years there were a several Functional programming languages new languages like Scala, Clojure, F-Sharp and How how do you see? Mookie or Mookie Among them also OCaml Where it will be you you think In comparison with All those that most of them are based on the Java A Platform Yeah, so F-Sharp is in top net, but the other ones are Java based. Yeah, so it seems like it's a company You have this Java language which can serve a certain problem and on top of it If we have Clojure which works as a JVM and this is to some degree similar So of course what is very new and that doesn't have the many features yet But it's it's on top of Python and you might use it for some different types of problems that Python might not be the best or you don't like it or something like this So that's that's how I see it right now. So I'm it's very difficult to predict the future So but it's similar to this and close was used quite a bit in Scala. They are pretty successful Hi, um Are there doc strings in Mocchi? I Don't know. I think so haven't checked there should be so I think there should be in there Otherwise we put it in so if something is missing for it, but I think it's it that should be there. Okay Yeah, thank you for the really interesting talk. It's like it's the first project I see where people compile something into Python bytecode and So kind of thank you for opening the floodgates of like having a bunch of languages on the same platform Which I think could be useful and because many people are missing different features and now they don't need to go Quiddoo, but they can just roll their own And I have a small question So in the example with the pipeline it seems like you partially apply in map And I was wondering how do you figure out whether you partially apply function or not because in Python like It would not be very clear Like the number of arguments can be varying and so how do you do you know how this is how this works because This is normal Python map in the end, right? Yeah, you have one argument you have n and you apply the function this bus to all this to everything in the list Yeah, so basically rates iterates through the list is just one arguments forever. It's just it's just like list comprehension Yeah, but what what stands between the second pipeline in this and The first and second pipeline it looks to me like partial application of map And then it would be applied to something that goes through the paper pipeline So I was wondering how it figures out that here it needs to partially apply it whereas in Some other places. Yeah, so you see like it's like it takes map as a first argument And then gets with a with a pipeline of already gets all the other arguments in there It's like map map will apply to a list in this case means apply map to this list of Apply map to range That yeah, I understand how this works. I I just What I don't understand is how the parser understands that in this case map should be partially applied But if for example, I wrote print this buzz in the middle It would probably need to like it would need to fully apply print perhaps or something like that or maybe also So I was wondering How this works in practice? Maybe I'll come to you. I don't it in detail. We have to have the local source code Hi, thanks for the interesting talk What was the inspiration for the name of the language? The name is is Japanese means rice cake. So this is Yeah, just wish you decided for the name. So the name was given already Thanks. I've got two questions that the first so you've got all these persistent data structures Which are really wonderful in other languages that have great multi-threading but in this case It seems like a lot of that power is just lost. Is there other plans to? somehow magically support More threading around these persistent data structures It runs with pi pi Okay, if you saw the talk before the STM works and pi pi is doing the right thing Piper should probably figure this out itself. I'm not sure so that that's one thing So it's just Python byte when it works with pi pi That's why we say we need Python 3.2 because pi pi right now is in most modern versions of 3.2 That would be probably the easiest Solution just use whatever there because doing it yourself could be a little work right and and the second question was about the actor stuff How when you have these actors running? What are they running in separate threads different processes? Is they use they use? Event let I think okay, so there's they use event let and I haven't worked with it with the actors yet So I have I cannot tell you much about the actors, but they use event let to just give you a nice as syntax to use it more questions Which technique do you use to optimize tell recursion? It's not optimized. It's just rewritten in a loop See if you look at the source code You can you can translate it? No, there's a translate and you look the source code is just a while while true loop and just translating it to a loop Generate is an immediate function stuff like this when you have mutual Recursivity you transform let's say three or two function in a big loop Or do you just handle? Recursion with one function. It's just one function. So fuck more questions. So thank you very much