Functional Programming Lecture 13: FP in the Real World

Similar documents
Functional Programming Lecture 1: Introduction

Functional Programming

Programming Paradigms

Anders Møller Michael Schwartzbach Erik Ernst Hans Hüttel

Robot Programming with Lisp

Refactoring to Functional. Hadi Hariri

Concepts of Programming Languages

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

Imperative languages

Functional programming in C#

Welcome to. Instructor Marc Pomplun CS 470/670. Introduction to Artificial Intelligence 1/26/2016. Spring Selectivity in Complex Scenes

Presented By Andrew Butt

Lecture 8: Summary of Haskell course + Type Level Programming

A Brief Introduction to Scheme (I)

Introduction to Functional Programming

JVM ByteCode Interpreter

Functional Programming Principles in Scala. Martin Odersky

CS457/557 Functional Languages

Functional Programming Patterns And Their Role Instructions

Introduction to Functional Programming in Haskell 1 / 56

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Chapter 11 :: Functional Languages

BCS THE CHARTERED INSTITUTE FOR IT. BCS Higher Education Qualifications BCS Level 6 Professional Graduate Diploma in IT EXAMINERS' REPORT

Lecture content. Course goals. Course Introduction. TDDA69 Data and Program Structure Introduction

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Introduction to Functional Programming and Haskell. Aden Seaman

SD314 Outils pour le Big Data

Programming Language Pragmatics

Concepts of Programming Languages

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

CS 314 Principles of Programming Languages

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

Programming Paradigms and Languages Introduction to Haskell. dr Robert Kowalczyk WMiI UŁ

Functional Languages. Hwansoo Han

3. Functional Programming. Oscar Nierstrasz

Is Functional Programming (FP) for me? ACCU Conference 2008 Hubert Matthews

Functional Programming

Scala : an LLVM-targeted Scala compiler

Functional Programming in Java. CSE 219 Department of Computer Science, Stony Brook University

CPS 506 Comparative Programming Languages. Programming Language Paradigm

New Programming Paradigms

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

All you need is fun. Cons T Åhs Keeper of The Code

Swift, functional programming, and does it matter? Alexis

CS 11 Haskell track: lecture 1

CS252 Advanced Programming Language Principles. Prof. Tom Austin San José State University Fall 2013

15. Functional Programming

Programming (Econometrics)

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Topic 1: Introduction

Programming Languages 2nd edition Tucker and Noonan"

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Chapter 15. Functional Programming Languages

Programming Systems in Artificial Intelligence Functional Programming

Functional Programming and Haskell

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Programming Languages and Techniques (CIS120)

Functional Programming

Weeks 6&7: Procedures and Parameter Passing

Programming Languages and Techniques (CIS120)

4. Functional Programming Language-Oriented Programming

Functional Programming in Haskell for A level teachers

Introduction to Concepts in Functional Programming. CS16: Introduction to Data Structures & Algorithms Spring 2017

Functional Programming Languages (FPL)

CSCE 314 Programming Languages

Stop coding Pascal. Saturday, April 6, 13

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Cloud Computing & Visualization

IA010: Principles of Programming Languages

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

Introduction to R programming a SciLife Lab course

Introduction to Functional Programming

8/27/17. CS-3304 Introduction. What will you learn? Semester Outline. Websites INTRODUCTION TO PROGRAMMING LANGUAGES

F# for Industrial Applications Worth a Try?

MF#K - Introduction to F# GroupM Mødegruppe for Ƒunktionelle Københavnere (MF#K)

ITT8060 Advanced Programming

Scripted Components: Problem. Scripted Components. Problems with Components. Single-Language Assumption. Dr. James A. Bednar

Scripted Components Dr. James A. Bednar

4/19/2018. Chapter 11 :: Functional Languages

Functional Programming. Big Picture. Design of Programming Languages

Programming Paradigms in Python

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Equivalent Notations. Higher-Order Functions. (define (f x y) ( body )) = (define f (lambda (x y) ) ) Anonymous Functions.

CS 415 Midterm Exam Spring 2002

CS558 Programming Languages

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

COS 326 Functional programming: an elegant weapon for the modern age

CS 320: Concepts of Programming Languages

Concepts of programming languages

An introduction introduction to functional functional programming programming using usin Haskell

DATA SCIENCE USING SPARK: AN INTRODUCTION

Frege. purely functional programming on the JVM. GOTO Berlin 2015

First Programming Language in CS Education The Arguments for Scala

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Functional Programming Invades Architecture. George Fairbanks SATURN May 2017

Course Syllabus. Programming Language Paradigms. Spring - DIS Copenhagen. Semester & Location: Elective Course - 3 credits.

