symmetry, scala & software -- refresh dublin october 2013

39
Symmetry, Scala & Software Refresh Dublin 15 October 2013 Eric Bowman @ebowman [email protected] http://tech.gilt.com

Upload: eric-bowman

Post on 22-Apr-2015

950 views

Category:

Technology


0 download

DESCRIPTION

Talk given as part of the Refreshing Series, about how symmetry manifests itself in software, using Scala as an example where internal symmetry leads to much greater expressiveness.

TRANSCRIPT

Page 1: Symmetry, Scala & Software -- Refresh Dublin October 2013

Symmetry, Scala & Software

Refresh Dublin 15 October 2013

Eric Bowman @ebowman

[email protected] http://tech.gilt.com

Page 2: Symmetry, Scala & Software -- Refresh Dublin October 2013
Page 3: Symmetry, Scala & Software -- Refresh Dublin October 2013
Page 4: Symmetry, Scala & Software -- Refresh Dublin October 2013

“harmonious and beautiful proportion

and balance”

http://en.wikipedia.org/wiki/Symmetry

Page 5: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://en.wikipedia.org/wiki/Symmetry

“patterned self-similarity”

Page 6: Symmetry, Scala & Software -- Refresh Dublin October 2013
Page 7: Symmetry, Scala & Software -- Refresh Dublin October 2013

Symmetry is changing without changing.

Page 8: Symmetry, Scala & Software -- Refresh Dublin October 2013

Symmetry in Scala

Page 9: Symmetry, Scala & Software -- Refresh Dublin October 2013

•Object-Oriented/Functional Hybrid

•Statically Typed

Scala

Page 10: Symmetry, Scala & Software -- Refresh Dublin October 2013

public class HelloWorld { public static void main(String[] args) { System.out.println("hello, world"); } }

Page 11: Symmetry, Scala & Software -- Refresh Dublin October 2013

public class HelloWorld { public static void main(String[] args) { System.out.println("hello, world"); } }

object HelloWorld extends App { println("hello, world") } !

Page 12: Symmetry, Scala & Software -- Refresh Dublin October 2013

println("hello, world")

public class HelloWorld { public static void main(String[] args) { System.out.println("hello, world"); } }

object HelloWorld extends App { println("hello, world") } !

Page 13: Symmetry, Scala & Software -- Refresh Dublin October 2013

for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.println(i*j); } } !!

Page 14: Symmetry, Scala & Software -- Refresh Dublin October 2013

for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.println(i*j); } } !!

for { i <- 1 to 10 j <- 1 to 10 } println(i * j) !

Page 15: Symmetry, Scala & Software -- Refresh Dublin October 2013

Future<Integer> f = callService1(); Future<Integer> f2 = callService2(f.get()); System.out.println(f2.get());

Page 16: Symmetry, Scala & Software -- Refresh Dublin October 2013

Future<Integer> f = callService1(); Future<Integer> f2 = callService2(f.get()); System.out.println(f2.get());

val f = for { x <- callService1() y <- callService2(x) } yield y println(Await.result(f, Duration.Inf)) !

Page 17: Symmetry, Scala & Software -- Refresh Dublin October 2013

String capitalize(String name) { if (name == null) return null; else return Character.toUpperCase( name.charAt(0)) + name.substring(1); }

Page 18: Symmetry, Scala & Software -- Refresh Dublin October 2013

String capitalize(String name) { if (name == null) return null; else return Character.toUpperCase( name.charAt(0)) + name.substring(1); }

def capitalize(n: Option[String]): Option[String] = { for (name <- n) yield { name(0).toUpper + name.substring(1) } } !!

Page 19: Symmetry, Scala & Software -- Refresh Dublin October 2013

String capitalize(String name) { if (name == null) return null; else return Character.toUpperCase( name.charAt(0)) + name.substring(1); }

Future<Integer> f = callService1(); Future<Integer> f2 = callService2(f.get()); System.out.println(f2.get());

for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.println(i*j); } } !!

for { i <- 1 to 10 j <- 1 to 10 } println(i * j) !

val f = for { x <- callService1() y <- callService2(x) } yield y println(Await.result(f, Duration.Inf)) !!

def capitalize(name: Option[String]): Option[String] = for (n <- name) yield { n(0).toUpper + n.substring(1) } !!

Java

Scala

Page 20: Symmetry, Scala & Software -- Refresh Dublin October 2013

Refactoring

