Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ......

71
www.erlang-solutions.com Healthy side of functional programming Bartłomiej Górny [email protected]

Transcript of Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ......

Page 1: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

Healthy side of functional programming

Bartłomiej Gó[email protected]

Page 2: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

CODER’S HEALTH ISSUES

▸ Wrists▸ Spine▸ Eyes▸ stress

Page 3: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

CODER’S HEALTH ISSUES

▸ Wrists▸ Spine▸ Eyes▸ stress

Page 4: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

CODER’S HEALTH ISSUES

▸ Wrists▸ Spine▸ Eyes▸ stress }

Page 5: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

CODER’S HEALTH ISSUES

▸ Wrists▸ Spine▸ Eyes▸ stress } ▸ Efficient use of resources

Page 6: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

CODER’S HEALTH ISSUES

▸ Wrists▸ Spine▸ Eyes▸ stress } ▸ Efficient use of resources

▸ Brain organisation vs code structure

Page 7: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

WRISTS AND FINGERS

▸ RSI, CTS▸ Keyboards▸ Trackball▸ Less typing

Page 8: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

DOES FP MEAN LESS TYPING?

▸ QuickCheck implementations in various languages▹ (in thousands of lines)

Clojure 6.7

Haskell 9.6

C 12.2

C++ 14.2

Java 17.9

JavaScript 18.5

PHP 20.1

.NET 77.4

Page 9: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MAKING WORK EASIER

▸ Use brain efficiently▹ Reduce effort and stress

▸ What are we best at?

Page 10: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MAKING WORK EASIER

▸ Use brain efficiently▹ Reduce effort and stress

▸ What are we best at?

PATTERN RECOGNITION

Page 11: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MAKING WORK EASIER

▸ Use brain efficiently▹ Reduce effort and stress

▸ What are we best at?

PATTERN RECOGNITION

▸ Evolutionary optimisation▹ Finding animal tracks▹ Searching edible plants▹ Recognising expressions (hostile or friendly?)▹ Danger alerts

Page 12: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

PATTERN RECOGNITION

Can you spot what’s wrong with this picture?

How long did it take you?

Page 13: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

PATTERN RECOGNITION / MATCHING

Page 14: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

PATTERN RECOGNITION - OTHER EXAMPLES

▸ Chess▹ Analysis, strategic planning, forecasting▹ Recognising patterns on board▹ RPD (Recognition Primed Decisions)

Page 15: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

RECOGNITION PRIMED DECISIONS

▸ Based on matching input to an in-memory pattern▸ Done instantly without conscious thinking▸ Often based on episodic patterns

Page 16: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

RECOGNITION PRIMED DECISIONS

▸ Based on matching input to an in-memory pattern▸ Done instantly without conscious thinking▸ Often based on episodic patterns

▸ ...some call it “experience”

Page 17: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

IN SEARCH FOR THE RIGHT POSTURE

▸ ...and how FP can help with that

Page 18: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

SPINE - THEORY

Page 19: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

PRACTICE - COMMON TYPING POSITION

Page 20: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

COMMON CODE READING POSITION

Page 21: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

THINKING

Page 22: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING AND UNDERSTANDING

▸ We do much more reading than writing▹ Legacy code, morning bootstrap, code

reviews…▸ We have to understand the code

Page 23: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING AND UNDERSTANDING

▸ To understand = to build a concept network

fuel

people

wrooom

cargoes into does

carries

Page 24: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING AND UNDERSTANDING

▸ To understand = to build a concept network

fuel

people

wrooom

cargoes into does

carries

uses

Page 25: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING AND UNDERSTANDING

▸ To understand = to build a concept network

▸ Thinking is (re)building concept network

fuel

people

wrooom

cargoes into does

carries

uses

Page 26: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

WHERE DO CONCEPTS COME FROM?

▸ Brain processes a stream of input▸ Concepts must be:

▹ Isolated▹ Formulated▹ stored

Page 27: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MEMORY STRUCTURE

▸ Working memory▹ “Input buffer”▹ High throughput▹ Low capacity▹ Volatile

▸ Associative memory▹ Low throughput▹ Unknown (infinite?) capacity▹ persistent

Page 28: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MEMORY STRUCTURE

workingmemory

/dev/null

associativememory

Page 29: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MEMORY STRUCTURE

workingmemory

/dev/null

associativememory

Page 30: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MEMORY STRUCTURE

workingmemory

/dev/null

associativememory

Page 31: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

MEMORY STRUCTURE

workingmemory

/dev/null

associativememory

Page 32: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS

75

Page 33: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS

75

Page 34: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

