馃搵 Listy

Listy to uporz膮dkowane kolekcje warto艣ci. Mog膮 zawiera膰 elementy r贸偶nych typ贸w i s膮 mutowalne.

Tworzenie list

// Podstawowe tworzenie let numbers = [1, 2, 3, 4, 5]; let mixed = [1, "text", true, null]; // r贸偶ne typy let empty = []; let nested = [[1, 2], [3, 4]]; // zagnie偶d偶one // Trailing comma dozwolona let list = [1, 2, 3,]; // OK // Generowanie z range let range = 1..5; // [1, 2, 3, 4, 5] let odds = 1..10 step 2; // [1, 3, 5, 7, 9] // List multiplication let zeros = [0] * 5; // [0, 0, 0, 0, 0] let pattern = [1, 2] * 3; // [1, 2, 1, 2, 1, 2]

Dost臋p do element贸w

let list = [10, 20, 30, 40, 50]; // Indeksowanie (od 0) print(list[0]); // 10 (pierwszy) print(list[2]); // 30 // Negative indexing (od ko艅ca) print(list[-1]); // 50 (ostatni) print(list[-2]); // 40 (przedostatni) print(list[-5]); // 10 (pierwszy) // Safe access print(list?[100]); // null (nie b艂膮d!)

Slicing

let list = [0, 1, 2, 3, 4, 5]; // [start:end] - od start do end (exclusive) print(list[1:4]); // [1, 2, 3] // [:end] - od pocz膮tku print(list[:3]); // [0, 1, 2] // [start:] - do ko艅ca print(list[3:]); // [3, 4, 5] // Negatywne indeksy w slice print(list[1:-1]); // [1, 2, 3, 4] (bez ostatniego) print(list[-3:]); // [3, 4, 5] (ostatnie 3)

Modyfikacja

let list = [1, 2, 3]; // Zmiana elementu list[0] = 100; print(list); // [100, 2, 3] // Zmiana z negatywnym indeksem list[-1] = 99; print(list); // [100, 2, 99] // Metody mutuj膮ce list.push(4); // [100, 2, 99, 4] list.pop(); // Usuwa 4, zwraca 4 list.unshift(0); // [0, 100, 2, 99] list.shift(); // Usuwa 0, zwraca 0

W艂a艣ciwo艣ci list (OOP)

let numbers = [1, 2, 3, 4, 5]; numbers.length; // 5 numbers.first; // 1 numbers.last; // 5 numbers.isEmpty; // false [].isEmpty; // true

Metody transformacji (zwracaj膮 now膮 list臋)

let nums = [1, 2, 3, 4, 5]; // Map - transformacja ka偶dego elementu nums.map(x => x * 2); // [2, 4, 6, 8, 10] // Filter - wybierz pasuj膮ce nums.filter(x => x > 2); // [3, 4, 5] // Reverse nums.reverse(); // [5, 4, 3, 2, 1] // Sort [3, 1, 4, 1, 5].sort(); // [1, 1, 3, 4, 5] // Unique - usu艅 duplikaty [1, 2, 2, 3, 3, 3].unique(); // [1, 2, 3] // Flatten - sp艂aszcz zagnie偶d偶one [[1, 2], [3, 4]].flatten(); // [1, 2, 3, 4]

Metody agregacji

let nums = [1, 2, 3, 4, 5]; // Reduce - zredukuj do jednej warto艣ci nums.reduce((acc, x) => acc + x, 0); // 15 // Wbudowane agregacje nums.sum(); // 15 nums.min(); // 1 nums.max(); // 5 nums.average(); // 3

Metody wyszukiwania

let nums = [1, 2, 3, 4, 5]; // Find - znajd藕 pierwszy pasuj膮cy nums.find(x => x > 3); // 4 // FindIndex - indeks pierwszego pasuj膮cego nums.findIndex(x => x > 3); // 3 // Includes - czy zawiera nums.includes(3); // true // IndexOf - pozycja elementu nums.indexOf(3); // 2 // Some - czy kt贸rykolwiek pasuje nums.some(x => x > 4); // true // Every - czy wszystkie pasuj膮 nums.every(x => x > 0); // true

Pobieranie fragment贸w

let nums = [1, 2, 3, 4, 5]; nums.take(3); // [1, 2, 3] - pierwsze 3 nums.drop(2); // [3, 4, 5] - pomi艅 pierwsze 2 nums.slice(1, 4); // [2, 3, 4] nums.chunk(2); // [[1, 2], [3, 4], [5]]

艁膮czenie i konwersja

// Concat - 艂膮czenie list [1, 2].concat([3, 4]); // [1, 2, 3, 4] // Join - do stringa ["a", "b", "c"].join("-"); // "a-b-c" // Spread operator let a = [1, 2]; let b = [...a, 3, 4]; // [1, 2, 3, 4]

Chainowanie metod

let result = [1, 2, 3, 4, 5, 6] .filter(x => x % 2 == 0) // [2, 4, 6] .map(x => x * 10) // [20, 40, 60] .reduce((a, b) => a + b, 0); // 120 print(result); // 120

List Comprehension

// Podstawowa sk艂adnia: [wyra偶enie for zmienna in iterable] let squares = [x * x for x in 1..5]; print(squares); // [1, 4, 9, 16, 25] // Z warunkiem (if) let evens = [x for x in 1..10 if x % 2 == 0]; print(evens); // [2, 4, 6, 8, 10] // Z艂o偶one wyra偶enia let bigSquares = [x * x for x in 1..10 if x > 5]; print(bigSquares); // [36, 49, 64, 81, 100] // Z exclusive range let indices = [i for i in 0..<5]; print(indices); // [0, 1, 2, 3, 4]

Iteracja

let items = ["a", "b", "c"]; // For-in for (item in items) { print(item); } // Z indeksem for (i in 0..size(items)) { print(i, items[i]); } // ForEach items.forEach(item => print(item));
馃挕 Wskaz贸wka

Metody map, filter, reduce s膮 dost臋pne zar贸wno jako metody OOP (list.map(...)) jak i funkcje po za艂adowaniu modu艂u functional.