35
Vagif Abilov Playing functional Conway’s game of life

Vagif Abilov Playing functional Conway’s game of life

Embed Size (px)

Citation preview

Page 1: Vagif Abilov Playing functional Conway’s game of life

Vagif Abilov

Playing functionalConway’s game of life

Page 2: Vagif Abilov Playing functional Conway’s game of life

About myself

• Mail: [email protected]• Twitter: @ooobject• GitHub: object• BitBucket: object• Blog:

http://bloggingabout.net/blogs/vagif/default.aspx• Articles: http://www.codeproject.com

The source code for this presentation can be found at https://github.com/object/ConwayGame

Page 3: Vagif Abilov Playing functional Conway’s game of life

Conway’s game of life

• Invented in 1970 by the British mathematician John Conway

• Zero-player game, its evolution is fully determined by its initial state

• The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead

Source: Wikipedia

Page 4: Vagif Abilov Playing functional Conway’s game of life

Rules of Conway’s game of life

1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.

2. Any live cell with two or three live neighbours lives on to the next generation.

3. Any live cell with more than three live neighbours dies, as if by overcrowding.

4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Page 5: Vagif Abilov Playing functional Conway’s game of life

Impelemening Conway’s game

How would you approach the implementation?

A small quiz: what names should we introduce?

• Classes• Class properties• Class methods• Structs• Enums

Page 6: Vagif Abilov Playing functional Conway’s game of life

What people say on Twitter

“How is writing Java like writing classic Russian literature? You have to introduce 100 names before anything

can happen.”

@jamesiry

Page 7: Vagif Abilov Playing functional Conway’s game of life

Speaking about Russian literature

Nightingales, a sigh, a whisperIn a shady nookAnd the lullaby in silverOf a lazy brook.…

• A short poem by Afanasy Fet (1850)• 12 lines, not a single verb

Page 8: Vagif Abilov Playing functional Conway’s game of life

Checking other implementations

Source:http://www.codeproject.com/Articles/185208/Solving-Conway-s-Game-of-Life-using-State-Pattern

Page 9: Vagif Abilov Playing functional Conway’s game of life

Implementation code metrics

• 5 classes (including 2 subclasses)• 5 properties• 5 methods• 316 lines of code• 64 effective lines of code (calculated using VS

code metrics)

Page 10: Vagif Abilov Playing functional Conway’s game of life

Cell state definition

Discussion in comments about cell state definition

• Class (base class with 2 subclasses implementing State pattern)

• Enum (Dead, Alive)• Boolean

What would you choose?

Page 11: Vagif Abilov Playing functional Conway’s game of life

Cell state choice consequence

• No matter what type you choose to represent cell state, you will need a cell property to hold it

• Having cells with different property values (Dead and Alive) encourages design where both types of cells are stored

• Storing cells with different states has negative impact on the scalability

• Moreover: it limits the solution to boards of fixed size

• Adding names add constraints!

Page 12: Vagif Abilov Playing functional Conway’s game of life

Imperative languages lessons

• Extra definitions may better clarify your intentions but may also lead to increased system complexity

• Names are opinionated, more names – more opinions

• More names – more constraints• Design patterns are often introduced to patch up

shortcomings in the language

Page 13: Vagif Abilov Playing functional Conway’s game of life

Entering functional programming

Conway’s game of life is about transforming cell generations

What if we focus just on functional transformations?

Page 14: Vagif Abilov Playing functional Conway’s game of life

Implementation plan

1. Classical game of life2. Multidimensional game of life3. Concurrent game of life4. Game of life visualization5. Something completely different

Page 15: Vagif Abilov Playing functional Conway’s game of life

Classical game of life

Page 16: Vagif Abilov Playing functional Conway’s game of life

Observation

It’s possible to write clean code without class hierarchies

Page 17: Vagif Abilov Playing functional Conway’s game of life

Multidimensional game of life

Page 18: Vagif Abilov Playing functional Conway’s game of life

Observation

You don’t need to revise your code to make it generic:

with type inference it just happens to you

Page 19: Vagif Abilov Playing functional Conway’s game of life

