Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Collection Builders and Quanti ers vs Streams

Collection Builders and Quanti ers vs Streams

Virtual Machine Meetup

October 06, 2017
Tweet

More Decks by Virtual Machine Meetup

Other Decks in Education

Transcript

  1. Streams … have a more declarative style … are often

    terser … have a strong affinity with functions … encourage less mutability … encourage looser coupling … can succinctly express quite sophisticated behaviour … provide scope for future efficiency gains But: – Performance – Familiarity – Cognitive overhead – Debugging https://stackoverflow.com/questions/44180101/in-java-what-are-the-advantages-of-streams-over-loops#44180875
  2. List<Student> filtered = new ArrayList<>(); for (Student s : students)

    { if (s.age() < 18) { filtered.add(s); } } return filtered;
  3. List<Integer> a = Arrays.asList(3, 1, 7, 4, 2, 5, 9,

    8, 6); Stream<Integer> s = a.stream() .filter(x –> x < 5) .sorted() ; System.out.println(s.limit(3).collect(Collectors.toList())); System.out.println(s.count());
  4. List<Integer> a = Arrays.asList(3, 1, 7, 4, 2, 5, 9,

    8, 6); Stream<Integer> s = a.stream() .filter(x –> x < 5) .sorted() ; System.out.println(s.limit(3).collect(Collectors.toList())); System.out.println(s.count()); [1, 2, 3] Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed
  5. sum = 0; s.stream() .foreach(x –> sum += x) ;

    println(sum); sum ← 0 for x ∈ s do sum ← sum + x end printLine(sum)
  6. sum = 0; s.stream() .foreach(x –> sum += x) ;

    println(sum); sum ← 0 for x ∈ s do sum ← sum + x end printLine(sum)
  7. a = new int[1]; s.stream() .foreach(x –> a[0] += x)

    ; println(a[0]); sum ← 0 for x ∈ s do sum ← sum + x end printLine(sum)
  8. a = new int[1]; s.stream() .filter(x >= 0) .foreach(x –>

    a[0] += x) ; println(a[0]); sum ← 0 for x ∈ s, x ≥ 0 do sum ← sum + x end printLine(sum)
  9. sum ← 0 for x ∈ s do sum ←

    sum + x if sum ≥ limit then break end end printLine(sum)
  10. • List.of(…) • Set.of(…) • Map.of(k 1 , v 1

    , … , k 10 , v 10 ) • Map.ofEntries(entry(k 1 , v 1 ), … , entry(k 10 , v 10 ), …)
  11. (1, 2, 2, 3) {1, 2, 3} {1 ↦ ‘a’,

    2 ↦ ‘b’, 3 ↦ ‘c’} ⦃1, 2, 2, 3⦄ ⟨1, 2, 2, 3⟩ [1, 2, 2, 3] [1 2 3] [1 2; 3 4] Tuple Set Map Bag List Array Vector Matrix
  12. • {1, … , n} • {1, 3, … ,

    n} • {a, a + Δ, … , b} • {a, a − Δ, … , b} • {a, …} • {a, a + Δ, … } • {…, −1, 0, +1, …} • {a, b, c, d, e, f, g} • {1, 2, 3, 4, 5, 6, 7} • {1, … , 5; 7, 8; 10, … , 15; 20, … , 25; 30, 31}
  13. x_class := [ CA FE BA BE 00 00 00

    35 00 05 07 00 03 07 00 04 01 00 01 58 01 00 10 6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74 06 01 00 01 00 02 00 00 00 00 00 00 00 00 ] 16 x_class : Array⟦ 1 ⟧ = [ (CA) 16 , (FE) 16 , (BA) 16 , (BE) 16 , … ]
  14. {(x 1 , x 2 , … , x n

    ) | x 1 ∈ C 1 , p 1 (x 1 ); x 2 ∈ C 2 (x 1 ), p 2 (x 1 , x 2 ); … ; x n–1 ∈ C n–1 (x 1 , x 2 , … , x n–2 ), p n–1 (x 1 , x 2 , … , x n–1 ); x n ∈ C n (x 1 , x 2 , … , x n ), p n (x 1 , x 2 , … , x n ) }
  15. {(x, y, z) ∣ x ∈ S, y ∈ S,

    z ∈ S, x² + y² = z²} where S := {1, … , N}
  16. {(x, y, z) ∣ x ∈ S, y ∈ S,

    z ∈ S, x² + y² = z²} where S := {1, … , N} ⟨(x, y, z) ∣ x ∈ {1, … , N}, y ∈ {x, … , N}, z ∈ {y, … , N}, x² + y² = z² ⟩
  17. {(x, y, z) ∣ x ∈ S, y ∈ S,

    z ∈ S, x² + y² = z²} where S := {1, … , N} ⟨(x, y, z) ∣ x ∈ {1, … , N}, y ∈ {x, … , N}, z ∈ {y, … , N}, x + y ≤ z, x² + y² = z² ⟩
  18. C 1 := {1, 2, 3} C 2 := {2,

    3, 4} assert {x | x ∈ C 1 ∧ x ∈ C 2 } = {2, 3} assert {x | x ∈ C 1, x ∈ C 2 } = {2, 3} assert {x | x ∈ C 1, y ∈ C 2 } = {1, 2, 3} assert {(x, y) | x ∈ C 1, y ∈ C 2 } = {(1, 2), (1, 3), (1, 4), (2, 2), …} assert {x | x ∈ C 1 ∨ x ∈ C 2 } = {1, 2, 3, 4} assert ⟨x | x ∈ C 1 ∨ x ∈ C 2 ⟩ = ⟨1, 2, 3, 2, 3, 4⟩ assert ⟨x | x ∈ C 1 ∘ C 2 ⟩ = ⟨1, 2, 3, 2, 3, 4⟩ assert ⟨x | x ∈ C 1 ‡ C 2 ⟩ = ⟨1, 2, 2, 3, 3, 4⟩
  19. • s.count() • s.findAny() • s.findFirst() • s.min(c) • s.max(c)

    • s.skip(n) • s.dropWhile(p) • s.takeWhile(p) • s.reduce(…) • s.collect(…) • s.limit(n)
  20. {1, 2, 3} ⦃1, 2, 2, 3⦄ ⟨1, 2, 2,

    3⟩ [1, 2, 2, 3] • ordered skip(n) dropWhile(p) takeWhile(p) • others ⟪⟫❨❩❲❳⁅ ⁆⸦⸧⦅⦆⦇⦈⦉⦊⦋ ⦌⦍ ⦎⦏ ⦐⦑⦒⦓⦔⦕⦖⸢⸣⸤⸥⸨⸩⟬⟭⟮⟯⌈⌉⌊⌋ unordered, unique unordered ordered ordered
  21. ∑[1 ≤ k ≤ n] 2k ∑{k | k ∈

    {1, … , n}} 2k ∑⟨(i, j) | i ∈ ⟨1, … , n⟩, j ∈ ⟨1, … , m⟩⟩ a[i][j]
  22. S := {1, … , N} S ∖ {d ∣

    a ∈ S, b ∈ S, c ∈ S, d ∈ S, a² + b² + c² = d²}
  23. S := {1, … , N} S ∖ {d ∣

    a ∈ S, b ∈ S, c ∈ S, d ∈ S, a² + b² + c² = d²} {d ∣ d ∈ S, ∄(a ∈ S) ∄(b ∈ S) ∄(c ∈ S) a² + b² + c² = d²}
  24. S := {1, … , N} S ∖ {d ∣

    a ∈ S, b ∈ S, c ∈ S, d ∈ S, a² + b² + c² = d²} {d ∣ d ∈ S, ∄(a ∈ S) ∄(b ∈ S) ∄(c ∈ S) a² + b² + c² = d²} {d ∣ d ∈ S, ∀(a ∈ S) ∀(b ∈ S) ∀(c ∈ S) a² + b² + c² ≠ d²}
  25. S := {1, … , N} S ∖ {d ∣

    a ∈ S, b ∈ S, c ∈ S, d ∈ S, a² + b² + c² = d²} {d ∣ d ∈ S, ∄(a ∈ S) ∄(b ∈ S) ∄(c ∈ S) a² + b² + c² = d²} {d ∣ d ∈ S, ∀(a ∈ S) ∀(b ∈ S) ∀(c ∈ S) a² + b² + c² ≠ d²} {d ∈ S ∣ ∀(a ∈ S) ∀(b ∈ S) ∀(c ∈ S) a² + b² + c² ≠ d²}
  26. 100 % println( IntStream.rangeClosed(1, N).filter(d –> IntStream.range(1, d).allMatch(a –> IntStream.range(a, d).allMatch(b

    –> IntStream.range(b, d).allMatch(c –> a * a + b * b + c * c != d * d; ) ) ) ) .collect(toList()) );
  27. 100 % println( IntStream.rangeClosed(1, N).filter(d –> { return IntStream.range(1, d).allMatch(a –>

    { return IntStream.range(a, d).allMatch(b –> { return IntStream.range(b, d).allMatch(c –> { return a * a + b * b + c * c != d * d; }); }); }); }) .collect(toList()) );
  28. ~70 % println( IntStream.rangeClosed(1, N).filter(d –> { int dd = d

    * d; return IntStream.range(1, d).allMatch(a –> { int aa = a * a; return IntStream.range(a, d).allMatch(b –> { int aa_bb = aa + b * b; return IntStream.range(b, d).allMatch(c –> { return aa_bb + c * c != dd; }); }); }); }) .collect(toList()) );
  29. {d ∈ S ∣ ∀(a ∈ S) ∀(b ∈ S)

    ∀(c ∈ S) a² + b² + c² ≠ d²} where S := {1, … , N}
  30. ~7 % {d ∈ {1, … , N} ∣ ∀(a ∈

    {1, … , d}) ∀(b ∈ {a, … , d}) ∀(c ∈ {b, … , d}) a² + b² + c² ≠ d² }
  31. ~6 % {d ∈ {1, … , N} ∣ ∀(a ∈

    {1, … , d} where dd := d²) ∀(b ∈ {a, … , d} where aa := a²) ∀(c ∈ {b, … , d} where aa_bb := aa + b²) aa_bb + c² ≠ dd }
  32. public static boolean isFlush(Hand hand) { return hand.stream() .allMatch( c

    –> hand.stream() .filter(cc –> cc != c) .allMatch(cc –> cc.suit == c.suit) ) ; }
  33. public static boolean isFlush(Hand hand) { Card arb = hand.stream().findAny().get();

    return hand.stream() .filter(c –> c != arb) .allMatch(c –> c.suit == arb.suit) ; }
  34. public static boolean isFlush(Hand hand) { Card arb = hand.stream().findAny().get();

    Suit suit = arb.suit; return hand.stream() .filter(c –> c != arb) .allMatch(c –> c.suit == suit) ; }
  35. function isFlush(Hand hand) → is return ∀(c ∈ hand) ∀(cc

    ∈ hand, cc ≠ c) ( cc.suit = c.suit ) end
  36. function isFlush(Hand hand) → is return ( ∀(cc ∈ hand

    where c := arb(hand), cc ≠ c) cc.suit = c.suit ) end
  37. function isFlush(Hand hand) → is return ( ∀( cc ∈

    hand where (c := arb(hand); suit := c.suit), cc ≠ c ) cc.suit = suit ) end
  38. function isFlush(Hand hand) → is return ( ∀( cc ∈

    hand where c := arb(hand), suit := c.suit, cc ≠ c ) cc.suit = suit ) end
  39. function fourOfAKind(Hand hand) → is return ( ∃(s ∈ ℘₄(hand))

    ∀(c ∈ s where r := arb(s).rank) (c.rank = r) ) end