Refactoring to Functional. Hadi Hariri

Similar documents
JVM ByteCode Interpreter

Stop coding Pascal. Saturday, April 6, 13

Functional Programming

Introduction to Functional Programming and Haskell. Aden Seaman

Streams in Java 8. Start programming in a more functional style

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

Presented By Andrew Butt

Concepts of Programming Languages

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

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

INTRODUCTION TO HASKELL

Functional programming in C#

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Functional Programming and Haskell

3. Functional Programming. Oscar Nierstrasz

Robot Programming with Lisp

Functional Programming Lecture 13: FP in the Real World

Functional Programming Principles in Scala. Martin Odersky

SD314 Outils pour le Big Data

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

Functional Programming in C++

Functional Programming in C++

Functional Programming Lecture 1: Introduction

Haskell Refresher Informatics 2D

Chapter 11 :: Functional Languages

Programming Languages and Techniques (CIS120)

.consulting.solutions.partnership. Clojure by Example. A practical introduction to Clojure on the JVM

Declarative concurrency. March 3, 2014

Introduction to Functional Programming in Haskell 1 / 56

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

Lecture 8: Summary of Haskell course + Type Level Programming

CS457/557 Functional Languages

CSCC24 Functional Programming Scheme Part 2

in Microservices using DDD, Event Sourcing & CQRS Augusto Branquinho Mike Shigueru Matsumoto

Functional Programming for Imperative Programmers

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Functional Programming Patterns And Their Role Instructions

CS 11 Haskell track: lecture 1

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

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

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

Functional Logic Programming Language Curry

PL Categories: Functional PLs Introduction to Haskell Haskell: Functions

Functional Programming in Ruby

Imperative languages

Programming Language Pragmatics

Functional Programming. Big Picture. Design of Programming Languages

Programming Paradigms

Programming Systems in Artificial Intelligence Functional Programming

Concepts of program design Exam January 31, 13:30 16:30

Functional Programming

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

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

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

CPEG 852 Advanced Topics in Computing Systems The Dataflow Model of Computation

Chapter 15. Functional Programming Languages

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

Java SE 8 New Features

Functional Programming and the Web

Comp215: More Recursion

Haskell in the corporate environment. Jeff Polakow October 17, 2008

Lectures Basics in Procedural Programming: Machinery

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

Background Type Classes (1B) Young Won Lim 6/28/18

Programming Languages, Summary CSC419; Odelia Schwartz

Higher Order Perl. Finlay Thompson. 14 March 2006

Classical Themes of Computer Science

Functional Programming

CSCI-GA Scripting Languages

CS558 Programming Languages

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

CSCE 314 Programming Languages

CS 415 Midterm Exam Spring 2002

4. Functional Programming Language-Oriented Programming

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

Introduction. chapter Functions

Using Scala for building DSL s

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

Frege. purely functional programming on the JVM. JUG Luzern 2016

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

Contents. A Source Code 32 A.1 ASCII versions of mathematical symbols A.2 Definitions of used library functions... 32

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Shared state model. April 3, / 29

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Concepts behind the C# 3 language. Faculty of Mathematics and Physics, Charles University in Prague

Thoughts on Assignment 4 Haskell: Flow of Control

Functional Programming in Haskell for A level teachers

Functional Languages. Hwansoo Han

An Explicit-Continuation Metacircular Evaluator

FUNCTIONAL PROGRAMMING

With examples in F# and C# SAMPLE CHAPTER. Tomas Petricek. WITH Jon Skeet FOREWORD BY MADS TORGERSEN MANNING

Safe Smart Contract Programming with Scilla

Functional Programming Concepts for Data Processing

An introduction to C++ template programming

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Functional Programming

IA010: Principles of Programming Languages

Transcription:

Refactoring to Functional Hadi Hariri

Functional Programming In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions.

Let s refactor to functional

Wait. But why?

Goals Write less code. Write expressive code. Write correct code. Write performant code. (because Silicon Valley)

Be part of the in-crowd

Can I use any language?

What is a functional language?

A language with Functions as First-Class Citizens Higher-Order Functions Lambdas Top-level Functions* Immutable Data

I ll be using Kotlin. You can use Java 8, Java 7..5 +FJ, Scala, JavaScript, Clojure, Haskell, F#, C#, OCaml, Lisp.

Basic Function Syntax

Higher-Order Functions and Lambdas

Pure Functions Given same input, function always provides same output Execution does not cause observable side-effects Referential Transparency

Reduce moving parts

Aim for Pure Functions

Can we achieve our goals?

What s functional about that?

The Loops

Removing State

Some syntax sugar

Still emphasis on how, not what

Writing less code

Getting the data the way we need it

Transforming data

Again, let s write less

A more involved sample

The manual way

Once again, writing less code

Applying these techniques to common scenarios

Customer Search

Customer Search

Templates Patterns

Less boilerplate

Guaranteeing things take place

Guaranteeing things take place

Or when they take place

Strategy Pattern

Same strategy, less code

What about dependencies?

Let s think about dependencies in classes

When dependencies grow

How do we deal with dependencies?

We pass in the behaviour

Thinking Functionally

Functions are our primitives http://www.flickr.com/photos/mknowles/47457221/

We re merely combining functions

Functions taking functions filter - takes a predicate map - takes a mapping function groupby - takes a key lookup function

Functions returning functions

Pipelining

Keeping it readable Abstract multiple pipeline calls into descriptive functions Intermediate variables don t always help

How do we re-use?

Composing functions

Partial Function Applications

Partial Function Applications

Currying

Currying

And then there s the data

We re taking data and processing it in stages

What is our data? Customer Invoices Point Age

And what is this data? Scalars: Age, Date of Birth, Amount Collections of ATD s: List of Customers, List of Invoices, Set of Pairs

Take as input lists and output lists

Common List Operations map, flatmap, filter, findall, merge, zip..

And scalars can be obtained from lists

Common Scalar Operations first, last, find, aggregate..

Many functions boil down to folds fold, reduce, aggregate same thing.

What is fold? [ ] f(x,y) where x accumulator y element in list

What is fold? [ ] f(, ) =

What is fold? [ ] f(, ) =

What is fold? [ ] f(, ) =

What is fold? [ ] f(, ) =

What is fold? [ ] f(, ) =

What is fold? [ ] f(, ) =

Recursion, Pattern Matching and Folds

The Recursive Maximum

The Base is the accumulator

Immutability of the data

We need to avoid state Treat lists as infinite Allows for lazy evaluation, reactive programming Aim for immutability Create. Don t modify. There s a difference between ReadOnly and Immutable

What about Performance?

That Annoying Fibonacci

The Iterative Approach

Memoization

Recursion and Tail Calls

Converting to Tail Calls Memoization Tail Call Optimization

Tail Call Optimization

Inlining Higher-order functions can have performance impact Inlining functions can minimise performance impact

What about all that scary stuff?

Functors Collection of a whereby you can apply a function a -> b, returning collection of b map is a an example of Functor

Monads Abstract Data Type, following a series of rules Maybe Monad -> Option List Monad -> IEnumerable<T> (Iterable<T>) Promises

In Summary

Embrace Functions as Primitive Elements Focus on writing less your own code Focus on writing more descriptive code Focus on writing deterministic code Rocket Science

Recommended Books The Little Schemer Learn you a Haskell Functional Programming in Java

Thank you