Slide 1

Slide 1 text

Haskell, Elm – why do they appeal? My motivation – Nodejs re-sparked interest in FP Course by RxJs author Still not fluent, looking for chances to try things out HackerRank, noughts and crosses Computer game puzzle first real-world solution using Haskell

Slide 2

Slide 2 text

Please watch the cartoons They are there for your education. Thank you.

Slide 3

Slide 3 text

Ask questions! How else am I going to learn ? Also - it's important to hear what everybody thinks about all this and learn from that Still, is a lot to get through, so instead of one2one chats, there's the pub, email or coffee sometime Really – my contact details are at the end, do get in touch

Slide 4

Slide 4 text

No problem if you don't get all of it I don't . (not kidding here !) There isn't a test at the end (I hope not,anyway) Although all the topics are related, you don't need to grasp all of them to learn the next one. This is really so, it's ok to let stuff go by So whether you ask questions now, later or never, that's fine

Slide 5

Slide 5 text

FP examples LINQ RxJs Redux

Slide 6

Slide 6 text

Why might any of this be useful?

Slide 7

Slide 7 text

Dev projects start off so well

Slide 8

Slide 8 text

So, how does this happen?

Slide 9

Slide 9 text

Overview Quick intro to Haskell Visual review of solution with Elm front-end Go over Haskell code, then talk about solutions Elm - uni-directional design and code review DEMO! Time travel debugging Bonus - solutions in Js/RxJs app & C# Linq (if we get the time)

Slide 10

Slide 10 text

Don't believe everything you hear! I'm not a Haskell programmer, I just write Haskell programs What follows is how I see/understand things It may or may not exactly match the books, wikis, academic papers, mathematical proofs etc.

Slide 11

Slide 11 text

Scientific jargon to follow

Slide 12

Slide 12 text

