programming languages
play

Programming Languages Closures-ish in Python and C++ Higher-order - PowerPoint PPT Presentation

Programming Languages Closures-ish in Python and C++ Higher-order programming Higher-order programming, e.g., with map and filter , is great Language support for closures makes it very pleasant Without closures, we can still do it


  1. Programming Languages Closures-ish in Python and C++

  2. Higher-order programming • Higher-order programming, e.g., with map and filter , is great • Language support for closures makes it very pleasant • Without closures, we can still do it more manually / clumsily – In OOP (e.g., C++) with classes with private members – Python has closures, though they are slightly clunky • Working through this: – Shows connections between languages and features – Can help you understand closures and objects Fall 2011 CSE341: Programming Languages 2

  3. C++ • In the beginning, there was the function pointer. – Not a substitute for full-fledged closures, because a function pointer points to code only, no environment. Fall 2011 CSE341: Programming Languages 3

  4. C++ • In the beginning, there was the function pointer. – Not a substitute for full-fledged closures, because a function pointer points to code only, no environment. • Then came objects, capable of having "private data" by using private members. – People started using "function objects" by creating classes that overload the parentheses operator (yes, you can do that). Fall 2011 CSE341: Programming Languages 4

  5. C++ • In the beginning, there was the function pointer. – Not a substitute for full-fledged closures, because a function pointer points to code only, no environment. • Then came objects, capable of having "private data" by using private members. – People started using "function objects" by creating classes that overload the parentheses operator (yes, you can do that). • In 2011, the C++ standard was updated to include "function objects" as well as lambdas. – This is super-awesome and really cool, but not all compilers support everything yet. Fall 2011 CSE341: Programming Languages 5

  6. Lambda expressions in C++ [ capture option ] ( args ) { block of code } C++ will try to automatically figure out the return type for you, but if it can't, you can also do: [ capture option ] ( args ) -> returnType { block of code } Fall 2011 CSE341: Programming Languages 6

  7. Lambda expressions in C++ [ capture option ] ( args ) { block of code } The "capture option" tells C++ how to handle non-local variables in the block of code. [ ] means capture nothing (if you have no non-locals) [=] means capture non-locals by value (make a copy) [&] means capture non-locals by reference (don't make a copy) Fall 2011 CSE341: Programming Languages 7

  8. Map and filter • Part of the C++ algorithms library – map is called "transform" – many of these higher-order functions don't use vectors or arrays directly, but operate on "iterators." – An iterator is a data type that expresses the abstraction of a "sequence," whether that sequence is implemented as a vector, array, or looping through some other data structure – other things that have iterators • strings (sequence of characters) • keys in a hashtable Fall 2011 CSE341: Programming Languages 8

  9. Python • Python has fewer hoops to jump through because the language was designed with closures in mind (e.g., nested function defs are OK). – C++'s "closures" (which aren't really closures) had to be hacked in later. • Syntax is cleaner in Python as well because no capture by value/capture by reference, plus no data type declarations needed because it's a dynamically typed language. – Summary: syntax closer to Racket/Scheme's syntax. Fall 2011 CSE341: Programming Languages 9

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend