All You Need is Fold in the Key of C#

134
All You Need is Fold in the Key of C# Mike Harris

Transcript of All You Need is Fold in the Key of C#

Page 1: All You Need is Fold in the Key of C#

All You Need is Foldin the Key of C# Mike Harris

Page 2: All You Need is Fold in the Key of C#

All You Need Is FOLD

Page 3: All You Need is Fold in the Key of C#

All You Need Is FOLD

Page 4: All You Need is Fold in the Key of C#

All you need is FOLD

Page 5: All You Need is Fold in the Key of C#

FOLD

Page 6: All You Need is Fold in the Key of C#

Fold is All You Need!

Page 7: All You Need is Fold in the Key of C#

Tour

• Backstage

• Concert

• Farewell

Page 8: All You Need is Fold in the Key of C#

Backstage

Page 9: All You Need is Fold in the Key of C#

f

g

Function

Page 10: All You Need is Fold in the Key of C#

f g

f(g)

Function

Page 11: All You Need is Fold in the Key of C#

Fold

a1

a2

an

b

Page 12: All You Need is Fold in the Key of C#

Fold

foldr :: (α → β → β) → β → ([α] → β)foldl :: (β → α→ β) → β → ([α] → β)

SeedFunc

a1

a2

an

Fold b

Given

We

Func

Seed

Page 13: All You Need is Fold in the Key of C#

f anf a2f Seed a1

Page 14: All You Need is Fold in the Key of C#

f anf a2f(Seed, a1)

Page 15: All You Need is Fold in the Key of C#

f anf(f(Seed, a1), a2)

Page 16: All You Need is Fold in the Key of C#

b

Page 17: All You Need is Fold in the Key of C#

Fold

a1

a2

an

b

Page 18: All You Need is Fold in the Key of C#

— Paul FeyerabendAgainst Method Chapter 16

“Fundamental conceptual change … presupposes new world-views and

new languages capable of expressing them.”

Page 19: All You Need is Fold in the Key of C#

Concert

All You Need is Fold

Page 20: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 21: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 22: All You Need is Fold in the Key of C#

And

T/F

T/F

T/F

T/F

Page 23: All You Need is Fold in the Key of C#

And

and :: [Bool] → Booland = fold (∧) True

TrueAnd

f1

f2

fn

Fold f

Given

We

And

True

Page 24: All You Need is Fold in the Key of C#

And

True And

Page 25: All You Need is Fold in the Key of C#

public bool And( params bool[] facts){ return facts.Aggregate( true, (m, x) => m && x);}

And True

Page 26: All You Need is Fold in the Key of C#

var sut = new Folder(); sut.And(false, true, false);

Page 27: All You Need is Fold in the Key of C#

And

True

False True False

And

Page 28: All You Need is Fold in the Key of C#

And

True

False True False

M

X

result False

And

Page 29: All You Need is Fold in the Key of C#

And

False

True False

M

X

result False

And

Page 30: All You Need is Fold in the Key of C#

And

False

False

M

X

result False

And

Page 31: All You Need is Fold in the Key of C#

And

True

False True False

result False

And

Page 32: All You Need is Fold in the Key of C#

public bool And( params bool[] facts){ return facts.Aggregate( true, (m, x) => m && x);}

And True

Page 33: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 34: All You Need is Fold in the Key of C#

Or

T/F

T/F

T/F

T/F

Page 35: All You Need is Fold in the Key of C#

Or

or :: [Bool] → Boolor = fold (∨) False

FalseOr

f1

f2

fn

Fold f

Given

We

Or

False

Page 36: All You Need is Fold in the Key of C#

Or

False Or

Page 37: All You Need is Fold in the Key of C#

public bool Or( params bool[] facts){ return facts.Aggregate( false, (m, x) => m || x);}

Or False

Page 38: All You Need is Fold in the Key of C#

var sut = new Folder(); sut.Or(false, true, false);