Page 35: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

687583861

Page 36: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

Page 37: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

68 75 8 386 1

Page 38: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

68 75 8 386 1

Page 39: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

68 75 8 386 1

Page 40: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

68 75 8 386 1

Page 41: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

68 75 8 386 1

Page 42: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

ASSOCIATIONS IN ACTION

▸ Memorising a phone number (a tragic story)

Page 43: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

FROM INPUT TO CONCEPT

workingmemory

/dev/null

associativememory

Page 44: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

FROM INPUT TO CONCEPT

workingmemory

/dev/null

associativememory

3

Page 45: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

FROM INPUT TO CONCEPT

workingmemory

/dev/null

associativememory

3 8

Page 46: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

FROM INPUT TO CONCEPT

workingmemory

/dev/null

associativememory

3 8 6

Page 47: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

FROM INPUT TO CONCEPT

workingmemory

/dev/null

associativememory

3 8 6

Page 48: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

FROM INPUT TO CONCEPT

workingmemory

/dev/null

associativememory

Page 49: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

Page 50: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

Page 51: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

Page 52: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

Page 53: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

Page 54: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

Page 55: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

Page 56: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

▸ FP is explicit about concepts▹ Isolated▹ Formulated▹ Easy to associate

Page 57: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

READING CODE, REVISITED

▸ FP is explicit about concepts▹ Isolated▹ Formulated▹ Easy to associate

▸ Understanding▹ Thinking = building a concept network

Page 58: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

UNDERSTANDING THE CODE

Page 59: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

FUNCTIONAL PROGRAMMING

▸ Better aligned with human brain

Page 60: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

“Coughs and stops. My theory is that a seal got stuck.”

Page 61: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

“And you call it a theory?!”

Page 62: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

“Theory is a collection of axioms, rules of inferences and theorems derived from them. Theory is a system, not some stupid guesswork.”

Page 63: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

LATER:“He thinks he's so smart, while he can't tell theory from hypothesis.”

Page 64: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

IN SEARCH FOR A PROOF

▸ Meta-research▸ Experiment

Page 65: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

IN SEARCH FOR A PROOF

▸ Meta-research▸ Experiment▸ Which language is most readable for you?

Page 66: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

IN SEARCH FOR A PROOF

▸ Meta-research▸ Experiment▸ Which language is most readable for you?▸ Idea I: multi-language assignment

▹ Reading+coding task in language to choose from▹ Measure average performance and stress level

Page 67: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

IN SEARCH FOR A PROOF

▸ Meta-research▸ Experiment▸ Which language is most readable for you?▸ Idea I: multi-language assignment

▹ Reading+coding task in language to choose from▹ Measure average performance and stress level

▸ Idea II: natural language comparison▹ Natural language instruction▹ Written imperatively or functionally

Page 68: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

IN SEARCH FOR A PROOF

Place the steak between two sheets of heavy plastic (resealable freezer bags work well) on a solid, level surface. Firmly pound the beef with the smooth side of a meat mallet to a thickness of 1/8 inch. Combine the olive oil, 2 tablespoons of cilantro, cumin, oregano, 1 pinch of cayenne in a large glass or ceramic bowl; season to taste with salt and pepper. Add the beef and toss to evenly coat. Cover the bowl with plastic wrap, and marinate in the refrigerator for 30 minutes.

Page 69: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

IN SEARCH FOR A PROOF

Place the steak between two sheets of heavy plastic (resealable freezer bags work well) on a solid, level surface. Firmly pound the beef with the smooth side of a meat mallet to a thickness of 1/8 inch. Combine the olive oil, 2 tablespoons of cilantro, cumin, oregano, 1 pinch of cayenne in a large glass or ceramic bowl; season to taste with salt and pepper. Add the beef and toss to evenly coat. Cover the bowl with plastic wrap, and marinate in the refrigerator for 30 minutes.

(Prepare steak)

Prepare steak: (wrap beef) (pound beef) (mix sauce)

Pound beef: beat it with the smooth side of a meat mallet to a thickness of 1/8 inch

Mix sauce: (get ingredients) mix in a glass bowl add salt and pepper

Page 70: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

PERSONALITY

▸ Why do people prefer a certain programming style?▹ Accident?▹ Personality traits?▹ Attitudes?

▸ Requires an extensive research▹ Comparative▹ respondents?

Page 71: Healthy side of functional programming...JavaScript 18.5 PHP 20.1.NET 77.4. MAKING WORK EASIER ... Done instantly without conscious thinking ... Written imperatively or functionally.

www.erlang-solutions.com

THANK YOU FOR ATTENTION