Bartosz Milewski Concurrency First class functions Generic programming Memory Management (move semantics) Math nomenclature Functor Applicative Functor.

Slides:



Advertisements
Similar presentations
CPS 235 Object Oriented Programming Paradigm
Advertisements

Functional Programming Lecture 10 - type checking.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
Exceptions, Templates, And The Standard Template Library (STL) Chapter 16.
Copyright © 2012 Pearson Education, Inc. Chapter 16: Exceptions, Templates, and the Standard Template Library (STL)
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
The Standard Template Library – part 2. auto_ptr Regular pointers may cause memory leaks Regular pointers may cause memory leaks void f() { SomeClass.
Grab Bag of Interesting Stuff. Topics Higher kinded types Files and handles IOError Arrays.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
OOP Project Develop an Application which incorporates the following OO Mechanisms and Principals: – Classes Visibility Constructor(s) Class Variable (if.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
C++ Training Datascope Lawrence D’Antonio Lecture 1 Quiz 1.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2015 © 1.
Data Structures Using C++ 2E
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Chapter 9: Functional Programming in a Typed Language.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 16: Exceptions,
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CS 403, Class 23Slide #1 CS Programming Languages Class 23 November 16, 2000.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions.
Grab Bag of Interesting Stuff. Higher Order types Type constructors are higher order since they take types as input and return types as output. Some type.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
CSCI 383 Object-Oriented Programming & Design Lecture 25 Martin van Bommel.
Functional Programming Lecture 3 - Lists Muffy Calder.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Overview of STL Function Objects
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Monads Technion – Institute of Technology Software Design (236700) Author: Gal Lalouche - Technion 2016 © 1.
ML: a quasi-functional language with strong typing
Operator overloading Conversions friend inline
Advanced C++ Programming
Operator overloading Conversions friend inline
Generic Programming Techniques in C++
Starting Out with C++ Early Objects Eighth Edition
PROGRAMMING IN HASKELL
CMSC 202 Templates.
CMSC 202 Lesson 22 Templates I.
PROGRAMMING IN HASKELL
Containers and the Standard Template Library (STL)
Exception Handling.
Exceptions, Templates, and the Standard Template Library (STL)
Grab Bag of Interesting Stuff
Templates I CMSC 202.
EE 312 Final Exam Review.
Standard Template Library
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Standard Template Library
Final Exam Review Inheritance Template Functions and Classes
Templates CMSC 202, Version 4/02.
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Review for Midterm 3.
Standard Template Library
Presentation transcript:

Bartosz Milewski

Concurrency First class functions Generic programming Memory Management (move semantics) Math nomenclature Functor Applicative Functor Monad Monoid

Sorting: compare function Find, copy_if: predicate function Accumulate: binary function for_each(v.begin(), v.end(), [](char c) { cout << c; }); transform(v.begin(), v.end(), w.begin(), [](int x) { return x * x; });

Currying, partial application: bind Combining algorithms v.erase(remove_if(v.begin(), v.end(), bind(logical_and (), bind(greater (), _1, -10), bind(less (), _1, 10))), v.end());

Channel for passing data (John Reppy, ML) Promise Future

Page 7 promise prms; Thread AThread B Promise Shared state Shared state

Shared state Shared state Page 8 promise prms; auto ftr = prms.get_future(); Thread AThread B Future Promise

Page 9 promise prms; auto ftr = prms.get_future(); thread th(&thFun, std::move(prms)); Thread AThread B Shared state Shared state Future Promise

Page 10 promise prms; auto ftr = prms.get_future(); thread th(&thFun, std::move(prms)); Thread AThread B Shared state Shared state Future Promise prms.set_value(Hello from future); Hello Thread B

Page 11 promise prms; auto ftr = prms.get_future(); thread th(&thFun, std::move(prms)); std::string str = ftr.get(); Thread AThread B Shared state Shared state Future Promise prms.set_value(Hello from future); Hello Thread B

Composability Orthogonality (Separation of concerns)

Problem: Apply a function to a future future ftr = async(…); … string s = ftr.get(); // blocks? … then continue to parse(s)

future ftr = async(…); string s = ftr.get(); // blocks? … then parse(s) template auto future::then(F&& func) -> future ; future fTree = ftr.then([](future fstr) { return parse(fstr.get()); // doesnt block }); Tree tree = fTree.get(); // blocks? future fTree = ftr.next(parse); Tree tree = fTree.get(); // blocks? Next combinator

next lifts parse to act on futures future fStr = … future fTree = fStr.next(parse); » transform lifts square to act on containers vector v = {1, 2, 3}; vector w; w.resize(v.size()); transform(v.begin(), v.end(), w.begin(), square);

Unconstrained parametric polymorphism (universally quantified types) For all types T: template class future; template class vector; template class unique_ptr; A mapping of types: T -> future

Type constructor Function lifting: then, transform, (Haskell: fmap) T -> future fuction -> function (future ));

Problem: Composing (chaining) async calls future async_open(string &); future async_read(HANDLE fh); In principle, this is the result: future > ffBuf = async_open("foo").next(&async_read);

Collapse two levels of future async_open("foo.cpp").next(&async_read).unwrap().n ext(&async_process).unwrap(); Combination of next and unwrap called bind (Haskell: >>=, bind combines join with fmap) In C++, next (then) can be overloaded to serve as bind

Usage: conditional asynchrony, recursion A future that is ready make_ready_future future fint = make_ready_future (42); int i = fint.get(); // doesnt block Analogously, for containers: vector v = {42};

Functor pattern Type constructor Function lifting (then, next, transform) Collapsing (unwrap, concat) Value lifting (make_ready_future)

Type constructor Value lifting: make_ready_future() bind: combination of.next(f).unwrap() [or an overload of next] Usage of the future monad pattern: Composing libraries of async functions

Its all in the wrist next (or bind) checks for exceptions and propagates them (without calling the continuation) At the end of the chain, recover from exception async_open("foo.cpp").next(&async_read).next(parse).r ecover(&on_error); Exception monad Implements short-circuiting Can be put on top of the future monad (monad transformers)

Problem: Need N futures to proceed. Create a barrier, get all values, proceed. when_all: takes futures, returns future of finished futures Client gets, iterates, gets each, and proceeds with values Functional approach Apply a regular function of n argument to n futures. Lifting of n-ary functions when_all_done(futures).next(fn) Together with make_ready_future: applicative functor

Problem: Wait for the first future to complete when_any: takes futures, returns a future of futures, at least one of them ready Client gets, iterates, checks is_ready, picks value. proceeds Functional approach The OR combinator (like addition?) Combines two futures into one Assoc.: (f1 OR f2) OR f3 = f1 OR (f2 OR f3) Neutral element: the never future never OR f = f = f OR never Defines a monoid

New patterns based on functional programming Functor Applicative Functor Monad Monoid Composability and orthogonality Result: Library of futures