Oh Oh To Functional

Post on 04-Jul-2015

627 views 2 download

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

Transcript of Oh Oh To Functional

Oh-Oh to FunctionalIntroduction to Functional Thinking

naresh@agilefaqs.com@nashjain

http://nareshjain.com

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

Commercial Break!

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

Mumbai

Tech Talks!

MathWorks Game

dhaval@agilefaqs.com@softwareartisan

Courtesy

2

4

5 1

2

6 4

xⁿ

57

11

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.

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.

1

6

2

4

5

3 7

xⁿ

9

Story #2

As a Kid learning arithmetics,

I want to identify prime numbers

So that I can differentiate between composite and prime numbers.

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.

1

11

2

4

5

-3 4

xⁿ

9

7

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.

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.

2

4

5 1

2

6 4

xⁿ

57

11

Story #4

As a Kid learning arithmetics,

I want to identify numbers within a range

So that I can grow my understanding of ranges.

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.

2

4

5

3

8

1 7

xⁿ

9

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.

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.

2

4

5 1

2

6 4

xⁿ

12

11

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.

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.

2

4

5 1

2

6 4

xⁿ

48

11

*

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.

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.

2

4

5 1

2

6 4

xⁿ

57

11

12

Functional Features in Java 8

Quick Demo

Thank YouQuestions?

naresh@agilefaqs.com@nashjain