Slide 1

Slide 1 text

@miciek Michał Płachta www.michalplachta.com IMMUTABILITY AGAINST THE MACHINE

Slide 2

Slide 2 text

@miciek Back to 2001

Slide 3

Slide 3 text

@miciek 2001 Let’s replace Haskell with Java University of Texas

Slide 4

Slide 4 text

@miciek 2001 Let’s replace Haskell with Java University of Texas

Slide 5

Slide 5 text

@miciek Dijkstra writes to University of Texas https://www.cs.utexas.edu/users/EWD/transcriptions/OtherDocs/Haskell.html Facing students with the novelty of functional programming immediately drives home the message that there is more to programming than they thought. “ ”

Slide 6

Slide 6 text

@miciek Dijkstra writes to University of Texas https://www.cs.utexas.edu/users/EWD/transcriptions/OtherDocs/Haskell.html Facing students with the novelty of functional programming immediately drives home the message that there is more to programming than they thought. “ ”

Slide 7

Slide 7 text

@miciek - declarative, functional - relationships between values - rigorous reasoning - imperative - step-by-step statements - operational reasoning Learning from other paradigms Our Paradigm Other Paradigms

Slide 8

Slide 8 text

@miciek DEMO: printing stuff in the REPL

Slide 9

Slide 9 text

@miciek What is it? vs What does it do? declarative vs imperative The tools we use shape our thinking!

Slide 10

Slide 10 text

@miciek The tools we use shape our thinking What is it? An immutable value! What does it do? Eeee… It prints ‘4’.

Slide 11

Slide 11 text

@miciek - declarative, functional - relationships between values - rigorous reasoning - imperative - step-by-step statements - operational reasoning Learning from other paradigms What Does It Do? What is It? We can learn a lot from there!

Slide 12

Slide 12 text

@miciek What does it do? List xs = Arrays.asList(1, 2, 3, 4, 5); List result = new ArrayList < > (); // empty for (Integer x: xs) { result.add(x * 2); } // now result is a list [2, 4, 6, 8, 10] Java

Slide 13

Slide 13 text

@miciek What is it? val numbers = List(1, 2, 3, 4, 5) val doubles = numbers.map(i => i * 2) / / List(2, 4, 6, 8, 10)

Slide 14

Slide 14 text

@miciek What is it? val numbers = List(1, 2, 3, 4, 5) val doubles = numbers.map(i => i * 2) / / List(2, 4, 6, 8, 10) Scala numbers List[Int] doubles List[Int] .map

Slide 15

Slide 15 text

@miciek What does it do? String tvShow = "The Wire (2002-2008)"; int extractYearStart(String show) throws Exception {…} int extractYearEnd(String show) throws Exception {…} int start = extractYearStart(tvShow); / / 2002 int end = extractYearEnd(tvShow); // 2008 Java

Slide 16

Slide 16 text

@miciek What does it do? String tvShow = "The Wire (2002-2008)"; int extractYearStart(String show) throws Exception {…} int extractYearEnd(String show) throws Exception {…} int start = extractYearStart(tvShow); / / 2002 int end = extractYearEnd(tvShow); // 2008 extractYearStart("Chernobyl (2019)"); / / ??? Java

Slide 17

Slide 17 text

@miciek What does it do? String tvShow = "The Wire (2002-2008)"; int extractYearStart(String show) throws Exception {…} int extractYearEnd(String show) throws Exception {…} int start = extractYearStart(tvShow); / / 2002 int end = extractYearEnd(tvShow); // 2008 extractYearStart("Chernobyl (2019)"); / / ??? Java

Slide 18

Slide 18 text

@miciek What does it do? int extractYearStart(String show) throws Exception {…} int extractYearEnd(String show) throws Exception {…} int extractSingleYear(String show) throws Exception {…} Integer year = null; try { year = extractYearStart(tvShow); } catch(Exception e) { year = extractSingleYear(tvShow); } Java

Slide 19

Slide 19 text

@miciek What is it? def extractYearStart(show: String): Option[Int] = ??? def extractYearEnd(show: String): Option[Int] = ??? def extractSingleYear(show: String): Option[Int] = ??? Scala None Option[A] Some[A] val value: A