Page 21: Symmetry, Scala & Software -- Refresh Dublin October 2013

“By continuously improving the design of code, we make it easier and easier to work with. This is in sharp contrast to what typically happens: little refactoring and a great deal of attention paid to expediently adding new features. If you get into the hygienic habit of refactoring continuously, you'll find that it is easier to extend and maintain code.”

http://en.wikipedia.org/wiki/Code_refactoring

Page 22: Symmetry, Scala & Software -- Refresh Dublin October 2013

•Abstraction

•Composition/Decomposition

•Name & Location

Page 23: Symmetry, Scala & Software -- Refresh Dublin October 2013

1. Finite, unchanging list of possible refactorings.

2. Every refactoring is reversible.

3. Every refactoring is deterministic.

4. Refactorings can be combined in any order.

Page 24: Symmetry, Scala & Software -- Refresh Dublin October 2013

Group Theory

Page 25: Symmetry, Scala & Software -- Refresh Dublin October 2013

“‘Numbers measure size; groups measure symmetry.”

Groups and Symmetry, Armstrong, Spring-Verlag, 1988, p.1

Page 26: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://upload.wikimedia.org/wikipedia/commons/5/53/Evariste_galois.jpg

“The most substantial piece of writing in the whole literature of mankind”

Page 27: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://opinionator.blogs.nytimes.com/2010/05/02/group-think/

Page 28: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://opinionator.blogs.nytimes.com/2010/05/02/group-think/

Page 29: Symmetry, Scala & Software -- Refresh Dublin October 2013

“the symmetry group of an object is the group of all isometries under which the

object is invariant with composition as the operation”

http://en.wikipedia.org/wiki/Symmetry_group

Page 30: Symmetry, Scala & Software -- Refresh Dublin October 2013

“the symmetry group of a program ... is the group of all refactorings under which the

program behaves identically after applying refactorings in any order”

Page 31: Symmetry, Scala & Software -- Refresh Dublin October 2013

String capitalize(String name) { if (name == null) return null; else return Character.toUpperCase( name.charAt(0)) + name.substring(1); }

Future<Integer> f = callService1(); Future<Integer> f2 = callService2(f.get()); System.out.println(f2.get());

for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { System.out.println(i*j); } } !!

for { i <- 1 to 10 j <- 1 to 10 } println(i * j) !

val f = for { x <- callService1() y <- callService2(x) } yield y println(Await.result(f, Duration.Inf)) !!

def capitalize(name: Option[String]): Option[String] = for (n <- name) yield { n(0).toUpper + n.substring(1) } !!

Java

Scala

Page 32: Symmetry, Scala & Software -- Refresh Dublin October 2013

Breaking Symmetry

Page 33: Symmetry, Scala & Software -- Refresh Dublin October 2013

a[i].x += a[i - 1].x; a[i].y += a[i - 1].y;

http://www.metalev.org/2011/04/source-code-symmetry-and-transcendent.html

Page 34: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://www.metalev.org/2011/04/source-code-symmetry-and-transcendent.html

a(i) += a(i-1)

a[i].x += a[i - 1].x; a[i].y += a[i - 1].y;

Page 35: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://pages.cs.wisc.edu/~dyer/vsam/images/mona.gif

http://www.leonardodavinci.net/images/gallery/monalisa_detail1.jpg

http://www.saylor.org/site/wp-content/uploads/2012/04/6-mona-lisa.gif

Page 36: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://upload.wikimedia.org/wikipedia/commons/thumb/d/db/MaryRose-carpentry_tools1.jpg/250px-MaryRose-carpentry_tools1.jpghttp://test.classconnection.s3.amazonaws.com/324/flashcards/95324/png/altarpiece.png

Page 37: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://upload.wikimedia.org/wikipedia/commons/thumb/d/db/MaryRose-carpentry_tools1.jpg/250px-MaryRose-carpentry_tools1.jpghttp://test.classconnection.s3.amazonaws.com/324/flashcards/95324/png/altarpiece.png

Page 38: Symmetry, Scala & Software -- Refresh Dublin October 2013

http://upload.wikimedia.org/wikipedia/commons/6/63/Michelangelos_David.jpghttp://amolife.com/image/images/stories/Art&Abstract/most_popular_sculptures%20(7).jpg

http://www.biographyonline.net/artists/images/Much-The_Scream.jpg

http://www.pics24h.com/img/artwork/5-most-famous-paintings/5-most-famous-paintings03.jpg