Page 39: All You Need is Fold in the Key of C#

Or

False

False True False

Or

Page 40: All You Need is Fold in the Key of C#

Or

False

False True False

M

X

result False

Or

Page 41: All You Need is Fold in the Key of C#

Or

False

True False

M

X

result True

Or

Page 42: All You Need is Fold in the Key of C#

Or

True

False

M

X

result True

Or

Page 43: All You Need is Fold in the Key of C#

Or

False

False True False

result True

Or

Page 44: All You Need is Fold in the Key of C#

public bool Or( params bool[] facts){ return facts.Aggregate( false, (m, x) => m || x);}

Or False

Page 45: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 46: All You Need is Fold in the Key of C#

Length size

Page 47: All You Need is Fold in the Key of C#

Length

length :: [α] → Intlength = fold (λx n → 1 + n) 0

0+ 1

x1

x2

xn

Fold #

Given

We

+ 1

0

Page 48: All You Need is Fold in the Key of C#

+ 1

0 Length

Page 49: All You Need is Fold in the Key of C#

public int Length(ICollection<T> coll){ return coll.Aggregate( 0, (m, _) => m + 1);}

+ 1 0

Page 50: All You Need is Fold in the Key of C#

var sut = new Folder(); sut.Length("Mike".ToCharArray());

Page 51: All You Need is Fold in the Key of C#

+ 1

0

M i k e

Length

Page 52: All You Need is Fold in the Key of C#

+ 1

0M

result 1

M i k e

Length

Page 53: All You Need is Fold in the Key of C#

+ 1

1M

result 2

i k e

Length

Page 54: All You Need is Fold in the Key of C#

+ 1

2M

result 3

k e

Length

Page 55: All You Need is Fold in the Key of C#

+ 1

3M

result 4

e

Length

Page 56: All You Need is Fold in the Key of C#

+ 1

0

result 4

M i k e

Length

Page 57: All You Need is Fold in the Key of C#

public int Length(ICollection<T> coll){ return coll.Aggregate( 0, (m, _) => m + 1);}

+ 1 0

Page 58: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 59: All You Need is Fold in the Key of C#

Reverse

x1

x2

xn

xn

x2

x1

Page 60: All You Need is Fold in the Key of C#

Reverse

reverse :: [α] → [α]reverse = fold (λx m → m ++ [x]) [ ]

[ ]Cons

x1

x2

xn

Fold [x]

Given

We

Cons

[ ]

Page 61: All You Need is Fold in the Key of C#

Cons

[ ] Reverse

Page 62: All You Need is Fold in the Key of C#

public IEnumerable<T>Reverse(IEnumerable<T> coll){ return coll.Aggregate( new List<T>(), (m, x) => new List<T> {x} .Concat(m).ToList()););}

Cons [ ]

Page 63: All You Need is Fold in the Key of C#

var sut = new Folder(); sut.Reverse("Mike".ToCharArray());

Page 64: All You Need is Fold in the Key of C#

Cons

[ ]

M i k e

Reverse

Page 65: All You Need is Fold in the Key of C#

Cons

[ ]

M i k

M

X

result M

e

Reverse

Page 66: All You Need is Fold in the Key of C#

Cons

M

i k e

M

X

result iM

Reverse

Page 67: All You Need is Fold in the Key of C#

Cons

iM

k e

M

X

result kiM

Reverse

Page 68: All You Need is Fold in the Key of C#

Cons

kiM

e

M

X

result ekiM

Reverse

Page 69: All You Need is Fold in the Key of C#

Cons

[ ]

result ekiM

M i k e

Reverse

Page 70: All You Need is Fold in the Key of C#

public IEnumerable<T>Reverse(IEnumerable<T> coll){ return coll.Aggregate( new List<T>(), (m, x) => new List<T> {x} .Concat(m).ToList()););}

Cons [ ]

Page 71: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 72: All You Need is Fold in the Key of C#

Map

x1