Slide 20

Slide 20 text

@miciek What is it? def extractYearStart(show: String): Option[Int] = ??? def extractYearEnd(show: String): Option[Int] = ??? def extractSingleYear(show: String): Option[Int] = ??? val year = extractYearStart(tvShow).orElse(extractSingleYear(tvShow)) Scala extractYearStart Option[Int] extractSingleYear Option[Int] year Option[Int] .orElse

Slide 21

Slide 21 text

@miciek - declarative, functional - relationships between values - rigorous reasoning - what is it? - imperative - step-by-step statements - operational reasoning - what does it do? We don’t have to think like a machine The Machine Immutability

Slide 22

Slide 22 text

@miciek Shaping our thinking habits It is not only the violin that shapes the violinist, we are all shaped by the tools we train ourselves to use… “ ” https://www.cs.utexas.edu/users/EWD/transcriptions/OtherDocs/Haskell.html

Slide 23

Slide 23 text

@miciek Plus… FP ideas aren’t going away https://www.infoq.com/articles/data-oriented-programming-java/

Slide 24

Slide 24 text

@miciek Passing immutable values around numbers List[Int] extractYearStart Option[Int] extractSingleYear Option[Int] doubles List[Int] year Option[Int] .map .orElse

Slide 25

Slide 25 text

@miciek What about talking to the outside world?

Slide 26

Slide 26 text

@miciek IMMUTABILITY AGAINST THE MACHINE Using S tri n g s, L i s t s, Op ti ons Talking to the outside world Threads, resource safety, and state

Slide 27

Slide 27 text

@miciek -User can search for an attraction name -The app finds the attraction and its location -It returns movies that are set in this location A peculiar travel guide

Slide 28

Slide 28 text

@miciek A peculiar travel guide > “Bridge of Sighs” You searched for “Bridge of Sighs”, which is located in Venice. Before visiting, you can watch: Casino Royale

Slide 29

Slide 29 text

@miciek The model case class LocationId(value: String) case class Location(id: LocationId, name: String) case class Attraction(name: String, location: Location) case class Movie(name: String, boxOffice: Long) case class TravelGuide(attraction: Attraction, movies: List[Movie])

Slide 30

Slide 30 text

@miciek We will use Wikidata https://query.wikidata.org/

Slide 31

Slide 31 text

@miciek Thinking Like A Machine https://jena.apache.org/

Slide 32

Slide 32 text

@miciek String query = "SELECT DISTINCT ?attraction ?attractionLabel \n" + " ?location ?locationLabel WHERE {\n" + " ?attraction wdt:P31 wd:Q570116;\n" + " rdfs:label ?attractionLabel;\n" + " wdt:P131 ?location.\n" + "} LIMIT 3"; RDFConnection connection = RDFConnectionRemote.create() .destination("https: // query.wikidata.org/") .queryEndpoint("sparql") .build(); QueryExecution execution = connection.query(QueryFactory.create(query)); Java Thinking Like A Machine https://jena.apache.org/

Slide 33

Slide 33 text

@miciek Iterator solutions = execution.execSelect(); while(solutions.hasNext()) { QuerySolution solution = solutions.next(); String id = solution.getResource("attraction").getLocalName(); String label = solution.getLiteral("label").getString(); System.out.printf("Got attraction %s (id = %s)%n", label, id); } Java Thinking Like A Machine Got attraction Yellowstone National Park (id = Q351) Got attraction Eiffel Tower (id = Q243) Got attraction Table Mountain (id = Q213360) Output

Slide 34

Slide 34 text

@miciek def execQuery(query: String): IO[List[QuerySolution]] = { … } Scala Immutability https://typelevel.org/cats-effect/

Slide 35

Slide 35 text

@miciek def execQuery(query: String): IO[List[QuerySolution]] = { … } Scala Immutability https://typelevel.org/cats-effect/ IO[List[QuerySolution]] Immutable value representing a potentially side-effectful program that, once executed successfully, will produce a list of query solutions.

Slide 36

Slide 36 text

