Latest / Talk Python To Me / #117: Functional Python with Coconut
Transcript
- 0:00One of the nice things about the Python programming language is it's at least three programming paradigms in one.
- 0:05There's the procedural style, the object-oriented style, and the functional style of programming.
- 0:12This week, you'll meet Evan Hubinger, who is taking Python's functional programming style and turning it to 11.
- 0:19We are talking about Coconut, a full functional programming language that is a proper superset of Python itself.
- 0:25This is Talk Python, episode 117, recorded May 22, 2017.
- 0:54Welcome to Talk Python To Me, a weekly podcast on Python, the language, the libraries, the ecosystem, and the personalities.
- 1:01This is your host, Michael Kennedy.
- 1:03Follow me on Twitter, where I'm @mkennedy.
- 1:05Keep up with the show and listen to past episodes at talkpython.fm.
- 1:09And follow the show on Twitter via at Talk Python.
- 1:13This episode is brought to you by Rollbar and Hired.
- 1:16Thank them both for supporting the show.
- 1:18Check them out at Rollbar and at Hired underscore HQ on Twitter and tell them thank you.
- 1:25Before we jump into the main part of the show, I want to let you know that we're adding a couple of new courses over at Talk Python Training.
- 1:32Within the next few weeks, we'll be releasing RESTful HTTP services with Pyramid and Python, including a SQLAlchemy data-driven backend, and another course, MongoDB for Python developers.
- 1:45If you're interested in either of these, be sure to create a free account over at training.talkpython.fm to get notified.
- 1:53Evan, welcome to Talk Python.
- 1:54Thank you. Thank you for having me.
- 1:55Oh, it's an honor to have you. I'm really excited.
- 1:57You've got this really cool project that I think is doing some interesting stuff in the Python space.
- 2:03And so I'm pretty excited to share this new functional superset of a language that you've created.
- 2:11But of course, before we get into the details with Coconut, let's talk about your story.
- 2:15How did you get into programming in Python?
- 2:16So I started programming actually as a sort of summer camp thing between middle school and high school.
- 2:24It was not a very well-taught summer camp, actually.
- 2:28It was really just, we're going to throw some Python at you.
- 2:32I took a look at it, and I sort of experimented with it, and it was interesting.
- 2:36What really got me excited about it was just the things that I started to be able to do once I just was exposed to it,
- 2:45and then started learning about Python online, going through the documentation, and building up.
- 2:50I started writing text games and worked my way up to more complicated mathematical stuff,
- 2:57follow along what I was learning in school.
- 3:00And as I went through, I sort of worked my way through the various different things in Python,
- 3:06working my way to know the language better and better,
- 3:09all from this start just as, what is this thing?
- 3:13This thing that actually lets you take a computer and give it commands and actually output something that's interesting, useful, and powerful.
- 3:20And so that was my beginning, my introduction, the way that I got started.
- 3:24That's cool.
- 3:25And the first language was Python that you learned?
- 3:26My first language was Python.
- 3:28Yeah.
- 3:28And I used Python pretty exclusively for many years after that.
- 3:34The second language that I really got into after that was Haskell.
- 3:37Okay.
- 3:38Yeah.
- 3:39Haskell is definitely one of the more popular functional programming languages, right?
- 3:43And so did that kind of blow your mind?
- 3:46You're like, whoa, Haskell's different.
- 3:48Absolutely.
- 3:48Yeah.
- 3:49So I, at that point, had been pretty used to Python.
- 3:52That was sort of two years after I had originally learned about Python.
- 3:56And since that time in doing a lot of Python, like I was saying,
- 3:59Haskell was this new experience.
- 4:01This experience of there's all these new concepts, all these new features,
- 4:06all these new powerful things I'd never seen before.
- 4:08I was blown away by how powerful pattern matching was.
- 4:12I was blown away by how powerful all of the sorts of different constructs that I had available to me,
- 4:18the different things that I could do in Haskell that I had never seen before.
- 4:22And one of the first things, the sort of mythical origin story of sorts was one of the first things I did
- 4:29when I tried to learn Haskell was I went back and I tried to implement it and like do all of those things in Python.
- 4:35And of course that wasn't, that didn't go as well as I was hoping.
- 4:38And that was the sort of basis and jumping off point for what I would later do.
- 4:42That's really cool.
- 4:43Is that the thing you called Rabbit?
- 4:45Yes.
- 4:45So originally, so the very first programming language that I wrote was Rabbit.
- 4:50That was a, just a sort of project that I was making.
- 4:54I was like, I'm going to take all of these cool Haskell features that I think are awesome,
- 4:59all of these new things that I'm learning, and I'm going to put them in Python.
- 5:03I'm going to write an interpreter on top of Python that, of course, that's a terrible idea for what should have been obvious reasons.
- 5:11But how long can it take, like a week?
- 5:13But yeah, so I wrote that.
- 5:17It was, it was, it was fun.
- 5:19It was a cool, interesting project.
- 5:21Would not recommend trying to write code in Rabbit.
- 5:24But it was a big learning experience for sure.
- 5:27And a lot of the lessons that I learned in that process, I would later apply to Google.
- 5:34Yeah, that's really cool.
- 5:34And Rabbit is actually on GitHub.
- 5:36So I'll put it in the show notes.
- 5:37And if people want to check out your first attempt, they can do that, right?
- 5:41Yes.
- 5:41Yes.
- 5:41That's on my GitHub.
- 5:42Nice.
- 5:43All right.
- 5:44So what do you do day to day?
- 5:46You're still in college, right?
- 5:47Yes.
- 5:47So I'm a student at Harvey Mudd.
- 5:49I'm a rising junior.
- 5:50And for people listening to other parts of the world, where's Harvey Mudd?
- 5:54Harvey Mudd is in Claremont, which is a suburb of LA in California.
- 5:58Yeah.
- 5:59Excellent.
- 5:59Okay.
- 6:00And then you also did some intern work at some pretty interesting companies, right?
- 6:03Yeah.
- 6:04So I am currently an intern at Google.
- 6:06Last summer, I interned at Yelp.
- 6:09And before that, I was an intern at Ripple.
- 6:11Oh, that sounds...
- 6:12That's pretty awesome, man.
- 6:13What do you think of that experience?
- 6:16It's been a blast.
- 6:17I've really liked all of the companies that I've worked for.
- 6:20I've done a lot of really interesting things as part of those jobs.
- 6:25And Yelp, of course, that was really exciting last year because that was all in Python.
- 6:28Oh, yeah.
- 6:29Nice.
- 6:30So you're a math CS grad, right?
- 6:32That's correct.
- 6:33Yes, I'm a math CS major.
- 6:34I see.
- 6:34Nice.
- 6:35So, yeah, I also studied math, not so much CS.
- 6:39Like I said, it's a bit of a minor type thing.
- 6:41It wasn't called that, but more or less.
- 6:44And so how do you feel like that's prepared you for this, for these internships and some
- 6:49of these experiences?
- 6:50Obviously, the computer science portion of it is there.
- 6:53But the math thing, it really is also there as well.
- 6:56I know that in my work at Yelp, there were points where I used graph theory, points where I used
- 7:04number theory.
- 7:05So the stuff really does come up where you want to have that background to be able to fall back
- 7:12on to help you handle weird, interesting, challenging problems.
- 7:16Right.
- 7:16More in the algorithmic side.
- 7:18Yeah, nice.
- 7:19I also feel like math, if you get really into math, like advanced math proofs and real analysis
- 7:25and things like that, it teaches you how to think and solve problems.
- 7:29And it doesn't really matter the rules around it.
- 7:32The programming rules are programming rules, right?
- 7:34I think it's pretty good for foundation.
- 7:37Yeah, I totally agree with that.
- 7:38Awesome.
- 7:38So you've had this Pascal experience.
- 7:41And of course, there's a number of different functional programming languages.
- 7:45Why don't we talk a little bit about like, what is functional programming?
- 7:49I mean, it's not just using functions, right?
- 7:51It's entirely different than that, isn't it?
- 7:54Yeah.
- 7:55So the sort of textbook classic definition of functional programming is that it's all
- 8:00about pure functions.
- 8:01It's all about immutable data structures.
- 8:03And all of that is true.
- 8:05Functional programming does involve pure functions.
- 8:07It does involve mutable data structures.
- 8:09But one of the things I think a lot of people get frustrated by with that definition is that
- 8:14it's sort of exclusive.
- 8:15It says what functional programming is not.
- 8:18It isn't about mutability.
- 8:19And it isn't about functions with side effects.
- 8:22But it doesn't say what functional programming is.
- 8:25And what functional programming is, is really those are the parts that Coconut is really
- 8:29trying to nail down.
- 8:30Because Coconut isn't a pure functional programming language.
- 8:33But what it brings to the table, and what functional programming brings to the table,
- 8:37and what Coconut brings to the table with it, is the way that you work with those pure
- 8:41functions and those immutable data structures, having them, the things that that lets you do.
- 8:45It lets you have things like higher order functions.
- 8:47So you can take paradigm standard practices and turn them into functions that you can then apply
- 8:54in general concepts.
- 8:55It lets you have pattern matching.
- 8:56It lets you, instead of having to explicitly define how you're going to match or take a
- 9:02look at a particular incoming data type, it lets you just define a general pattern for
- 9:07what the data type looks like and how it gets deconstructed.
- 9:10Right.
- 9:10Maybe the best way to conceptualize that is it's a little bit like function overloading,
- 9:15but way more general, right?
- 9:18Yeah.
- 9:19Yeah.
- 9:19That's definitely a good way to think about it.
- 9:21Because when you're overloading a function, I guess, in like C or C++, you're doing it
- 9:26based on just types.
- 9:27And pattern matching is sort of, what if you could do that not just on types, but on all
- 9:31sorts of different attributes of the data?
- 9:34What if I could have a different function call for when it was an empty list versus when
- 9:38it wasn't an empty list?
- 9:39And that's, yeah, that's one of the big things that the pattern matching list you do.
- 9:43Awesome.
- 9:44Yeah.
- 9:44We'll dig into that.
- 9:44Yeah.
- 9:45And you also have lazy evaluation.
- 9:47There's some other follow-ons or corollaries from this that are very, I think one of the
- 9:54reasons driving functional programming is with this immutability and no side effects, parallelization
- 10:02becomes like what they call embarrassingly parallel, right?
- 10:07Where it's just like, well, you just break it into pieces and just do that, right?
- 10:10Because you don't have to worry about one piece getting ahead of another, right?
- 10:14Yep, exactly.
- 10:15Exactly.
- 10:15Parallelization becomes really trivial, really easy to do when you have your functions written,
- 10:22your data structures written in a functional style.
- 10:25Nice.
- 10:25Nice, nice.
- 10:26So give us some examples of existing functional languages.
- 10:29So I mentioned Haskell.
- 10:31I'm a big fan of Haskell.
- 10:33I've used Haskell a lot.
- 10:34But there's definitely lots of other functional languages.
- 10:37On the sort of more pure side, you have languages like OCaml and Erlang, as well as Elixir.
- 10:44It's a newer variant.
- 10:45On the sort of middle ground, sort of half-functional, you have languages like Scala, languages like
- 10:53CoffeeScript or LiveScript or JavaScript.
- 10:55There's really a whole wide array of functional programming languages out there that try to bring
- 11:02in some of these new powerful features and put them in different contexts.
- 11:07Sure.
- 11:07And if we think about Python, Python itself has some functional components, right?
- 11:13We could go like full in OOP programming in Python.
- 11:18We could go quite deep into the functional program.
- 11:21You can, or just straight imperative code, right?
- 11:24We can sort of pick some pieces up and choose a style for Python, right?
- 11:29Yeah, absolutely.
- 11:30I mean, that's one of the things that I love about Python.
- 11:32Python really is great at being a multi-paradigm language.
- 11:36You really can do all of the functional programming stuff.
- 11:39You know, one of the things, of course, that I would say attest to that is that Coconut can
- 11:43compile to Python and make it all work, even when you try to import it from Python.
- 11:47But there's a lot of things that, while they technically work, really don't work nicely.
- 11:52So when Python is designed, the sort of whole philosophy around it, the use cases that are
- 11:58considered really aren't generally functional programming use cases.
- 12:01You know, when the syntax is designed, when the various different use cases are considered,
- 12:06it's mostly thinking about and considering object-oriented use cases.
- 12:10And that's fine.
- 12:11Python's a great object-oriented programming language.
- 12:13But it means that a lot of the functional programming use cases will get left behind.
- 12:18So you have things like Python's Lambda, which is notoriously ugly, as well as functools.partial,
- 12:24which is difficult to use.
- 12:27You get a lot of things that don't really...
- 12:29Features you would really like to see from a functional standpoint that don't exist at all.
- 12:32Features like tail call optimization or tail recursion elimination.
- 12:35Features like pattern matching.
- 12:37And features like immutable algebraic data types.
- 12:40A lot of that stuff is missing.
- 12:42Right.
- 12:43The very dynamic nature of Python does make it a little bit hard to have immutable data types,
- 12:49for sure.
- 12:50And yeah, its primary thing is not in its functional style.
- 12:54I would say whatever it is, maybe it's OOP, maybe it's not quite, maybe it's something slightly
- 13:00different.
- 13:00But functional is not its primary focus.
- 13:02Do you think that the adoption of things like Scheme and Lisp and Haskell and whatnot is somewhat
- 13:12limited by its pureness?
- 13:14I would say that's definitely true.
- 13:16Because it really is much more difficult to try to learn a pure functional programming language
- 13:23than it is to try to get...
- 13:24Especially if you're coming from an imperative, non-functional background.
- 13:28Than it is to try to go with a language that is partially functional.
- 13:33Haskell is great, but getting started with Haskell isn't easy.
- 13:38There's a lot to learn.
- 13:39There's a lot of new concepts that you may not be familiar with, even if you've programmed
- 13:43for a long time in other programming languages.
- 13:45And so that really does create a barrier to entry.
- 13:48I guess so.
- 13:48It definitely seems like it would be a challenge to me.
- 13:51So you decided to add one more language to this list of this spectrum of functional programming
- 13:58languages.
- 13:58Yeah.
- 14:00So what's this project called Coconut?
- 14:01Yes.
- 14:02So that's true.
- 14:03So Coconut is a functional programming language that compiles to Python.
- 14:08So some of the basic design decisions of Coconut.
- 14:11Coconut syntax is a strict superset of Python 3 syntax.
- 14:15So Coconut takes Python 3 and adds on top of that new features for functional programming.
- 14:21That includes things like algebraic data types, tail call optimization, pattern matching.
- 14:26And then Coconut takes all of that and compiles it to universal version independent Python.
- 14:34Yeah, the version independent part is pretty awesome.
- 14:36Well, and it's version independent in certain ways, right?
- 14:39Like the syntax that you write into your Coco files, that's Python 3, right?
- 14:45Exactly.
- 14:46So Coconut syntax is based on Python 3.
- 14:49It's a strict superset of Python 3.
- 14:50But then that gets compiled to Python code that is compatible with 2.6, 2.7, 3.2, and above.
- 14:58Right.
- 14:58So the execution engine can be either Python 2 or Python 3, but you input Python 3 or pure
- 15:05Coconut, right?
- 15:06Yes, that's exactly correct.
- 15:07Coconut code you input is a strict superset of Python 3.
- 15:11But the compiled output is universal or you can specifically target one Python version or
- 15:16another.
- 15:16Okay.
- 15:17There are some cases where you are forced to target a Python version.
- 15:20One example of this is if you wanted to do something like 3.5 matrix multiplication.
- 15:23There's no way to backport that to Python 2.
- 15:27And so you're going to have to pass a flag that tells the compiler, I only care about 3.5.
- 15:32Right.
- 15:32Okay.
- 15:32Yeah.
- 15:33It's really interesting the way it works.
- 15:35I mean, the analogies that come to mind are like TypeScript, right?
- 15:40TypeScript is a superset of JavaScript, compiles of pure JavaScript.
- 15:43C++ on top of C, right?
- 15:46Not so much things like CoffeeScript where it's entirely a different language.
- 15:51It just happens to transpile into a thing that you might know like JavaScript or something.
- 15:57So I think that's going to make it much easier to adopt, right?
- 16:00Like if I've got some program that's Python 3 and I want to make it more functional, I could
- 16:07just put a different file extension, the .coco on it, and change it where it matters, right?
- 16:14Yeah, that's exactly it.
- 16:15So that was a purposeful design decision on my part from the very beginning.
- 16:20One of the really core things that I, like I was saying earlier, learned from Rabbit was
- 16:26I want to make sure to have a coherent story for how people are going to adopt this, why
- 16:33people are going to use this, and how even just as an existing Python developer, I can make
- 16:38something that is useful to you.
- 16:39And one of the ways, one of the really fundamental ways that I try to achieve that is via this,
- 16:45we're going to make the whole syntax a superset of Python 3.
- 16:49That way, you already know, if you know Python 3, most of the core syntax of the language.
- 16:55You don't have to learn anything new to be able to do things you already know how to do.
- 17:00The only things that you get to learn new are new features, new things that you can add
- 17:06to your repertoire.
- 17:07So the point is not to relax, reduce the amount that your Python developer is capable of,
- 17:14but only to expand and to allow you to be able to do new things in Coconut that you wouldn't
- 17:19otherwise be able to.
- 17:20That's really cool.
- 17:21And I can tell you from going through the tutorial, there's some really mind-bending stuff in there
- 17:25that we're going to get to.
- 17:26It's pretty awesome.
- 17:27So before we get into the details, why do you choose Coconut as a name?
- 17:32Oh, as a name.
- 17:34So that's a reference to Monty Python and the Holy Grail.
- 17:38And the Knights Insane and those guys are clicking along?
- 17:40Yeah, they've got the follow-behind, the Knights of the Round Table, smashing the coconuts together.
- 17:46So, yep.
- 17:47That's awesome.
- 17:47Yeah, yeah.
- 17:48If I was going to guess, that's what I thought.
- 17:50But yeah, perfect, perfect.
- 17:51It's really funny that a lot of people think Python is some kind of snake reference.
- 17:55And of course, it's not, right?
- 17:57Yeah.
- 17:58Hey, everyone.
- 18:00Michael here.
- 18:01Let me take just a moment and thank one of our sponsors who makes this show possible.
- 18:04This portion of Talk Python To Me has been brought to you by Rollbar.
- 18:07One of the frustrating things about being a developer is dealing with errors.
- 18:11Ah, relying on users to report errors, digging through log files, trying to debug them, or a million alerts just flooding your inbox and ruining your day.
- 18:19With Rollbar's full-stack error monitoring, you get the context, insight, and control you need to find and fix bugs faster.
- 18:25Adding the Rollbar Python SDK is just as easy as pip install Rollbar.
- 18:30You can start tracking production errors and deployments in eight minutes or less.
- 18:34Rollbar works with all the major languages and frameworks, including the Python ones like Django, Flask, Pyramid, as well as Ruby, .NET, Node, iOS, and Android.
- 18:43You can integrate Rollbar into your existing workflow, send error alerts to Slack or HipChat, or automatically create new JIRA issues, pivotal tracker issues, and a lot more.
- 18:52They have a special offer for Talk Python To Me listeners.
- 18:54Visit talkpython.fm/rollbar, sign up, and get the bootstrap plan free for 90 days.
- 19:00That's 100,000 errors tracked for free.
- 19:02But, you know, just between you and me, I hope you don't encounter that many errors.
- 19:06Give Rollbar a try today.
- 19:08Just go to talkpython.fm/rollbar.
- 19:12Yeah, so you covered some of the reasons why you built it, but, like, maybe give us some of the technical underpinnings of, like, why do we need this new language instead of just different PEP practices or whatever to work in this way?
- 19:25Sure.
- 19:26So, I think Coconut really does add a lot of very fundamental changes to Python syntax.
- 19:33So, it's very hard to do a lot of that stuff via just, like, the PEP process.
- 19:37I don't even think that that would necessarily be a good thing if sometime along the road all of that, all of Coconut syntax is implemented in Python.
- 19:45It really is trying to be a separate language because it adds a lot of new stuff on top of Python that is specifically for being able to do a lot of these more advanced functional things to add to some of the more basic things that you can do in Python.
- 20:02As well, I would say that as far as, like, why use Python, why get into it, the more technical underpinnings as well.
- 20:11First, Coconut really is built to be useful to existing Python programmers.
- 20:16You should be able to just pick it up, write a Coconut file, and immediately, like, as you work your way through the tutorial, as you work with the documentation, see new things that you can just add in to your existing knowledge of Python just to make your code more powerful, to make it so that you have new access to new features, as well as make the version compatibility story really easy for you.
- 20:36Yeah, and I really like the fact that you take one input language or variation, Python 3, but then it becomes execution independent.
- 20:47Yeah, exactly.
- 20:48One of the really nice things about compiling to Python source code is that it really is truly independent.
- 20:53You can run Python 3 even without adding any new Coconut features, or if you do add new Coconut features, run it through Coconut, get a compiled source that, because it's still Python, you can even run on PyPy, you can run on Jython or IronPy.
- 21:06You don't even need to, like, if you compile the CPython bytecode, for example, then it'd be sort of very restricted, but it really is just pure Python, and so wherever you want to be able to use Coconut constructs, you can.
- 21:19Yeah, that's cool. So right now the compiler, or transpiler, or whatever you want to think of it as, takes Cocoa syntax and compiles it to Python syntax that is very careful to work on both versions of the language.
- 21:35Is there a chance that you could change that compiler, extend it, make it compiler to things like, could I compile it to JavaScript? Could I compile it to .NET Intermediate Language, or Java file, Java bytecode, or things like this?
- 21:49There are no plans to do something like that right now. That would be a pretty massive undertaking.
- 21:56But certainly possible, I suppose, sometime very far down the road, I could see Coconut going that direction.
- 22:03I'm not proposing that it should, I was just kind of curious what your thoughts on that were.
- 22:08I think that for a lot of that stuff, there are a lot of utilities that already exist for taking Python code and making it available in other frameworks and other places.
- 22:17And Coconut can run right on top of those things, because it just produces Python source code.
- 22:21Right, so you really want to just stay focused on enhancing the functional programming story within the Python space purely.
- 22:27Right. Coconut has a pretty singular mission that is really focused on, I'm going to make Coconut be able to allow you to do functional programming really well in Python.
- 22:36You don't have to deal with any of the weird syntax ugliness, any of the missing features.
- 22:43Everything's going to work, you're going to be able to just take all of these powerful functional programming features and just run it in Python.
- 22:48Nice.
- 22:49So, what if I don't know functional programming, right?
- 22:53Maybe I'm self-taught and I've learned Python and whatever.
- 22:56Should I bother to learn or play with Coconut?
- 22:59What do you think?
- 23:01So, I mean, I would simply definitely say yes.
- 23:03My argument would actually be that Coconut's a really nice way to learn functional programming,
- 23:08because it means that you can start with everything you know about Python already being applicable.
- 23:14So, you don't have to, like if you try to learn functional programming through Haskell, for example,
- 23:19you have to rebuild up all of that knowledge about programming in general
- 23:25and all of the different ways to perform all of the different operations.
- 23:29Whereas with Coconut, all of that basic syntax is just Python.
- 23:33And it's only building functional stuff on top of that that you have to learn.
- 23:38And so, I think that trying to learn Coconut really is a good way to start getting into different functional programming features.
- 23:46And the tutorial for Coconut is, I think, a great way to do that.
- 23:49Yeah, that's really cool.
- 23:51It's definitely a way to expand your mind of what's possible.
- 23:53So, how do you install it?
- 23:57Is there like some download I got to do?
- 23:59Or what's the story?
- 24:00Pip install Coconut.
- 24:01That's it.
- 24:02Beautiful.
- 24:03It's the way it should be, right?
- 24:04And then, yeah.
- 24:05So, once you pip install Coconut, you have a new CLI.
- 24:09You just say Coconut thing and then stuff happens, right?
- 24:12Yep.
- 24:12Most basic command line interface is just Coconut name of a .coco file.
- 24:17And it'll compile that to a Python file.
- 24:19There's, of course, more complicated stuff than that.
- 24:21There's lots of command line flags and whatnot.
- 24:22But the most basic syntax is just that.
- 24:25Yeah, nice.
- 24:25So, you basically run it through this compile step.
- 24:28And then, you've got a set of Python files laying there.
- 24:30And you just execute them like you would anything, right?
- 24:33Any Python program.
- 24:34Coconut can compile and execute if you want it to take care of both of the steps.
- 24:39If you pass the minus R flag.
- 24:40But it can take care of a lot of different things.
- 24:43It's got a lot of different features.
- 24:44But most fundamentally, it's a compiler, transpiler, whatever you want to say.
- 24:47You give it Coconut code, it turns it into Python code.
- 24:50Sure.
- 24:50Okay.
- 24:50So, one of the pieces of syntax.
- 24:52Like, it's a little.
- 24:53It's beyond a little.
- 24:54It is hard to talk about syntax in audio format.
- 24:59So, I don't want to do it too much.
- 25:00But I want to touch just a little bit on a few things.
- 25:03Because a little bit, I think, will give people a strong idea.
- 25:08So, one of the things that's kind of annoying is if I want to take one function and pass its value to the next function and pass its return value to the next function.
- 25:17I basically have to do that inside out.
- 25:19The innermost.
- 25:21If I want to do it in one expression, not multi-line, right?
- 25:23I take the first function.
- 25:25Then I wrap it.
- 25:26And I call it the second function.
- 25:28I take that and wrap it in another one.
- 25:29And so, you don't read it normally.
- 25:32You read it, like, inside out, which is not great.
- 25:35So, you have this syntax in Cocoa and Coconut that says, left or right, take this and flow it to the, you know, almost like a Unix pipe sort of thing, right?
- 25:46Yeah.
- 25:47Pipeline operators in Coconut are borrowed from F Sharp.
- 25:51They exist in a lot of other languages as well, like Elm.
- 25:55But, yeah, the basic idea is it's a much more intuitive way to read and think about function calls.
- 26:03It really is one of the more syntactic, sugary features.
- 26:07It's not like something like pattern matching or tail call optimization that is sort of a more powerful thing behind the scenes.
- 26:13But I really do think that it's a good example of a way in which just from the most basic level before you even start delving into the more complex functional programming features, it just immediately makes your code easier to read, easier to understand, easier to follow.
- 26:28Because, like you were saying, that is how we more intuitively, natively think about functions is you take some value and you apply a transformation to it.
- 26:38And then you get something new and then you apply another transformation to it.
- 26:41And when you write it with that syntax, that's exactly the way that it looks.
- 26:44And so I do really think that it does make it look nicer.
- 26:48Yeah, it's pretty interesting.
- 26:49Let's talk about some of the other features.
- 26:52Some of them are pretty straightforward.
- 26:54So you have an alternate Lambda syntax.
- 26:57Yes.
- 26:58Give us a sense of what that is.
- 26:59And also, why did you create that?
- 27:01It's not that different.
- 27:02No.
- 27:03So the alternate Lambda syntax is just an arrow.
- 27:05It's the hyphen and then the greater than sign, which is already used in Python 3, for example, for the return type annotations.
- 27:14But the basic idea and operator of these, because another programming language is you use Lambdas a lot in functional programming.
- 27:22And the word Lambda is somewhat ungainly.
- 27:25You don't really want to have to write it everywhere.
- 27:28Of course, you still can in Coconut because it's a strict superset of Python 3.
- 27:31But it gives you this new syntax that lets you work with it more nicely and make it easier to read.
- 27:37Again, another one of the more syntactic sugary features.
- 27:40Coconut does have a lot of nice sugar to play with.
- 27:43Yeah, and there are some special cases where there's like super short versions of these Lambda expressions where they basically expand out in the arguments, right?
- 27:54Like X comma Y.
- 27:55Do like star.
- 27:57Star in parentheses.
- 27:58And then it'll like do like an expanding multiplication or something like that, right?
- 28:03Yeah.
- 28:03I think you're referring to a couple of different things.
- 28:05It's the sort of star in parentheses.
- 28:06That's a syntax that is somewhat borrowed from Haskell.
- 28:10But you can get an operator function, which in Python you would have to do normally via import operator and then like operator.add or something.
- 28:17You just put the operator in parentheses, which is another nice syntactic sugary thing.
- 28:22As well as you can then like compose that with other functions via function composition operator.
- 28:29Partially apply that via coconut's partial application operator.
- 28:33And so you can work with these operator functions, work with the built-in functions to build up sort of more complex composite functions out of those in a very functional manner.
- 28:45Yeah, that's really cool.
- 28:46All right, let's dig into some of the things that are like farther out there, let's say.
- 28:51Let's start with algebraic data types.
- 28:54Those seem pretty easy to describe on air.
- 28:58All right, I'll give it a shot.
- 28:59So algebraic data types are fundamentally, first of all, immutable.
- 29:04And so that means that they've just got, they're sort of like classes, but they've just got that one constructor at the very beginning.
- 29:11You pass in the data that's going to be inside of it.
- 29:13And then it's done.
- 29:15You have a new data type.
- 29:17Sort of like a tuple.
- 29:19Yeah, to me it looks like you take the class word and you change it to data.
- 29:22And then it has a sort of convention for the initialization because it can't be changed once it's initialized, right?
- 29:29Yep, yep.
- 29:30You change class to data and then you put the arguments to the constructor effectively, the different things that will be inside of it as instead of the like inheritance right there in the syntax.
- 29:40But then there's a lot of things that are more complex, more beyond just the fact that, okay, it's an immutable data type and it's got this syntax.
- 29:47Some of the things that are really cool that you can do with algebraic data types are, first of all, is pattern matching.
- 29:52You can pattern match against an algebraic data type because you can say in the pattern, like, for example, let's say I have a two vector that I have defined as an algebraic data type.
- 30:03And then I want to write a function that just gives me that first element.
- 30:07If it's a two vector and it's got an x position and a y position.
- 30:10And I can pattern match and I can say, like, two vector of x comma y in my function definitions.
- 30:17Like, f of def, like, def f of two vector of x comma y.
- 30:22And then it'll actually match any incoming data that is this two vector, decompose it into the components based on that pattern.
- 30:31Say, okay, this value matches the pattern.
- 30:34And I know from the pattern I see that there's this x and this y and I need to bind to the argument, the values inside of the algebraic data type.
- 30:44And it deconstructs it into its component parts in the pattern.
- 30:48And then you can take out and work with those components.
- 30:51So the pattern matching component of algebraic data types is really powerful, really useful stuff.
- 30:56There's some other cool stuff you can do with algebraic data types, too.
- 31:00Coconut provides an fmap function that lets you map over the contents inside the algebraic data type to produce a new algebraic data type.
- 31:07And there's a bunch of other stuff as well along those lines that you can do with algebraic data types that are more powerful than just, okay, it's immutable.
- 31:14But it is immutable.
- 31:15And that is important.
- 31:16Right.
- 31:17And do you use something like slots or something internally and properties to say there's a property with the name of the thing you added, but in fact, it's like a hidden field?
- 31:27How do you make it immutable?
- 31:29Internally, slots and named tuple are very sort of heavily relied upon.
- 31:35There's some other magic that has to go on sometimes.
- 31:40If you are somewhat creative with the way you define the constructor, like if you use a star, for example.
- 31:49But in general, yeah, it uses named tuple and slots pretty heavily behind the scenes.
- 31:53Yeah, interesting.
- 31:54Maybe tell people what slots are.
- 31:56I know some people know, but not everybody.
- 31:58And it's kind of an under-advertised feature of classes in Python.
- 32:03Not that you should use it often, but maybe you should know about it, right?
- 32:06Happily, yeah.
- 32:07So slots, specifically, it's dunder slots.
- 32:11It's a very useful attribute that you can set on any class.
- 32:14And when you set dunder slots to be some container, like I set dunder slots to be a tuple of some strings, then each of those strings, those become the only valid attributes for that class from that point.
- 32:28So they're the only instance attributes that you can have.
- 32:31And so for every instance that you make of that class, only the things in dunder slots are allowed to be changed, modified, reassigned, added to it.
- 32:40Even in __init__(), in your own class, you can't add another thing if it isn't pre-specified in slots.
- 32:45So that's a good way to get a lot of constraints for immutability.
- 32:50There's also some interesting memory performance trade-offs that you get with that as well.
- 32:55But let's not get sidetracked.
- 32:57You talked about algebraic data types and how they support pattern matching.
- 33:03Let's talk about pattern matching.
- 33:04There's like a couple ways which you have this concept, which honestly I've never really done much with.
- 33:12I know scheme, but that's about it for functional languages.
- 33:15So tell us what pattern matching is and like how it works.
- 33:20Yeah, so I sort of introduced this a little bit when I was talking about pattern matching against algebraic data type.
- 33:26But in its general form, pattern matching says, I have a specific type of data that I'm interested in working on in this function.
- 33:34So I want to maybe define a function specifically for dictionaries that have the form where one of the keys corresponds to a list of these parts.
- 33:45And that's pretty common where you have functions that operate on specific types of data.
- 33:49And pattern matching lets you define the specific type of data that you want the function to operate on.
- 33:55And then it lets you deconstruct the parts of that match that you actually want to use in the body of that function.
- 34:03It gives you bindings to them.
- 34:05And then it also lets you, in Coconut you can use addPattern for this, lets you dispatch to multiple functions based on which patterns match.
- 34:13So you can say, for this type of data that matches this pattern, I want to do this thing.
- 34:18But for this other type of data that matches this pattern, I want to do this thing instead.
- 34:22So pattern matching is a way to both check against your data as well as deconstruct it and make assignments to it.
- 34:30Yeah, one of the things I like in the function part of it, not necessarily the sort of error handling or case matching component,
- 34:39is what might have been one big function with many if statements now becomes separate functions, one for each particular case, right?
- 34:50So if you have an example with a factorial and you say, literally the syntax is def factorial of zero equals one.
- 34:57What the heck does that mean?
- 34:59Yes, so that is valid, Coconut.
- 35:01So what's happening there is you are defining a factorial function and you are saying the argument is the pattern zero.
- 35:09Zero is a valid pattern and it means it matches only the very specific data that is the number zero.
- 35:16And then the equals, that's just a little bit of syntactic sugar or an implicit return.
- 35:21And so the last expression after an equals in a function definition is automatically returned.
- 35:27Yeah, it doesn't have to be static, right?
- 35:29It can be computed?
- 35:30Yeah, it's just whatever the last expression is that gets returned.
- 35:34Okay, cool.
- 35:35Cool.
- 35:35And then you have this add pattern decorator.
- 35:38And you can take and create another function called factorial.
- 35:42And instead of having like n for the number being passed in, you have n is int if n is greater than zero.
- 35:50All right, tell us what this does.
- 35:52Okay, yeah.
- 35:53So also valid Coconut.
- 35:54So what's going on there is you have a little slightly complex pattern.
- 36:00But that's still what that is, that f is int if n greater than zero.
- 36:04That's another pattern.
- 36:05And that's doing a couple of things.
- 36:06It's first n is int.
- 36:08That's a type check.
- 36:10That says I only want to match n if it's an integer.
- 36:14And then the if n greater than zero, that's a guard.
- 36:17And that says if my pattern matches, perform this extra check to make sure that it is greater than zero before going in it.
- 36:25And say that the pattern doesn't match if that extra check at the end fails.
- 36:29Yeah, that's really cool.
- 36:30So basically the way you implement the factorial is you've got this one more complicated one that we just talked about.
- 36:37And it just more or less calls itself, right?
- 36:40And over and over with one fewer values in there.
- 36:45And then eventually that pattern doesn't match.
- 36:48It falls back to the one that returns one.
- 36:49And that's like the degenerative case of the recursion.
- 36:52And boom, you're done, right?
- 36:54But there's no if statements.
- 36:55You've written factorial with no if statements recursively.
- 36:59Yep, that's exactly correct.
- 37:00You know, that's actually, that reminds me of being confused in college.
- 37:04I remember I'd kind of played around with languages like C++ and stuff.
- 37:10And then I took the scheme course, a derivative of the list.
- 37:14And there were a lot of things that were like, okay, we're going to implement this with no if statements.
- 37:22Or, you know, the scheme itself doesn't even have like a looping construct in the language.
- 37:30There's no loops.
- 37:31So loops are actually done with recursion and these sorts of crazy things, right?
- 37:35And I just remember, wait, do this with no if statements?
- 37:38That's just kind of crazy, man.
- 37:40That can't work, can it?
- 37:41Right.
- 37:42Well, so as a functional programming guy, that doesn't worry me as much.
- 37:46Though it doesn't have to, even if it does worry you, that's not a problem in Coconut.
- 37:49Because you've still got your loops there.
- 37:51You've still got your if statements there.
- 37:53But you get to use all of these other very powerful features that sort of can in some ways replace a lot of that as well.
- 38:02And so it's, I hope, more friendly than trying to jump headfirst at the scheme.
- 38:08Yeah, you know, it wasn't that it worried me so much as it just kind of blew my mind.
- 38:13Like, I didn't think of programming.
- 38:15Like, I had this one view of programming, which was very imperative.
- 38:19And, you know, looping and ifs and whatnot and conditionals.
- 38:23And then they're just like, no, there's this completely different way of writing code.
- 38:27And I think that's one of the values of even if you don't ever want to use Coconut, I would encourage people to go through the tutorial.
- 38:34Because it's quite involved.
- 38:36And you really do.
- 38:37You do get to think or, let's say, challenge your own paradigms of programming, right?
- 38:43Yes, definitely.
- 38:44If you functional programming really is a new, interesting, powerful paradigm.
- 38:49That, if you haven't seen it before, really is nothing like what exists in a lot of other programming.
- 38:56Yeah, I feel like functional programming is one of those languages that you read more and you write less.
- 39:01And by that I mean you look at it and you go, okay.
- 39:05I know it's two lines, but they're very deep.
- 39:08I'm going to have to contemplate them for a little bit, right?
- 39:10Do you find that?
- 39:12Yeah, it's definitely true where it's like you can be very succinct and to the point in functional programming.
- 39:18Because, like, you know, in the factorial example, it lets you define a lot of the logic without being as, without having to go through the motions of how you would actually implement it.
- 39:28It lets you just say, factorial of zero is one, you figure out how to actually check to see if the value is zero and then return the right thing.
- 39:37I'm going to tell you what you need to know, the most basic information, which is zero should map to one, and you can take care of the rest.
- 39:45And that's sort of how functional programming works in a lot of different cases.
- 39:49And it's that nature of it that is one of the things that makes it so powerful.
- 39:53This portion of Talk Python To Me is brought to you by Hired.
- 39:57Hired is the platform for top Python developer jobs.
- 40:00Create your profile and instantly get access to thousands of companies who will compete to work with you.
- 40:05Take it from one of Hired's users who recently got a job and said,
- 40:09I had my first offer within four days and I ended up getting eight offers in total.
- 40:13I've worked with recruiters in the past, but they were pretty hit and miss.
- 40:16I tried LinkedIn, but I found Hired to be the best.
- 40:19I really like knowing the salary up front and privacy was also a huge seller for me.
- 40:23Well, that sounds pretty awesome, doesn't it?
- 40:25But wait until you hear about the signing bonus.
- 40:27Everyone who accepts a job from Hired gets a $300 signing bonus.
- 40:30And as Talk Python listeners, it gets even sweeter.
- 40:33Use the link talkpython.fm/hired and Hired will double the signing bonus to $600.
- 40:39Opportunity is knocking.
- 40:41Visit talkpython.fm/hired and answer the door.
- 40:45So let's talk about some of the other features.
- 40:47Another one, if you haven't done a lot of functional programming, you're probably not familiar with it.
- 40:53But one of the problems you can have with recursion is the call stack only has so much space on it, right?
- 41:00If you need to do what would effectively be many, many, many loops or very, very deep recursion, you can end up with an error, right?
- 41:09So you have a special way to solve this really cleanly.
- 41:12And I'm not sure I totally understand how you determine how to solve this.
- 41:16You have to tell me.
- 41:16All right.
- 41:18Tell me about your tail call optimization that you do.
- 41:21I'll go for it.
- 41:22Yeah.
- 41:22So in Python, that manifests via maximum recursion depth exceeded error.
- 41:27And what happens is every time Python sees a function call, it effectively creates a new stack frame.
- 41:34It says, you know, pops all the existing or pushes all the existing variables onto the stack and then enters this new frame with all of these new variables.
- 41:43And it does that execution.
- 41:44But what that means is that every time you call a new function, you're building up more memory, you're building up more function calls, and it can get very intensive doing that a lot.
- 41:53And so what Coconut does is it actually looks at every single function you write, and it says at any point, if you directly return a call to another function,
- 42:04then we don't actually need to keep around the stack frame that corresponds to your current function call.
- 42:11Because all of the data that is necessary to figure out what the return statement, what the actual return value of that function is going to be,
- 42:19is all contained just in this other function call.
- 42:22Because you've directly returned that function call.
- 42:25And so whenever Coconut sees something of that form, it automatically optimizes it away to get rid of the necessity of, like, making, adding this new stack frame.
- 42:36There's actually two ways in which Coconut does that for two different cases.
- 42:40The most extreme, most powerful case is Taylor-Cursion elimination.
- 42:43If you write a function that returns a call to itself, then you can refactor that into a wall loop, which is really nice and very fast.
- 42:52If it returns a call to another function, that's a little bit slower, but you can still prevent the new stack frame from being created.
- 43:00But it's a slightly more complex, more involved mechanism.
- 43:05Yeah, it works really nicely.
- 43:06And with the pattern matching, with the tail recursion optimization and stuff like that, one thing that I really liked was I was playing with the different versions of your implementation.
- 43:20And the pure Python, or nearly pure Python version, was slower than the highly Coconut style, the ones that leverages these special features the most.
- 43:35And so I thought that was pretty cool, actually.
- 43:37That it's not like, well, you get this better syntax, but you're going to suffer for it.
- 43:42But who cares?
- 43:43You don't need that much performance anyway.
- 43:45That you actually didn't, at least didn't pay a price for using it, but it seems like it actually might even be less of a price than not.
- 43:51Yeah.
- 43:52So in most cases, Coconut's performance is going to be very similar to Python's because, of course, it compiles to Python.
- 43:58But yeah, in some cases, Coconut really can perform optimizations that will save you.
- 44:04One of the biggest of those optimizations is the tail recursion elimination, tail call optimization, where it'll take a look at every function you write and just see, are you returning a call to another function directly?
- 44:15Implicitly or explicitly in any way?
- 44:17And if so, that gets optimized away always.
- 44:20And that's really nice for adding, like you were saying.
- 44:24It means that in some cases, Coconut code actually can be significantly faster than the equivalent Python code.
- 44:30In most cases, at the very least, it's usually not slower just because it really is Python.
- 44:36It just compiles to it.
- 44:37Yeah, that's really cool.
- 44:38And you could, of course, write those optimizations yourself because after all this does compile a Python, you could write what the compiler wrote.
- 44:44But that's a much harder deal, right?
- 44:46The goal is to have super simple code that is actually optimized.
- 44:50Yeah, exactly.
- 44:51The nice thing about the compiler, you can look through the compiled code.
- 44:56It'll just be a file right there.
- 44:58But you'll see that a lot of it really does get very complex, very hairy, very, especially with pattern matching.
- 45:05A lot of it gets really weird and hard to read because pattern matching can get very complex very quickly.
- 45:12And having a compiler figure out the sort of ways to implement all of that for you is really nice.
- 45:19Yeah, the way I think of it is people are very bad at writing state machines.
- 45:22Compilers are really good at it.
- 45:24Sometimes you end up with stuff like that, right?
- 45:27So speaking of debugging and trying to understand, like the thing you're going to actually debug when you run a debugger, be that some CLI one or something like PyCharm, you're going to see the transpiled compiled output, right?
- 45:43So how do I know if that maps back?
- 45:45Like, where does this map back to this funky syntax that I maybe have in my Cocoa file?
- 45:50Yeah, so that's an absolutely great question.
- 45:52Debugging is, of course, a huge, massive part of what every programmer does all the time.
- 45:57So Coconut has a really clean, simple story for that.
- 46:01So you can pass the line numbers flag, which is just minus L.
- 46:06And what that does is it appends to every line in the compiled Python a comment that says, take a look at this line in the source code that produced this line in the compiled code.
- 46:19Then you do pdb.setTrace, you get an error, and you see the lines that that error is referring to, and you'll see on those lines comments that are telling you what line in your source code caused that exception.
- 46:33What line in your source code are you tracing through?
- 46:36All of that stuff you can see, and you can just look at that line number, you can go right back to the source code without having to really worry about and deal with the compiled code at all.
- 46:46So that way, when you're debugging, you don't have to do any context switching.
- 46:49There's also another option, the keep lines option, which actually, instead of a line number, will just put the entirety of the line, and it'll just paste it right in there for you as well.
- 47:00So that's another option.
- 47:01Yeah, those are both pretty good stories.
- 47:02That's cool.
- 47:03So what about parallelization primitives?
- 47:06Coconut tries to make parallelization as easy as possible because you can have all of these nice immutable data structures, and ideally, you want to be able to use them in a nice way.
- 47:16Of course, in CPython, at least, Python fights back against you a little bit there.
- 47:20You've got the global interpreter lock.
- 47:21But what you can do, what Coconut provides, is two primitives.
- 47:26There's a parallel map and a concurrent map, which are multi-processed and multi-threaded, respectively, which take the most basic of the functional programmer's operations, and the one that is most easily parallelizable, the map operation, which just says apply this same thing, the same function, to every element.
- 47:45of this sequence, this data.
- 47:48And it allows you to really, really easily do that operation automatically in multiple processes or in multiple threads if you're doing I/O bound stuff such that you don't have to mess with any fancy libraries.
- 48:00You don't have to mess with anything fancy whatsoever.
- 48:02You just swap out wherever you're doing the map operation, that really parallelizable operation with the parallel version, and you're automatically getting all of the benefits of running that in parallel.
- 48:12And, of course, behind the scenes, it's using multi-processing and it's pickling everything.
- 48:16But yeah, that's really cool.
- 48:17So basically, it's still a blocking function as far as your code is concerned.
- 48:21But the internal representation, maybe it changes the order in which it processes them and it just fans it out.
- 48:28Right.
- 48:28All right.
- 48:29Awesome.
- 48:30And then one final feature we should talk about maybe is partial application.
- 48:35Is this your dollar operator?
- 48:38Yes, but the dollar operator is a little bit more than that.
- 48:41So the dollar operator in Python in Coconut just means lazify this.
- 48:45It means take this operation in Python and make it lazy instead of doing the Python thing.
- 48:52And so that means different things in different contexts.
- 48:54But there are sort of two.
- 48:55There are two contexts where it's used.
- 48:57So the big one.
- 48:58Yeah, you put it right before a function call.
- 48:59And that means instead of actually doing this function call, I want you to do it lazily.
- 49:03And don't do it until I actually tell you I want it.
- 49:06And so that is partial application.
- 49:08That says, here are the arguments that I'm going to pass to this function when I actually call it.
- 49:12But I might pass more arguments in later.
- 49:15And so for right now, you can just throw these arguments in and hold them there.
- 49:18And that's that partial application.
- 49:19The other usage of the dollar sign is for indexing, slicing with brackets.
- 49:25And in that case, it's a lazy form of that as well that doesn't actually perform the operation.
- 49:31And until you ask for the data, like an iterator, and lets you do it on arbitrary iterators.
- 49:37And so you can, for example, slice, do sort of complex slicing operations on iterators instead of just on lists or tuples, which lets you make use of the laziness of iterators in a much more effective way.
- 49:51Yeah, that sounds really pretty awesome.
- 49:53You definitely have some cool uses and examples in your tutorial.
- 49:58So one of the things that you talked about in the tutorial is the integration or the capability to use Jupyter or IPython notebooks.
- 50:08And so when I was playing your project, I'm like, oh, I was just at PyCon.
- 50:13I just saw Jake VanderPlas' really cool science-y keynote and got me excited about Jupyter.
- 50:20And I've, of course, played with it before, but, you know, not a lot.
- 50:23And I'm like, oh, I'll just explore your library through Jupyter.
- 50:26And that was really fun.
- 50:27So you have pretty good support for that, right?
- 50:28Yep. So Coconut has support for Jupyter through both a kernel, which means instead of running your code through IPython, you just select Coconut instead.
- 50:40And it works exactly like IPython, including, and this is actually somewhat of a new feature, including support for things like IPython magics and all of the sort of nice, fancy stuff that IPython throws in there.
- 50:53And then sort of on top of that, you have support for all of the Coconut utilities, all the Coconut language features as well.
- 51:03And so it's basically just like an IPython notebook, but instead of writing Python, you're writing Coconut.
- 51:07If you only want to do a little bit of Coconut, you can also use Coconut as an extension inside of IPython.
- 51:13And then it'll just take all of Coconut's built-ins, give you access to those.
- 51:18And whenever you want to use Coconut-specific syntax, then you just do the align or block magic specifically just to run the Coconut code.
- 51:25That looks really cool.
- 51:26And of course, data science type of work is a place where functional programming would really be applicable, right?
- 51:35Absolutely, yeah.
- 51:37The stuff that people do in data science and machine learning, it really does apply to the functional paradigm really well.
- 51:45Because a lot of that stuff is, first of all, it's data transformation.
- 51:49Instead of like a lot of the very heavily stateful stuff that happens in other areas of programming,
- 51:55a lot of that stuff, I would argue, still can benefit from a lot of the features of functional programming.
- 51:59When you're doing something that really is fundamentally, it's just got the one data that you're passing along.
- 52:04I've got, this is the data that I collected.
- 52:06I want to do some statistical transformations on it.
- 52:09I want to get insight into what it's like.
- 52:11What you're doing is you're passing it through transformations at each point where the functions themselves are sort of stateless transformations.
- 52:18And that is exactly the paradigm, exactly the approach that functional programming is built for.
- 52:25So it really does lend itself well to being able to make use of a lot of these constructs from functional programming.
- 52:33Especially like easily parallelization, if you just want a down and dirty, really easy way to just like process this data faster.
- 52:41And a lot of the coconuts, very powerful utilities for working with iterators so that you can construct your data and work with it in a lazy, lazy fashion.
- 52:51So you only have to deal with what you really need.
- 52:52Right.
- 52:53When you have billions of rows, but you only want to find a few of them.
- 52:56Lazy is good.
- 52:57Yes, exactly.
- 52:58Another area it sounds like this might be really helpful is in finance.
- 53:01I feel like that functional programming gets applied in that area a lot as well.
- 53:06Yeah, OCaml is really big there, for example.
- 53:09And so that's totally true.
- 53:11It's another area where you really want to be able to do a lot of the transformations on the data to be able to see what's happening.
- 53:20As well as one of the nice things about using immutable data structures is you can always really easily just take a snapshot.
- 53:25You're just like, okay, give me whatever the immutable data structure was at that point.
- 53:30And you know once you have a reference to it, it's never going to change.
- 53:33And so it's very easy to, like if you're doing finance, you want to see, like, okay, what was it like at this point?
- 53:39You can really easily just store references to immutable data structures because you know once you've got a reference to it, it'll never change.
- 53:44That's really interesting.
- 53:45Yeah, that's cool.
- 53:46Yeah, so that's another feature that is really useful, particularly in finance.
- 53:49Where else do you see functional programming really, people benefiting from adopting it?
- 53:54I'm a believer in functional programming being very broadly applicable in almost all cases.
- 54:00Like, you know, I said I worked at Yelp.
- 54:02And while I was there, one of the things that I did in my spare time was I, you know, went through some of the code base and tried to rewrite some of the things in Coconut to try to get it working.
- 54:11And even in, like, like a web application, even in something that doesn't seem functional at all, a lot of those constructs, just making, being able to make use of them gives you a lot more power and being able to define the logic in a simpler, easier manner that I think is really basically always applicable.
- 54:31Yeah, awesome.
- 54:33If I go to PyPI and randomly grab a package, could I use it in a Coconut app?
- 54:39Like, are there restrictions where this doesn't work?
- 54:41Yes, absolutely.
- 54:42In fact, you could use it regardless of whether that package was built for Python 2 or Python 3, because Coconut, of course, can compile to code that works on either version.
- 54:51But yeah, because Coconut compiles just to Python, anything that you would be able to access, anything you would be able to do from Python, you can do from Coconut.
- 55:00So that includes importing any package that you want to import.
- 55:03Also, the vice versa is true as well.
- 55:05If you want to, from Python code, import and make use of anything written in Coconut code, that is also really easy to do, you should, because the Coconut code ends up as Python code.
- 55:17And so it's really easy to import that as well.
- 55:18You basically ship the compiled Python files as the deliverable, right?
- 55:22Very cool.
- 55:23I think we're getting close to the end of the show, so we should probably start to wrap it up.
- 55:28I'm going to ask you about your favorite editor.
- 55:30But before we do, let me ask you an editor question about Coconut.
- 55:33So one of the things that I really like about editors is when they remind me what features are on a class or in a module and tell me what I have it right or wrong or give me auto-completion and things like that.
- 55:47Like, do I give that up if I take on Coconut?
- 55:50So that's a good question.
- 55:51So the answer is we're working on it.
- 55:53So right now, you can.
- 55:55There is one method that I have tested that does work with that.
- 55:58If you use Jupyter console, I know, and I think the notebook as well, you can get the Coconut's Jupyter or IPython kernel to do some auto-completions for you.
- 56:09I think, as far as I'm aware, that's the only one we've got so far.
- 56:14But I'm working on it.
- 56:15That's definitely, you know, there's other people that's working on it as well.
- 56:18That's definitely something that should be coming out in a more broader way pretty soon.
- 56:24I'm going to try to add it into Coconut's just basic command line interpreter as well.
- 56:29Yeah, it would be great.
- 56:31I mean, it's already really cool.
- 56:33But it would be even more cool if it could help us along the way, right?
- 56:37If you could get into Sublime, VS Code, PyCharm, those sorts of things, right?
- 56:43Yep.
- 56:43Definitely a cool feature.
- 56:44Being worked on for right now, try using the Jupyter IPython.
- 56:49Nice.
- 56:50Okay.
- 56:50And so then let me ask you, if you're going to write some Coconut or Python code, what editor do you open up?
- 56:57So I use Sublime.
- 56:58I'm a Sublime fanatic.
- 57:00I love Sublime.
- 57:01I use Sublime for everything.
- 57:03I write my school notes in Sublime.
- 57:05There are now a bunch of highlighters for Coconut, but the Sublime one, that was the one that I wrote.
- 57:10That was the sort of first one to be made.
- 57:13I'm the one that maintains that.
- 57:14I, of course, use that whenever I write Coconut code.
- 57:17So yeah, I love Sublime.
- 57:19That's definitely my editor of choice.
- 57:21Nice.
- 57:21And so there's also many, many, many PyPI packages, over 100,000 now,
- 57:27which is totally awesome.
- 57:29And let me go ahead and throw out Coconut as one of the really cool packages there.
- 57:33But maybe give us another one.
- 57:35Yeah, yeah.
- 57:36As you were saying, pip install Coconut.
- 57:38But yeah, so my favorite PyPI package would definitely be PyParsing.
- 57:42So PyParsing is awesome.
- 57:44It's a really cool utility for being able to define very simple grammars and really complex grammars as well,
- 57:52just in pure Python and then parse against them.
- 57:55Coconut's compiler is pretty much entirely written in PyParsing.
- 58:00So you can tell that I really like it.
- 58:02It's really powerful.
- 58:03It works really well.
- 58:04Coconut pushes PyParsing almost to its limits in a lot of cases,
- 58:09where it's the point where I had to, you know, submit a bunch of issues in the development of Coconut to PyParsing.
- 58:13But all of that's been fixed.
- 58:15The newest version of PyParsing is awesome.
- 58:17It works really well.
- 58:18Would definitely recommend checking out PyParsing.
- 58:21I actually used it for another project while I was at Yelp,
- 58:24which allows you to automatically refactor code by defining PyParsing patterns.
- 58:31Yeah, it looks for like code debt and duplication type things, something like that.
- 58:35Yep, exactly.
- 58:36It lets you define patterns using PyParsing grammars,
- 58:40as well as a bunch of sort of other Undeat specific stuff.
- 58:43And then you can define a like replacement function,
- 58:47and it'll scan for the grammar and then replace it with that.
- 58:50And so you can use that to really easily refactor code.
- 58:52Cool.
- 58:53Yeah, that's a great recommendation.
- 58:53And I think it's really neat that your work actually expanded PyParsing a little bit.
- 59:00But also its existence made your work pretty much possible, right?
- 59:04Yeah, it's a symbiotic relationship.
- 59:06It definitely is.
- 59:07Definitely it is.
- 59:08Okay.
- 59:09So this brings us to the end of the show.
- 59:12You know, final call to action.
- 59:14You have quite a few people who've starred this and are playing with it on GitHub.
- 59:18Are you looking for people to kick the tires, to contribute?
- 59:22What's the story?
- 59:23So yeah, so Coconut is built on GitHub.
- 59:26Everything's on GitHub.
- 59:27Definitely, definitely check out the GitHub.
- 59:29Start on GitHub for sure.
- 59:31I would definitely recommend trying to at least take a look.
- 59:34Go through the tutorial.
- 59:36Take a look at the documentation.
- 59:37Take a look and see if, you know, maybe this is something that you could really make use of.
- 59:42I, of course, think it's really cool and that people can really benefit from it.
- 59:47And if you have any questions, if you want to look around, there's a lot of resources for that.
- 59:52You can check out Coconut's Gitter, which is a chat room where you can just ask questions.
- 59:57And then there's a bunch of Coconut developers there, including me, that can just answer questions, check that regularly.
- 1:00:04You can also check out all of the sort of documentation resources.
- 1:00:08There's a frequently asked questions, the thorough documentation for all of the specification on the features and the tutorial to sort of give you basic introduction.
- 1:00:17And then, of course, if you are interested in contributing to Coconut, I would love to have more people get involved, more people take a look.
- 1:00:25It's all open source.
- 1:00:27Just take a look at the issues, fork it, make changes, submit a pull request.
- 1:00:32If you want to ask any questions about it, if you're trying to get started, definitely goes back to the Gitter.
- 1:00:37Just ask around on Gitter and I'd be happy to help anybody out there.
- 1:00:42So, yeah.
- 1:00:42Yeah, yeah, awesome.
- 1:00:43And if people are thinking of contributing, like, what kind of crazy language do they have to learn to, like, work with this?
- 1:00:49Like, what's it implemented in?
- 1:00:50Python!
- 1:00:51Yeah, that's awesome.
- 1:00:53I noticed in the GitHub is to be believed, you know, click that little bar and I'll tell you the languages used.
- 1:00:58It's 99.7% Python and 0.3% makefile or something like that.
- 1:01:05Yes, there is one makefile.
- 1:01:07So, I hope you don't shy away too much from having to deal with that.
- 1:01:11But, yeah, no, it's all Python.
- 1:01:13Coconut, the compiler, just like the compiled source, is written in Python that is intended to be universal.
- 1:01:19So, it works on 2.6, 2.7, 3.2 and above.
- 1:01:22But, yeah, it's just all just Python, pure Python.
- 1:01:24All right.
- 1:01:25Well, this is definitely an interesting project you have going and I'm happy to see it succeeding.
- 1:01:29And thanks for sharing it with everyone.
- 1:01:31Absolutely.
- 1:01:31Thank you so much for having me.
- 1:01:33It was really fun to be here.
- 1:01:36This has been another episode of Talk Python To Me.
- 1:01:39Today's guest was Evan Hubinger.
- 1:01:41And this episode has been brought to you by Rollbar and Hired.
- 1:01:45Rollbar takes the pain out of errors.
- 1:01:48They give you the context and insight you need to quickly locate and fix errors that might have gone unnoticed until your users complain, of course.
- 1:01:56As Talk Python To Me listeners, track a ridiculous number of errors for free at rollbar.com slash Talk Python To Me.
- 1:02:03Hired wants to help you find your next big thing.
- 1:02:06Visit talkpython.fm/Hired to get five or more offers with salary and equity presented right up front and a special listener signing bonus of $600.
- 1:02:15Are you or your colleagues trying to learn Python?
- 1:02:18Well, be sure to visit training.talkpython.fm.
- 1:02:21We now have year-long course bundles and a couple of new classes released just this week.
- 1:02:27Have a look around.
- 1:02:28I'm sure you'll find a class you'll enjoy.
- 1:02:29Be sure to subscribe to the show.
- 1:02:32Open your favorite podcatcher and search for Python.
- 1:02:34We should be right at the top.
- 1:02:35You can also find the iTunes feed at /itunes, Google Play feed at /play, and direct RSS feed at /rss on talkpython.fm.
- 1:02:45Our theme music is Developers, Developers, Developers by Corey Smith, who goes by Smix.
- 1:02:50Corey just recently started selling his tracks on iTunes, so I recommend you check it out at talkpython.fm/music.
- 1:02:56You can browse his tracks he has for sale on iTunes and listen to the full-length version of the theme song.
- 1:03:02This is your host, Michael Kennedy.
- 1:03:04Thanks so much for listening.
- 1:03:05I really appreciate it.
- 1:03:06Smix, let's get out of here.
- 1:03:08Smix, let's get out of here.
- 1:03:08Stay tuned.
- 1:03:10I'll see you next time.
- 1:03:15Bye.
- 1:03:16I'll pass the mic back to who rocked his best.
- 1:03:18I'll pass the mic back to who rocked his best.
- 1:03:18I'll pass the mic back to who rocked his best.
- 1:03:30Bye.