35
Oh-Oh to Functional Introduction to Functional T hinking [email protected] @nashjain http://nareshjain.com

Oh Oh To Functional

Embed Size (px)

DESCRIPTION

Given a problem, most of us have a decent idea of how to model the problem in an Object-Oriented world. We can use principles like SOLID, GRASP, "Keep It DRY, Shy, and Tell the Other Guy" and so on to guide our design. In my experience, if you apply these principles recursively, (esp. on certain problems) you will notice, that you start drifting towards functional style of programming. For example, your object evolve to have single functions and gradually you can make the classes immutable. This leads to pure function without side-effects. At that point, you can turn them into closures (a poor man's object.) Eventually this leads to higher-order functions, which can be used as first class-citizens. Further more, you can express your domain behavior in terms of generic functions like map and reduce, which can be chained or composed to really simplify your logic and make your design more communicative. At this stage, you can really feel the difference between a declarative vs. imperative style of programming. If time permits, we can also look at how to apply concepts like partial function application, currying, lazy evaluation and infinite data-structures to tackle complexity and design for scalability. If this sounds interesting, join us for a code-along session, where we'll take a problem and incrementally evolve the design from OO to more of a functional style. Key Take-ways: 1. Introduction to Functional thinking 2. Demystify some of the core Functional Programming concepts and jargons 3. Understand how to evolve to a Functional style and its advantages Agenda: * Object-Oriented Design Principles (SOLID, DRY) * Functional Concepts ** Single Abstract Method (SAM) ** Pure Functions ** Immutability ** High-Order Functions ** Lambda Expressions ** Lazy evaluation ** Functional Composition ** Declarative vs. Imperative programming style * Quick Demo - Java 8 Functional Features

Citation preview

Page 1: Oh Oh To Functional

Oh-Oh to FunctionalIntroduction to Functional Thinking

[email protected]@nashjain

http://nareshjain.com

Page 2: Oh Oh To Functional

AgendaObject-Oriented Design Principles (SOLID, DRY)

Functional Concepts

Single Abstract Method (SAM) Pure Functions Immutability High-Order Functions Lambda Expressions Lazy evaluation Functional Composition Declarative vs. Imperative programming style

Quick Demo - Java 8 Functional Features

Page 3: Oh Oh To Functional

Commercial Break!

Page 4: Oh Oh To Functional

http://agilefaqs.com | Copyright © 2014, AgileFAQs. All Rights Reserved.

Page 5: Oh Oh To Functional

Mumbai

Page 6: Oh Oh To Functional
Page 7: Oh Oh To Functional
Page 8: Oh Oh To Functional
Page 9: Oh Oh To Functional

Tech Talks!

Page 10: Oh Oh To Functional
Page 12: Oh Oh To Functional

2

4

5 1

2

6 4

xⁿ

57

11

Page 13: Oh Oh To Functional

Story #1

As a Kid learning arithmetics,

I want to identify even numbers

So that I can grow my understanding of odd and even numbers.

Page 14: Oh Oh To Functional

Acceptance Criteria#1

Given numbers 1, 2, 3, 4, 5 and 6

When using the Odd filter and emptying contents of the source bucket to target bucket

Then target bucket should contain 2, 4 and 6.

#2 Given numbers -2 and 3

When using the odd filter and emptying contents of the source bucket to target bucket

Then target bucket should contain -2.

Page 15: Oh Oh To Functional

1

6

2

4

5

3 7

xⁿ

9

Page 16: Oh Oh To Functional

Story #2

As a Kid learning arithmetics,

I want to identify prime numbers

So that I can differentiate between composite and prime numbers.

Page 17: Oh Oh To Functional

Acceptance Criteria#1

Given numbers 2, 4, 6, 7, 9 and 11

When using Prime filter and emptying contents of the source bucket to target bucket

Then target bucket should contain 2, 7 and 11.

#2 Given numbers -1, -2, 3, and 4

When using Prime filter and emptying contents of the source bucket to target bucket

Then target bucket should contain 3.

Page 18: Oh Oh To Functional

1

11

2

4

5

-3 4

xⁿ

9

7

Page 19: Oh Oh To Functional

Story #3

As a Kid learning arithmetics,

I want to identify odd primes

So that I can understand numbers that are odd and primes at the same time.

Page 20: Oh Oh To Functional

Acceptance CriteriaGiven numbers 2, 4, 5, 6 and 7

When using the Odd and prime filters and emptying contents of the source bucket to target bucket

Then target bucket should contain 5 and 7.

Page 21: Oh Oh To Functional

2

4

5 1

2

6 4

xⁿ

57

11

Page 22: Oh Oh To Functional

Story #4

As a Kid learning arithmetics,

I want to identify numbers within a range

So that I can grow my understanding of ranges.

Page 23: Oh Oh To Functional

Acceptance CriteriaGiven there are numbers 5, 7, 10 and 15

When using the range between 5..10 and emptying contents of the source bucket to target bucket

Then target bucket should contain 5, 7 and 10.

Page 24: Oh Oh To Functional

2

4

5

3

8

1 7

xⁿ

9

Page 25: Oh Oh To Functional

Story #5

As a Kid learning arithmetics,

I want to sum all the numbers that are in the bucket.

So that I can learn addition.

Page 26: Oh Oh To Functional

Acceptance Criteria

Given numbers 5, 7, 10 and 5

When using the aggregator and emptying contents of the source bucket to target bucket

Then target bucket should contain 27.

Page 27: Oh Oh To Functional

2

4

5 1

2

6 4

xⁿ

12

11

Page 28: Oh Oh To Functional

Story #6

As a Kid learning arithmetics,

I want to multiply all numbers in a bucket.

So that I can grow my understanding of numbers.

Page 29: Oh Oh To Functional

Acceptance Criteria

Given numbers 2, 4 and 6

When using the multiplier and emptying contents of the source bucket to target bucket

Then target bucket should contain 48.

Page 30: Oh Oh To Functional

2

4

5 1

2

6 4

xⁿ

48

11

*

Page 31: Oh Oh To Functional

Story #7

As a Kid learning arithmetics,

I want to sum all odd primes within a range.

So that I can grow my understanding of numbers.

Page 32: Oh Oh To Functional

Acceptance Criteria

Given numbers 1, 2, 5, 7, 8, 10 and 15

When using the range 2..10 and emptying contents of the source bucket to target bucket

Then target bucket should contain 12.

Page 33: Oh Oh To Functional

2

4

5 1

2

6 4

xⁿ

57

11

12

Page 34: Oh Oh To Functional

Functional Features in Java 8

Quick Demo