Haskell type strictness Haskell is relentless in type checking. No casts or ignore flags … Can decide not to specify type – Haskell complains if this does not hold up It does have the notion of type groups – num, equality … (C# generic constraints)

Slide 13

Slide 13 text

Haskell - immutability Nothing changes, we create new things only Functions have no side effects - Same input - same output, always except for when we have side effects, but let's move on!

Slide 14

Slide 14 text

Game

Slide 15

Slide 15 text

Game - puzzle

Slide 16

Slide 16 text

Haskell types The usual Int, Float, String, Bool suspects Lists are like arrays Any length – can only hold a single type Tuples are like, er, multiples ? Fixed length – may store multiple types Algebraic create our own from the above (diy) Records object-like syntactic sugar in Elm, a bit duck type-y

Slide 17

Slide 17 text

Haskell Lists – quick intro Build [1,2,3] like this 1: 2: 3: [] Pass as params like this xs entire list x:xs x is “head”, xs is “tail” (destructuring) NOTE - access anything past the head – may take a (lot) longer TL;DR – Haskell functions are designed to work with head of list as much as possible

Slide 18

Slide 18 text

Elm Show app in browser

Slide 19

Slide 19 text

Solution – List operations Wheels represented as lists of Ints One list is a Wheel Pos TurnWheel int Wheel Pos drop, take are list operations - ideal for small lists, slower for larger lists

Slide 20

Slide 20 text

Turn Wheel WheelPos turn WheelPos IN OUT

Slide 21

Slide 21 text

Strange stuff on the way This might all seem a bit weird … Feel free to let it go by !

Slide 22

Slide 22 text

Recursion, pattern matching Pattern matching – a bit like if … then … Common Haskell recursion boilerplate (just pseudo code) Fn xs 0 = xs end condition Fn x:xs n = (f x) : Fn xs (n – 1) recurse step e.g. map _ [] = [] map f (x:xs) = f x : map f xs

Slide 23

Slide 23 text

Solution - Recursion List of Wheel Pos Wheel Loop Not a great name, any suggestions? Created by buildWheelLoop Gives us secLoop, thrLoop and ansLoop As shown in app

Slide 24

Slide 24 text

build WheelLoop List WheelPos newP os WheelLoop WheelLoop From StartPos WheelPos WheelLoop Is called by

Slide 25

Slide 25 text

Solution - Map Given first Wheel, and secLoop Use map to go through secLoop and attach to copy of first item This creates a LoopsPermutation - two loops mixed, perhaps In effect, basis for solution/algorithm – see in app Here, Haskell code is little different from any modern language (that uses an anonymous function)

Slide 26

Slide 26 text

TwoWheel Perms WheelPos Wheel Loop List LoopsPermutation ThreeWheel Perms WheelPos Wheel Loop List LoopsPermutation Wheel Loop

Slide 27

Slide 27 text

Solution – sumPlusPerms Go through permutations and add anwers to each set Add across lists – this is what I wondered about for nought and crosses Here it seems natural enough - for a single column

Slide 28

Slide 28 text

Solution – findSpecificAnswer Go through permutations and anwers Trawl through answers, compare to answers loop Dropwhile is part of a range of elegant functions

Slide 29

Slide 29 text

Type transformations Quite like the sense of types being transformed

Slide 30

Slide 30 text

Something better Solution works – not sure it scales well More on this later Feels big and unwieldy, all those perms

Slide 31

Slide 31 text

Something better, surely Solution works – not sure it scales well and lots of smoke!

Slide 32

Slide 32 text

Something better I had this in mind when I started out

Slide 33

Slide 33 text

Solution – Second version Wanted a way not to build perms items So keep memory use limited to a small amount Since then, have heard about “constant space” Created a revised version of this, which filters the items before processing

Slide 34

Slide 34 text

Solution – Third version Based on early idea More memory per iteration than second But limited to 2 wheel perms So scales infinitely beyond that

Slide 35

Slide 35 text

Ran tests

Slide 36

Slide 36 text

More tests – Haskell profiling isn't so much fun!

Slide 37

Slide 37 text

Optimisation – hmm? Mild surprise at results of Haskell profiling

Slide 38

Slide 38 text

Optimisation

Slide 39

Slide 39 text

Solutions comparison 1 findSpecificAnswer 4,939,444,432 bytes allocated in the heap 13,881,442,884 bytes copied during GC 1,488,404 bytes maximum residency (9495 sample(s)) 56,556 bytes maximum slop 4 MB total memory in use (0 MB lost due to fragmentation) 25 million items, approx 20 mins

Slide 40

Slide 40 text

Solutions comparison 2a findSpecificAnswerCS 5,695,287,284 bytes allocated in the heap 14,737,731,940 bytes copied during GC 1,460,032 bytes maximum residency (10954 sample(s)) 57,016 bytes maximum slop 4 MB total memory in use (0 MB lost due to fragmentation)

Slide 41

Slide 41 text

Solutions comparison 2b findSpecificAnswerCS 7,630,725,720 bytes allocated in the heap 19,752,688,420 bytes copied during GC 1,460,060 bytes maximum residency (14680 sample(s)) 54,804 bytes maximum slop 4 MB total memory in use (0 MB lost due to fragmentation) 25 million items, approx 10 mins

Slide 42

Slide 42 text

Solutions comparison 3 findSpecificAnswerX 2,115,601,208 bytes allocated in the heap 9,224,696,568 bytes copied during GC 2,454,488 bytes maximum residency (4059 sample(s)) 71,972 bytes maximum slop 6 MB total memory in use (0 MB lost due to fragmentation)

Slide 43

Slide 43 text

Solutions comparison

Slide 44

Slide 44 text

Haskell – Lazy evaluation Other languages find one item easy

Slide 45

Slide 45 text

Haskell – Lazy evaluation But many are overwhelming

Slide 46

Slide 46 text

Haskell – Lazy evaluation

Slide 47

Slide 47 text

Optimising- general Constant space Speed to result Initial heap size Stack size

Slide 48

Slide 48 text

Solutions review 1st – lazy, but possible increases in mem usage – however Haskell may treats dropWhile as tail recursion, so may be tight loop 2nd – definitely works in constant space, should be treated as tail recursion 3rd – creates differences between perms – a middle ground in use of memory

Slide 49

Slide 49 text

Haskell - folds Like loops in other languages Designed to be used where lazy eval is, or is not, appropriate Very flexible and powerful Used for main familiar functions, map, filter … And which takes us to Elm

Slide 50

Slide 50 text

Elm – signals Like observables in RxJs Signal.foldp, Signal.map

Slide 51

Slide 51 text

Elm – uni-directional system Model – inputs, data (somewhat merged here) View – takes model, creates Html Html events lead to Signal changes (like Redux Actions) Update – takes Signal(s), updates model New model sent to view Considering code as pseudo-code works well

Slide 52

Slide 52 text

Time travel debugging DEMO !

Slide 53

Slide 53 text

Bonus code - RxJs Uni-directional UI RxJs – deals with infinite streams we can choose to get first item only Typescript – preserves Haskell types Do they stand out so clearly Are they useful? What have we gained or lost by this functional programming style ?

Slide 54

Slide 54 text

Bonus code – C# Linq Not so much done here, but get a flavour Like that the Haskell types transfer well We have lazy eval – we declare operations But nothing happens until x amount of data requested What have we gained or lost by this functional programming style ?

Slide 55

Slide 55 text

Thanks for listening Github: https://github.com/jkbits1 Repo: talkUI (nodejs server for apps) HaskellElmTalk (code only) [email protected] Katas and Puzzles https://www.hackerrank.com/ https://wiki.haskell.org/H-99:_Ninety-Nine_Haskell_Problems Books: http://learnyouahaskell.com/ http://book.realworldhaskell.org/ https://en.wikibooks.org/wiki/Haskell