x2

xn

z1

z2

zn

func

Page 73: All You Need is Fold in the Key of C#

Map

map :: (α → β) → ([α] → [β])map f = fold (λx xs → f x : xs) [ ]

[ ]Conj

x1

x2

xn

Fold [z]

Given

We

Conj

[ ]

Page 74: All You Need is Fold in the Key of C#

Conj

[ ]

func

Map

Page 75: All You Need is Fold in the Key of C#

public IEnumerable<U> Map<T, U>( IEnumerable<T> coll, Func<T, U> func){ return coll.Aggregate( new List<U>(), (m, x) => { m.Add(func(x)); return m;});}

Conj [ ]

Page 76: All You Need is Fold in the Key of C#

var sut = new Folder(); sut.Map( “Mike".ToCharArray(), s => s.ToUpper());

Page 77: All You Need is Fold in the Key of C#

Conj

[ ]

M i k e

upper

Map

Page 78: All You Need is Fold in the Key of C#

Conj

[ ]

M i k

M

X

result M

e

upper

Map

Page 79: All You Need is Fold in the Key of C#

Conj

M

i k e

M

X

result MIupper

Map

Page 80: All You Need is Fold in the Key of C#

Conj

MI

k e

M

X

result MIKupper

Map

Page 81: All You Need is Fold in the Key of C#

Conj

MIK

e

M

X

result MIKEupper

Map

Page 82: All You Need is Fold in the Key of C#

Conj

[ ]

M i k

result

e

upper MIKE

Map

Page 83: All You Need is Fold in the Key of C#

public IEnumerable<U> Map<T, U>( IEnumerable<T> coll, Func<T, U> func){ return coll.Aggregate( new List<U>(), (m, x) => { m.Add(func(x)); return m;});}

Conj [ ]

Page 84: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 85: All You Need is Fold in the Key of C#

Filter

x1

x2

xn

x1

xn

pred

Page 86: All You Need is Fold in the Key of C#

Filter

filter :: (α → Bool) → ([α] → [α])filter p = fold (λx xs → if p x then x : xs else xs) [ ]

if

Conj

[ ]

x1

x2

xn

Fold [x]

Given

We

[ ]

if

Conj

Page 87: All You Need is Fold in the Key of C#

if

[ ]

pred Conj

Filter

Page 88: All You Need is Fold in the Key of C#

public IEnumerable<T>Filter<T>( IEnumerable<T> coll, Func<T, bool> pred){ return coll.Aggregate( new List<T>(), (m, x) => { if(pred(x)) m.Add(x); return m;});} [ ]if Conj

Page 89: All You Need is Fold in the Key of C#

var sut = new Folder(); sut.Filter( “Mike".ToCharArray(), s => s.All(char.IsLower));

Page 90: All You Need is Fold in the Key of C#

if

[ ]

M i k e

isLower Conj

Filter

Page 91: All You Need is Fold in the Key of C#

if

[ ]

M i k

M

X

result [ ]

e

isLower Conj

Filter

Page 92: All You Need is Fold in the Key of C#

if

[ ]

i k e

M

X

isLower result iConj

Filter

Page 93: All You Need is Fold in the Key of C#

if

i

k e

M

X

isLower result ikConj

Filter

Page 94: All You Need is Fold in the Key of C#

if

ik

e

M

X

isLower result ikeConj

Filter

Page 95: All You Need is Fold in the Key of C#

if

[ ]

isLower

M i k e

result ikeConj

Filter

Page 96: All You Need is Fold in the Key of C#

public IEnumerable<T>Filter<T>( IEnumerable<T> coll, Func<T, bool> pred){ return coll.Aggregate( new List<T>(), (m, x) => { if(pred(x)) m.Add(x); return m;});} [ ]if Conj

Page 97: All You Need is Fold in the Key of C#

Set List

• And

• Or

• Length

• Reverse

• Map

• Filter

• Zip

Page 98: All You Need is Fold in the Key of C#

Zip

y1

y2

yj

z1

z2

zk

funcx1

x2

xi

Page 99: All You Need is Fold in the Key of C#

Zip

zip2 :: (α → β → γ) → ([α] → [β] → [γ])zip2 f = fold (λx y xs ys → f x y : xs ys) [ ]

[ ]Conj

y1

y2

ym

Fold [z]

Given

We

Conj

[ ]

x1

x2

xn

Page 100: All You Need is Fold in the Key of C#

Conj

[ ]

func

Zip

Page 101: All You Need is Fold in the Key of C#

public ICollection<TZ>Zip<TX, TY, TZ>( ICollection<TX> xs, ICollection<TY> ys, Func<TX, TY, TZ> func){ return xs.Zip(ys,(x, y) => new Tuple<TX, TY>(x, y)) .Aggregate( new List<TZ>(), (m, p) => { m.Add(func.Invoke(p.Item1, p.Item2)); return m; });} Conj [ ]

Page 102: All You Need is Fold in the Key of C#

return xs.Zip(ys, (x, y) => new Tuple<TX, TY>(x, y)) .Aggregate( new List<TZ>(), (m, p) => { m.Add( func.Invoke(p.Item1, p.Item2)); return m; });

Conj [ ]

Page 103: All You Need is Fold in the Key of C#

var sut = new Folder(); sut.Zip( new[] {1, 2}, new[] {10, 20}, (x, y) => x + y);

Page 104: All You Need is Fold in the Key of C#

Conj

[ ]

1 2

+

10 20

Zip

Page 105: All You Need is Fold in the Key of C#

Conj

[ ]

1 2

+

10 20

M

result 11

X

Y

Zip

Page 106: All You Need is Fold in the Key of C#

Conj

11

2

+

20

M

result 11 22

X

Y

Zip

Page 107: All You Need is Fold in the Key of C#

Conj

[ ]

+ result 11 22

1 2

10 20

Zip

Page 108: All You Need is Fold in the Key of C#

public ICollection<TZ>Zip<TX, TY, TZ>( ICollection<TX> xs, ICollection<TY> ys, Func<TX, TY, TZ> func){ return xs.Zip(ys,(x, y) => new Tuple<TX, TY>(x, y)) .Aggregate( new List<TZ>(), (m, p) => { m.Add(func.Invoke(p.Item1, p.Item2)); return m; });} Conj [ ]

Page 109: All You Need is Fold in the Key of C#

Encore

Coin Changer

Page 110: All You Need is Fold in the Key of C#

Change

coin1

coin2

coinn

c1

c2

cn

amount

Page 111: All You Need is Fold in the Key of C#

Coin Changer kata

changeFor :: [α] → α → [α]changeFor a = fold (λa x xs → (a % x, a / x) : xs (a, [ ])

Conj

Changer

[ ]

amount

c1

c2

cn

Fold

Given

We

[c]

0

Conj

Changer

[ ]

amount

Page 112: All You Need is Fold in the Key of C#

Conj

amount [ ]

Changer

Change

Page 113: All You Need is Fold in the Key of C#

public class CoinChanger { public IEnumerable<int> Coins { get; set; }

public IEnumerable<int> ChangeFor(int amount) { return Coins.Aggregate( new Tuple<int, List<int>>(amount, new List<int>()), (m, coin) => { m.Item2.Add(m.Item1/coin); return new Tuple<int, List<int>>( m.Item1%coin, m.Item2); }).Item2; } }

Conj [ ]

Changer amount

Page 114: All You Need is Fold in the Key of C#

public IEnumerable<int> ChangeFor(int amount) { return Coins.Aggregate( new Tuple<int, List<int>>(amount, new List<int>()), (m, coin) => { m.Item2.Add(m.Item1/coin); return new Tuple<int, List<int>>( m.Item1%coin, m.Item2); }).Item2; }

Conj [ ]

Changer amount

Page 115: All You Need is Fold in the Key of C#

var sut = new CoinChanger { Coins = new[] {25, 10, 5, 1} };

sut.ChangeFor(99);

Page 116: All You Need is Fold in the Key of C#

Conj

amount

25 10 5 1

[ ]

Changer

Change

Page 117: All You Need is Fold in the Key of C#

Conj

99

25 10 5 1

[ ]M

X

result 24 3Changer

Change

Page 118: All You Need is Fold in the Key of C#

Conj

24

10 5 1

3M

X

result 4 3 2Changer

Change

Page 119: All You Need is Fold in the Key of C#

Conj

4

5 1

3 2M

X

result 4 3 2 0Changer

Change

Page 120: All You Need is Fold in the Key of C#

Conj

4

1

3 2 0M

X

result 0 3 2 0 4Changer

Change

Page 121: All You Need is Fold in the Key of C#

Conj result 0 3 2 0 4Changer

amount [ ]

25 10 5 1

Change

Page 122: All You Need is Fold in the Key of C#

public class CoinChanger { public IEnumerable<int> Coins { get; set; }

public IEnumerable<int> ChangeFor(int amount) { return Coins.Aggregate( new Tuple<int, List<int>>(amount, new List<int>()), (m, coin) => { m.Item2.Add(m.Item1/coin); return new Tuple<int, List<int>>( m.Item1%coin, m.Item2); }).Item2; } }

Conj [ ]

Changer amount

Page 123: All You Need is Fold in the Key of C#

— Paul FeyerabendAgainst Method Chapter 18

“[A]ll methodologies, even the most obvious ones, have their limits”

Page 124: All You Need is Fold in the Key of C#
Page 125: All You Need is Fold in the Key of C#
Page 126: All You Need is Fold in the Key of C#
Page 127: All You Need is Fold in the Key of C#

Thank you!

Mike Harris@MikeMKHhttp://comp-phil.blogspot.com/Remember, all you need is Fold!

Page 128: All You Need is Fold in the Key of C#

Talks

• Rich Hickey - “Reducers”, EuroClojure 2012. https://vimeo.com/45561411

• Rich Hickey - “Transducers”, StrangeLoop 2014. https://www.youtube.com/watch?v=6mTbuzafcII

• Tomas Petricek - “History and Philosophy of Types”, StrangeLoop 2015. http://tpetricek.github.io/Talks/2015/philosophy-of-types/#/

Page 129: All You Need is Fold in the Key of C#

Books

• Richard Bird and Phil Wadler - Introduction to Functional Programming. Prentice-Hall,1988.http://usi-pl.github.io/lc/sp-2015/doc/Bird_Wadler.%20Introduction%20to%20Functional%20Programming.1ed.pdf

• Paul Feyerabend - Against Method. Verso, 2010.

• Michael Fogus - Functional JavaScript: Introducing Functional Programming with Underscore.js. O’Reilly, 2013.http://functionaljavascript.com/

Page 130: All You Need is Fold in the Key of C#

Papers

• Erik Meijer, Maarten Fokkinga, and Ross Paterson - “Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire”. http://eprints.eemcs.utwente.nl/7281/01/db-utwente-40501F46.pdf

• Graham Hutton - “A tutorial on the universality and expressiveness of fold”.http://www.cs.nott.ac.uk/~gmh/fold.pdf

Page 131: All You Need is Fold in the Key of C#

Blog Posts

• Brian McNamara - “Catamorphisms, part one”https://lorgonblog.wordpress.com/2008/04/05/catamorphisms-part-one/

• Edward Kmett - “Catamorphisms”https://www.fpcomplete.com/user/edwardk/recursion-schemes/catamorphisms

Page 132: All You Need is Fold in the Key of C#

Websites

• ClojureDocs - https://clojuredocs.org/

• HaskellWiki - https://wiki.haskell.org/Haskell

• Inside F# - https://lorgonblog.wordpress.com/

• lodash - https://lodash.com/docs

• MDN - https://developer.mozilla.org/en-US/

• ReactiveX - http://reactivex.io/

Page 133: All You Need is Fold in the Key of C#

Images (in order used)• "Abbey Rd Studios" by Misterweiss at en.wikipedia - Transfered from en.wikipedia Transfer was stated to be made by User:Blast..

Licensed under Public Domain via Commons - https://commons.wikimedia.org/wiki/File:Abbey_Rd_Studios.jpg#/media/File:Abbey_Rd_Studios.jpg

• "60s wallpaper" by Domincspics - Flickr. Licensed under CC BY 2.0 via Commons - https://commons.wikimedia.org/wiki/File:60s_wallpaper.jpg#/media/File:60s_wallpaper.jpg

• "70's Wallpaper" by Liz Sullivan - Own work. Licensed under CC BY-SA 4.0 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:70%27s_Wallpaper.JPG#/media/File:70%27s_Wallpaper.JPG

• "Flickr - ronsaunders47 - The Beatles-Sgt Pepper backdrop.". Licensed under CC BY-SA 2.0 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:Flickr_-_ronsaunders47_-_The_Beatles-Sgt_Pepper_backdrop..jpg#/media/File:Flickr_-_ronsaunders47_-_The_Beatles-Sgt_Pepper_backdrop..jpg

• "All you need is love graffiti Osijek" by Objavljeno - Own work. Licensed under CC BY-SA 3.0 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:All_you_need_is_love_graffiti_Osijek.JPG#/media/File:All_you_need_is_love_graffiti_Osijek.JPG

• "Illumined Pleasure Salvador Dali" by Salvador Dalí - Own workMrArifnajafov (Photography from the original). Licensed under CC BY-SA 3.0 via Wikimedia Commons - https://commons.wikimedia.org/wiki/File:Illumined_Pleasure_Salvador_Dali.JPG#/media/File:Illumined_Pleasure_Salvador_Dali.JPG

• "3 Savile Row" by Original uploader was Misterweiss at en.wikipedia - Transfered from en.wikipedia Transfer was stated to be made by User:Vinhtantran.. Licensed under Public Domain via Commons - https://commons.wikimedia.org/wiki/File:3_Savile_Row.jpg#/media/File:3_Savile_Row.jpg

• "Paul Feyerabend 2" by Grazia Borrini-Feyerabend. Licensed under Attribution via Commons - https://commons.wikimedia.org/wiki/File:Paul_Feyerabend_2.jpg#/media/File:Paul_Feyerabend_2.jpg

Page 134: All You Need is Fold in the Key of C#

Images (in order used)• "The Beatles on Green Hill in Almaty, Kazakhstan" by Ken and Nyetta - Flickr: The Beatles on

Green Hill in Almaty. Licensed under CC BY 2.0 via Commons - https://commons.wikimedia.org/wiki/File:The_Beatles_on_Green_Hill_in_Almaty,_Kazakhstan.jpg#/media/File:The_Beatles_on_Green_Hill_in_Almaty,_Kazakhstan.jpg

• "The Beatles in America" by United Press International, photographer unknown - This image is available from the United States Library of Congress's Prints and Photographs division under the digital ID cph.3c11094.This tag does not indicate the copyright status of the attached work.

• "The Beatles rooftop concert" by Source. Licensed under Fair use via Wikipedia - https://en.wikipedia.org/wiki/File:The_Beatles_rooftop_concert.jpg#/media/File:The_Beatles_rooftop_concert.jpg

• "Paul Feyerabend 2" by Grazia Borrini-Feyerabend. Licensed under Attribution via Commons - https://commons.wikimedia.org/wiki/File:Paul_Feyerabend_2.jpg#/media/File:Paul_Feyerabend_2.jpg

• Myself taken at StrangeLoop 2014 by my wife, Kelsey Harris