F# in Action: Playing Functional Conway's Game of Life
Upcoming SlideShare
Loading in...5
×
 

F# in Action: Playing Functional Conway's Game of Life

on

  • 1,992 views

F# in action: playing functional Conway's game of life ...

F# in action: playing functional Conway's game of life

John Conway's game of life has become a playground for programmers using different languages and platforms. It inspired many coding dojos and code retreats because it touches various aspects of development from component design to performance testing.

In this session we are going to take functional approach and play different variations of Conway's game of life using F#. The session starts with presentation of a traditional game implementation in C#, so we can later compare it with F# versions. Switching to F#, we will first write an implementation for a standard two-dimensional board, and then extend it to support asynchronous workflows, parallel tasks and even boards of arbitrary dimensions. Each implementation will be complemented with a set of tests using FsUnit framework, and graphical presentation of results will use Microsoft Chart Controls.

Statistics

Views

Total Views
1,992
Views on SlideShare
1,991
Embed Views
1

Actions

Likes
0
Downloads
5
Comments
0

1 Embed 1

https://twitter.com 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Popular choice: code retreats, coding dojos, pair programmingNext: job interview, define some names
  • Next: Russian literature
  • Next: implementation from CodeProject
  • Next: how would you define State property?
  • Why state pattern and not enumerators?“Enumerators are amateurish”Next: consequences and constraints
  • Next:imperative language sessions
  • Next : getting functional
  • Next: implementation plan

F# in Action: Playing Functional Conway's Game of Life F# in Action: Playing Functional Conway's Game of Life Presentation Transcript

  • Playing functionalConway’s game of life Vagif Abilov
  • About myself• Mail: vagif.abilov@gmail.com• Twitter: @ooobject• GitHub: object• BitBucket: object• Blog: http://bloggingabout.net/blogs/vagif/default.aspx• Articles: http://www.codeproject.comThe source code for this presentation can be found athttps://github.com/object/ConwayGame
  • Playing functionalConway’s game of life Vagif Abilov
  • 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 deadSource: Wikipedia
  • Rules of Conway’s game of life1. 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.
  • Impelemening Conway’s gameHow would you approach the implementation?A small quiz: what names should we introduce?• Classes• Class properties• Class methods• Structs• Enums
  • 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
  • Speaking about Russian literatureNightingales, 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
  • Checking other implementationsSource:http://www.codeproject.com/Articles/185208/Solving-Conway-s-Game-of-Life-using-State-Pattern
  • 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)
  • Cell state definitionDiscussion in comments about cell state definition• Class (base class with 2 subclasses implementing State pattern)• Enum (Dead, Alive)• BooleanWhat would you choose?
  • 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!
  • Imperative languages lessons• Symbol definition does not necessarily lead to clean code• Names are opinionated, more names – more opinions• More names – more constraints• Design patterns are often introduced to patch up shortcomings in the language
  • Entering functional programming Conway’s game of life is about transforming cell generations What if we focus just on functional transformations?
  • Implementation plan1. Classical game of life2. Multidimensional game of life3. Concurrent game of life4. Game of life visualization5. Surprise, surprise…
  • Classical game of life
  • Multidimensional game of life
  • Asynchronous 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
  • 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>
  • Benchmark resultsPattern 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 togenerate a pattern of N * N / 2 elements** Pattern size 10 was used to warm up the text fixture
  • Game of life visualization
  • Time for surprises Life in a tweet Living with no sense of monad
  • Life in a tweet• A great algorithm should fit in a tweet• If an algorithm does not fit in a tweet, it’s overcomplicated• But to fit a great algorithm in a tweet you need a great programming language• F# is a great programming language!
  • Life in a tweetlet f s g = g|>List.mapi(fun i x->match Seq.sumBy(fun o->g.[abs((i+o)%(s*s))])[-1-s;-s;1-s;-1;1;-1+s;s;1+s]with 3->1|2->x|_->0)• Written by Phil Trelfold (http://trelford.com/blog/post/140.aspx)• 127 characters long
  • Song time! Living With No Sense of Monads Originally performed by Smokie with slightly different words
  • Living with no sense of monadsI first met Alice in a small bookstore,"What book, - I asked, - are you looking for?"And she said: "About monads."I understood thats a key to her heart,I had no doubts that I was smart.It should be easy reading, an easy start...
  • Living with no sense of monadsBut I still dont get this wisdom,Its really mystic wordI guess it has its reasons,Someone gets it, but I dont.Cos for twenty-four yearsIve been trying to understand the monads.
  • Living with no sense of monadsTwenty-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 senseof monads
  • Living with no sense of monadsGrew up together,Went to the same class,And to be honest:I was better in mathThan Alice.
  • Living with no sense of monadsToo old for rock-n-roll - Im forty four,Too young for monads - still have a hope.That the day will comeWhen I let Alice know...
  • Living with no sense of monadsBut I still dont get this wisdom,Its really mystic wordI guess it has its reasons,Someone gets it, but I dont.Cos for twenty-four yearsIve been trying to understand the monads.(all together):Monads! What the f*ck is monads?!
  • Living with no sense of monadsTwenty-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 senseof monads
  • Thank you!• Mail: vagif.abilov@gmail.com• Twitter: @ooobject• GitHub: object• BitBucket: object• Blog: http://bloggingabout.net/blogs/vagif/default.aspx• Articles: http://www.codeproject.comThe source code for this presentation can be found athttps://github.com/object/ConwayGame