Introduction to Prof. Clarkson Fall Today s music: Prelude from Final Fantasy VII by Nobuo Uematsu (remastered by Sean Schafianski)

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

CS558 Programming Languages

Concurrency: what, why, how

Transcription:

Functional Programming Lecture 13: FP in the Real World Viliam Lisý Artificial Intelligence Center Department of Computer Science FEE, Czech Technical University in Prague viliam.lisy@fel.cvut.cz 1

Mixed paradigm languages Functional programming is great easy parallelism and concurrency referential transparency, encapsulation compact declarative code Imperative programming is great more convenient I/O better performance in certain tasks There is no reason not to combine paradigms 2

3

Source: Wikipedia 4

Scala Quite popular with industry Multi-paradigm language simple parallelism/concurrency able to build enterprise solutions Runs on JVM 5

Scala vs. Haskell Adam Szlachta's slides 6

Is Java 8 a Functional Language? Based on: https://jlordiales.me/2014/11/01/overview-java-8/ Functional language first class functions higher order functions pure functions (referential transparency) recursion closures currying and partial application 7

First class functions Previously, you could pass only classes in Java File[] directories = new File(".").listFiles(new FileFilter() { @Override public boolean accept(file pathname) { return pathname.isdirectory(); } }); Java 8 has the concept of method reference File[] directories = new File(".").listFiles(File::isDirectory); 8

Lambdas Sometimes we want a single-purpose function File[] csvfiles = new File(".").listFiles(new FileFilter() { @Override public boolean accept(file pathname) { return pathname.getabsolutepath().endswith("csv"); } }); Java 8 has lambda functions for that File[] csvfiles = new File(".").listFiles(pathname -> pathname.getabsolutepath().endswith("csv")); 9

Streams We want a list of adult users grouped by sex public Map<Sex, List<User>> groupusers(list<user> allusers) { Map<Sex, List<User>> result = new HashMap<>(); for (User user : allusers) { if (user.getage() >= 18) { List<User> currentusers = result.get(user.getsex()); if (currentusers == null) { currentusers = new ArrayList<>(); result.put(user.getsex(),currentusers);} currentusers.add(user); }} return result;} 10

Streams In Java 8, we can use higher order functions public Map<Sex, List<User>> groupusers(list<user> allusers) { return allusers.stream().parallelstream().filter(user -> user.getage() >= 18).collect(groupingBy(User::getSex)); } Declarative style (and lazy) easier to understand easier to parallelize 11

Is Java 8 a Functional Language? Functional language first class functions higher order functions pure functions (referential transparency) recursion closures currying and partial application Yes Yes No, but it provides many of the nice FP features No No tail recursion optimization by default Only values, variables become final Yes 12

First class functions Higher order functions Lambda Closures List comprehensions Referential transparency Currying/partial application Data immutability Pattern matching Lazy evaluation FP aspect in mainstream languages Haskell + + + + + + + + + + Java 8 (+) + + +/- - - (+) (+) - (+) C++14 + + + + - - (+) (+) (+) (+) Python + + + + + - + (+) (+/-) (+) JavaScript + + + + + - + (+) (+/-) (+) MATLAB + + + + - - + (+) - (+) 13

Erlang Haskell complex types + concurrency support Immutable data Pattern matching Functional programming Distributed Fault-tolerant 14

Map Reduce Distributed parallel big data processing inspired by functional programming John Hughes's slides 16

Lisp for Scripting in SW Tools Emacs: extensible text editor AutoCAD: technical drawing software Gimp: gnu image manipulation program 17

Gimp User scripts in: ~/.gimp-2.8/scripts Register the function by script-fu-register script-fu-menu-register Filters Script-Fu Refresh Scripts See example source code in a separate file. (example form Gimp documentation) 18

TAKE-AWAYS FROM FP 19

Declarative programming write what should be done and leave how to the optimizer particularly interesting in distributed setting easier to understand, no need to go back from how to what 20

Minimizing Side Effects reusability predictability concurrency lower mental load (modularity/encapsulation) It is easier than it seems! 21

Immutability You can use it in any programming language to ease parallelization avoid defensive copying avoid bugs in hashmaps / sets consistent state even with exceptions allows easier caching It is not as inefficient as it seems! 22

Recursion Many problems are naturally recursive easier to understand / analyze less code, less bugs combines well with immutability A great universal tool 23

Exam Schedule 45 min test anything hard to evaluate by programming 15 min break 2h of programming at computers (>50% points) ~2 Haskell and ~2 Scheme tasks upload system, otherwise no internet school computers with Linux (tool requests?) Dates (tentative): 31.5. 9:00; 6.6. 9:00; 24