Functional Programming Think Differently!! Functional languages are expressive, accomplishing tasks using short, succinct and readable code. Functional.

Slides:



Advertisements
Similar presentations
Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
Advertisements

Chapter 12 Lists and Iterators. List Its an abstract concept not a vector, array, or linked structure. Those are implementations of a List. A list is a.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Programming Languages and Paradigms
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Week 8 Arrays Part 2 String & Pointer
C++ Sets and Multisets Set containers automatically sort their elements automatically. Multisets allow duplication of elements whereas sets do not. Usually,
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
1 Chapter 11 Structured Types, Data Abstraction and Classes Dale/Weems/Headington.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Multiple-Subscripted Array
OOP Etgar 2008 – Recitation 101 Object Oriented Programming Etgar 2008 Recitation 10.
Computer Science II Exam I Review Monday, February 6, 2006.
Plab – Exercise 5 C++: references, operator overloading, friends.
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Basic Elements of C++ Chapter 2.
Programming is instructing a computer to perform a task for you with the help of a programming language.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
11 COS220 Concepts of PLs AUBG, COS dept Lecture 36 OOP The STL Standard Template Library Reference: MS Developer Studio, Visual C++, Lafore, Chap 15 STL,
Programming Language C++ Xulong Peng CSC415 Programming Languages.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
CPS120: Introduction to Computer Science Lecture 14 Functions.
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Data Collections: Lists CSC 161: The Art of Programming Prof. Henry Kautz 11/2/2009.
CSC Data Structures, Fall, 2008 Monday, September 29, end of week 5, Generic Functions and Classes in C++
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Overview of C++ Templates
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Operator Overloading. Binary operators Unary operators Conversion Operators –Proxy Classes bitset example Special operators –Indexing –Pre-post increment/decrement.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
CS201 – Introduction to Computing – Sabancı University 1 Built-in Arrays l C++ native array type (not the class version) l Two versions ä fixed size arrays.
1 CSE Programming in C++. 2 Overview Sign roster list Syllabus and Course Policies Introduction to C++ About Lab 1 Fill Questionnaire.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
CSIS 123A Lecture 7 Static variables, destructors, & namespaces.
Generic Programming and Library Design Brian Bartman
 Binary operators  Unary operators  Conversion Operators  Proxy Classes (simulating a reference) ▪ bitset example  Special operators  Indexing 
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Motivation for Generic Programming in C++
Chapter Topics The Basics of a C++ Program Data Types
Basic Elements of C++.
Pointers Psst… over there.
Basic Elements of C++ Chapter 2.
Pointers Psst… over there.
.NET and .NET Core 9. Towards Higher Order Pan Wuming 2017.
C++ Functions, Classes, and Templates
Pointers & Functions.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Predefined Functions Revisited
Pointers & Functions.
Lesson 25 Miscellaneous Topics
Class rational part2.
Introduction to Algorithms and Programming COMP151
Presentation transcript:

Functional Programming Think Differently!! Functional languages are expressive, accomplishing tasks using short, succinct and readable code. Functional languages provide richer ways for expressing abstractions: We can hide the HOW and just focus on the WHAT or RESULT 1-1

Real-world functional programming Modern programming languages like C# and C++ have incorporated functional programming paradigm. Generally this is done through libraries. It is not necessary to use a functional language to do functional programming. Although it’s much easier to do functional programming with a functional language. 1-2

Characteristics of Functional Programming Functions as first class objects Ability to pass functions around as parameters. No side effects Everything is done through function return values. Pass by value parameters only. Generally typeless, don’t have to declare the type of variables before using. Generally have implicit data structure support. You don’t have to mess with your own data structure.

Let’s get functional Monday: C++ algorithms that use functional style. Function objects Lambda expressions Tuesday: TR1: Functions Binders Lab in C++ Next Monday: Functional programming overview Intro to F#? Next Tuesday: Lab in F#?

Let’s get functional in C++ Characteristics of algorithms in C++ standard library: Functional style, generally don’t use explicit recursion or loops Implicit loop structure (for loop) Do something to each element of the vector Implicit data structure is a vector (array) In C++ standard library, there is a set of vector-based algorithms. You can think of a vector as an array. The algorithms just need a range (begin, end). Note where end is. begin end

for_each algorithm Needs #include for_each is in the std namespace. for_each takes a range of values specified with pointers or iterators. for_each takes a function pointer or a function object. for_each will apply the function to each element in the range of values.

for_each void print(int x) { cout << x; } void add5(int &x) { x = x+5; } int array1[5]={1, 2, 3, 4, 5}; for_each(array1, array1+5, add5); for_each(array1, array1+5, print);

Function Objects struct someFun{ void operator()(int& x) const { x = x+ 43; } }; int data[10]; someFun aFunObj; for_each(data, data+10, aFunObj); for_each(data, data+10, someFun());

accumulate Accumulate is in It has two forms: accumulate(begin, end,initValue); accumulate(begin, end,initValue, binaryFunc); The second form of accumulate is a little strange:

accumulate w/ function accumulate(beg, end, initValue, binaryFunc) initValue = binaryFunc(initValue, eachValueInRange) int data[10]; int result = accumulate(data, data+10, 1, binaryFunc); int binaryFunc (int sum, int value) { return sum * value; } data

Transform There are two forms of transform: transform(sourceBegin, sourceEnd, destination, unaryFunc); // apply unaryFunc to each element in source range, write results to // sequence starting at destination transform(source1Begin, source1End,source2Begin, destination, binaryFunc); // same as above, but work with two source ranges transform() can modify an existing sequence or can make a new one. To modify an existing sequence, make destination the same as sourceBegin.

Transform: Single Source int data[10]; int changeData(int &x) { return x*2; } transform(data, data+10, data, changeData);

Transform: Two Sources int data1[10]; int data2[10]; int data3[10]; int addThem(int x1, int x2) { return x1+x2; } transform(data1, data+10, data2,data3, addThem);

Transform: Two Sources w/ function object int data1[10]; int data2[10]; bool data3[10]; Struct magicSum { int sum; magicSum(int x): sum(x){} bool operator()(int x, int y) { return x+y == sum; } }; transfor m(data1, data+10, data2,data3, magicSum(37));