name: portada class: portada-slide, center, middle # Programació funcional ## Llistes --- --- # Exemple 1 - Given a list of int values, return the values that the double is lower than 10 ``` val newList = mutableListOf
() for(value in values) { val doubleValue = value*2 if(doubleValue>10){ newList.add(doubleValue) } } ``` --- # Programació _funcional_ ``` val newList = values.map { it *2 }.filter { it<10 } ``` --- # Programació _funcional_ - Obtenir el resultat mitjançant transformacions >> FP is a programming paradigm where programs are constructed by applying and composing functions. --- # Exemple 2 - Els valors que són més grans que l'anterior ``` val largerList = mutableListOf
() for(i in 1..values.lastIndex){ if(values[i-1] < values[i]) largerList.add(i) } ``` ``` values.windowed(2).filter { it[0] < it[1] }.map { it[1] } ``` --- # Exemple 3 - El nom de les persones amb menor salari segons l'edat, ordenades per edat ``` data class Person(val name: String, val age: Int, val salary: Double) persons.groupBy { it.age }.mapValues { it.value.minBy { it.salary }}.values.sortedBy { it.age } ``` --- # List ## Transformations --- # Map ``` val numbers = setOf(1, 2, 3) println(numbers.map { it * 3 }) println(numbers.mapIndexed { idx, value -> value * idx }) ``` ``` val numbers = setOf(1, 2, 3) println(numbers.mapNotNull { if ( it == 2) null else it * 3 }) println(numbers.mapIndexedNotNull { idx, value -> if (idx == 0) null else value * idx }) ``` --- # Zip ``` val colors = listOf("red", "brown", "grey") val animals = listOf("fox", "bear", "wolf") println(colors zip animals) val twoAnimals = listOf("fox", "bear") println(colors.zip(twoAnimals)) ``` --- # Associate ``` val numbers = listOf("one", "two", "three", "four") println(numbers.associateWith { it.length }) ``` --- # Flatten ``` val matrix = listOf(listOf(1, 2, 3), listOf(4, 5, 6), listOf(1, 2)) println(matrix.flatten()) ``` --- # Join to String ``` val numbers = listOf("one", "two", "three", "four") println(numbers.joinToString()) println(numbers.joinToString(", ")) ``` --- # List ## Filter --- # Filter ``` val numbers = listOf("one", "two", "three", "four") val longerThan3 = numbers.filter { it.length > 3 } println(longerThan3) ``` --- # Partition ``` val numbers = listOf("one", "two", "three", "four") val (match, rest) = numbers.partition { it.length > 3 } ``` --- # Any, all, none ``` val numbers = listOf("one", "two", "three", "four") println(numbers.any { it.endsWith("e") }) println(numbers.none { it.endsWith("a") }) println(numbers.all { it.endsWith("e") }) ``` --- # List ## Order --- # Sorted ``` val numbers = listOf("one", "two", "three", "four") println("Sorted ascending: ${numbers.sorted()}") println("Sorted descending: ${numbers.sortedDescending()}") ``` ``` val numbers = listOf("one", "two", "three", "four") val sortedNumbers = numbers.sortedBy { it.length } println("Sorted by length ascending: $sortedNumbers") val sortedByLast = numbers.sortedByDescending { it.last() } println("Sorted by the last letter descending: $sortedByLast") ``` --- # Agregate ``` fun main() { val numbers = listOf(6, 42, 10, 4) println("Count: ${numbers.count()}") println("Max: ${numbers.maxOrNull()}") println("Min: ${numbers.minOrNull()}") println("Average: ${numbers.average()}") println("Sum: ${numbers.sum()}") } ``` --- # Exemple - D'un text, les paraules, amb les seves repeticions, ordenades de més a menys repeticions ``` val string = """ One-one was a race horse. Two-two was one too. One-one won one race. Two-two won one too. """.trimIndent() string .split(" ", "-", ".", System.lineSeparator()) .filter { it.isNotEmpty() } .map { it.lowercase() } .groupingBy { it } .eachCount() .toList() .sortedBy { (_, count) -> count } .reversed() ``` --- # Documentació https://kotlinlang.org/docs/collections-overview.html