Asynchronous game of life

Page 20: Vagif Abilov Playing functional Conway’s game of life

Asynchronous burden

• Asynchronous support requires revision of API and its implementation

• It is common to offer two (significantly different) versions of API: one for synchronous and one for asynchronous execution

• Mutable nature of imperative languages require major changes if not complete rewrite of components to become thread-safe and support asynchronous or parallel execution

Page 21: Vagif Abilov Playing functional Conway’s game of life

Asynchronous programming model

• Synchronous: – Execute (can be any verb, e.g. Read)

• Asynchronous:– BeginExecute (receives AsyncCallback, returns IAsyncResult)– EndExecute (receives IAsyncResult object)– <Callback> method (receives IAsyncResult)

• File IO example:– Read– BeginRead, EndRead, <Callback>

Page 22: Vagif Abilov Playing functional Conway’s game of life

Benchmark results

Pattern size*

C# Linq F# F# Async F# PSeq

10** 0:00.183 0:00.357 0:00.320 0:00.570

25 0:00.428 0:00.315 0:00.157 0:00.196

50 0:06.034 0:04.064 0:01.512 0:01.231

100 1:25.162 1:06.429 0:27.428 0:31.270

* Pattern size N corresponds to a square N * N used to generate a pattern of N * N / 2 elements

** Pattern size 10 was used to warm up the text fixture

Page 23: Vagif Abilov Playing functional Conway’s game of life

Observation

Immutable data structures make it trivial to execute parts of your code asynchronously or in

parallel

Page 24: Vagif Abilov Playing functional Conway’s game of life

Game of life visualization

Page 25: Vagif Abilov Playing functional Conway’s game of life

Observation

It’s simple to send your data to external components

when data are exposed via primitive types

Page 26: Vagif Abilov Playing functional Conway’s game of life

Time for surprises

Something completely different

Living with no sense of monad

Page 27: Vagif Abilov Playing functional Conway’s game of life

Song time!

Living With No Sense of Monads

Originally performed by Smokie with slightly different words

Page 28: Vagif Abilov Playing functional Conway’s game of life

Living with no sense of monads

I first met Alice in a small bookstore,"What book, - I asked, - are you looking for?"And she said: "About monads."

I understood that's a key to her heart,I had no doubts that I was smart.It should be easy reading, an easy start...

Page 29: Vagif Abilov Playing functional Conway’s game of life

Living with no sense of monads

But I still don't get this wisdom,It's really mystic wordI guess it has it's reasons,Someone gets it, but I don't.'Cos for twenty-four yearsI've been trying to understand the monads.

Page 30: Vagif Abilov Playing functional Conway’s game of life

Living with no sense of monads

Twenty-four yearsJust waiting for a chance,I have to keep on reading, Maybe get a second glance,I will never get used to not understanding sense of monads

Page 31: Vagif Abilov Playing functional Conway’s game of life

Living with no sense of monads

Grew up together,Went to the same class,And to be honest:I was better in mathThan Alice.

Page 32: Vagif Abilov Playing functional Conway’s game of life

Living with no sense of monads

Too old for rock-n-roll - I'm forty four,Too young for monads - still have a hope.That the day will come When I let Alice know...

Page 33: Vagif Abilov Playing functional Conway’s game of life

Living with no sense of monads

But I still don't get this wisdom,It's really mystic wordI guess it has it's reasons,Someone gets it, but I don't.'Cos for twenty-four yearsI've been trying to understand the monads.

(all together):Monads! What the f*ck is monads?!

Page 34: Vagif Abilov Playing functional Conway’s game of life

Living with no sense of monads

Twenty-four yearsJust waiting for a chance,I have to keep on reading, Maybe get a second glance,I will never get used to not understanding sense of monads

Page 35: Vagif Abilov Playing functional Conway’s game of life

Thank you!

• Mail: [email protected]• Twitter: @ooobject• GitHub: object• BitBucket: object• Blog:

http://bloggingabout.net/blogs/vagif/default.aspx• Articles: http://www.codeproject.com

The source code for this presentation can be found at https://github.com/object/ConwayGame