@miciek def execQuery(query: String): IO[List[QuerySolution]] = { IO.delay(connection.query(QueryFactory.create(query)).execSelect()) } Scala Immutability IO.delay( … ) Creates an immutable value representing a potentially side-effectful program that will execute the given block of code and return the result.

Slide 37

Slide 37 text

@miciek def execQuery(query: String): IO[List[QuerySolution]] = { IO.delay(connection.query(QueryFactory.create(query)).execSelect()) } val solutionsProgram: IO[List[QuerySolution]] = execQuery(query) println(solutionsProgram) Scala Immutability

Slide 38

Slide 38 text

@miciek def execQuery(query: String): IO[List[QuerySolution]] = { IO.delay(connection.query(QueryFactory.create(query)).execSelect()) } val solutionsProgram: IO[List[QuerySolution]] = execQuery(query) println(solutionsProgram) // IO( .. . ) Scala Immutability

Slide 39

Slide 39 text

@miciek def execQuery(query: String): IO[List[QuerySolution]] = { IO.delay(connection.query(QueryFactory.create(query)).execSelect()) } val solutionsProgram: IO[List[QuerySolution]] = execQuery(query) val attractionsProgram: IO[List[Attraction]] = Scala Immutability We don’t have to run anything, let’s work with values first!

Slide 40

Slide 40 text

@miciek def execQuery(query: String): IO[List[QuerySolution]] = { IO.delay(connection.query(QueryFactory.create(query)).execSelect()) } val solutionsProgram: IO[List[QuerySolution]] = execQuery(query) def parseAttraction(s: QuerySolution): Option[Attraction] = { … } val attractionsProgram: IO[List[Attraction]] = solutionsProgram.map(solutions = > solutions.flatMap(parseAttraction)) Scala Immutability

Slide 41

Slide 41 text

@miciek val solutionsProgram: IO[List[QuerySolution]] = execQuery(query) val attractionsProgram: IO[List[Attraction]] = solutionsProgram.map(solutions = > solutions.flatMap(parseAttraction)) Scala Immutability List[QuerySolution] => List[Attraction] We transformed results without having them!

Slide 42

Slide 42 text

@miciek Immutability def execQuery(query: String): IO[List[QuerySolution]] = { IO.delay(connection.query(QueryFactory.create(query)).execSelect()) } val solutionsProgram: IO[List[QuerySolution]] = execQuery(query) def parseAttraction(s: QuerySolution): Option[Attraction] = { … } val attractionsProgram: IO[List[Attraction]] = solutionsProgram.map(solutions = > solutions.flatMap(parseAttraction)) Scala solutionsProgram IO[List[QuerySolution]] attractionsProgram IO[List[Attraction]] .map

Slide 43

Slide 43 text

@miciek DEMO: Let’s code the travel guide! > “Bridge of Sighs” You searched for “Bridge of Sighs”, which is located in Venice. Before visiting, you can watch: Casino Royale

Slide 44

Slide 44 text

@miciek IMMUTABILITY AGAINST THE MACHINE Using S tri n g s, L i s t s, Op ti ons Talking to the outside world Threads, resource safety, and state

Slide 45

Slide 45 text

@miciek IMMUTABILITY AGAINST THE MACHINE IO List Option String Ref Resource

Slide 46

Slide 46 text

@miciek - declarative, functional - relationships between values - rigorous reasoning - what is it? - imperative - step-by-step statements - operational reasoning - what does it do? We don’t have to think like a machine The Machine Immutability

Slide 47

Slide 47 text

@miciek Shaping our thinking habits It is not only the violin that shapes the violinist, we are all shaped by the tools we train ourselves to use… “ ” https://www.cs.utexas.edu/users/EWD/transcriptions/OtherDocs/Haskell.html

Slide 48

Slide 48 text

@miciek Let’s go back to 2023

Slide 49

Slide 49 text

@miciek Java (will) have it, too! FP Future Java

Slide 50

Slide 50 text

@miciek https://www.manning.com/plachta 40% off code: watchplachta40 www.michalplachta.com

Slide 51

Slide 51 text

@miciek Michał Płachta www.michalplachta.com IMMUTABILITY AGAINST THE MACHINE code, links, slides, book info