Czego Javowiec nauczy się
od Haskella?

Confitura 2015

Tomasz Nurkiewicz

nurkiewicz.com | nurkiewicz@gmail.com

@tnurkiewicz | @4financeIT

Nie znam Haskella


twitter.com/miketaylr/status/569212319864991744

Czego nie ma Haskell?

  • null
  • zmienne
  • wyjątki
  • void
  • Object
  • pętle
  • przeciążanie metod
  • efekty uboczne

System typów

Hoogle


[a] -> (a -> b) -> [b]

[a] -> (a -> Bool) -> Bool

Ord a => [a] -> [a]
					

Structural search

IntelliJ IDEA


class $Class$ {
	public static int $method$(String $string$, 
	                           int $def$)
}
					

twitter.com/jessitron/status/529652972679331840

Świat wybiera


Source: Are static typing and functional programming winning?

twitter.com/natpryce/status/557553705109950464

twitter.com/velartrill/status/615714789499637760

Stringly typed


String uuid;
String website;
String email;
String firstName, lastName;
String clientXml;
int birthYear;
long registrationDate;
int min, max;
boolean registered;
int priority;
					

Strongly typed


UUID uuid;
URL website;
Email email;
Name name;
Optional<Document> client;
Year birthYear;
Instant registration;
Range<Integer> range;
Status status;
Priority priority;
					

Żywa dokumentacja


Try<InputStream> tryOpen(File f) {
	return Try.ofFailable(
		() -> new FileInputStream(f));
}
					
github.com/jasongoodwin/better-java-monads

CompletableFuture<String> callWebService() {
	//...
}
					

null


https://www.facebook.com/programistaplakaljakcommitowal

20 lat Javy


twitter.com/ags313/status/598494588135985152

50 lat nulla


cake.sharonmattnadia.com/2008/10/cake-in-bag.html

infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare

Tony Hoare (2009)

"[null] led to innumerable errors, vulnerabilities, and system crashes,
which have probably caused a billion dollars of pain and damage [...]"

From "Java 8 in Action"

  • [...] NullPointerException is by far the most common exception in Java.
  • [...] worsens readability by [filling] your code with often deeply nested null checks.
  • [...] It doesn’t have any semantic meaning [...]
  • [...] Java always hides pointers from developers except in one case: the null pointer.
  • [...] It creates a hole in the type system. [...] it can be assigned to any reference type

twitter.com/bsletten/status/587441266863943680

twitter.com/shipilev/status/553566131194454016

niebezpiecznik.pl/post/nullowy-sms-od-orange

niebezpiecznik.pl/post/pociag-do-nulla

http://stackoverflow.com/questions/4456438

org.apache.catalina.connector.CoyoteAdapter

Option(al)?


Optional<String> opt = null;  //YOLO
					

scala> Some(null) == None
res1: Boolean = false
					

Kotlin

Demo

Domyślnie not-null

	
val s: String = "abc"
val x = s.length()

						

Nie kompiluje się

	
val s: String? = null
val x = s.length()

						

Sprawdzenie

	
val s: String? = "abc"
val x: Int? = s?.length()

if (s != null) {
    val x2: Int = s.length()
}
						

Immutability


twitter.com/kellabyte/status/552509551795535874

Bazy danych

  • Cassandra
  • HDFS
  • Datomic

1994


Source: www.pearsoned.co.uk

Wzorzec Flyweight

A flyweight [...] minimizes memory use by sharing as much data as possible with other similar objects;
[...] simple repeated representation would use an unacceptable amount of memory.
[...] Flyweight objects must be immutable.

Source: wikipedia.org/wiki/Flyweight_pattern

2013


Source: vaughnvernon.co/?page_id=168

value object - 258 razy

immutable - 68 razy

"Implementing DDD"

[...] model using Value Objects instead of Entities wherever possible.

[...] Entity’s design should be biased toward serving as a Value container

[...] Value types that measure, quantify, or describe things are easier to create, test, use, optimize, and maintain.


From chapter 6

git


git-scm.com/book/en/v2/Git-Internals-Git-Objects

Linus Torvalds

[...] performance almost always matters.
And I absolutely detest the fact that people [...] dismiss performance concerns so readily.

Git mailing list, Fri, 8 Aug 2008

youtube.com/watch?v=1PhArSujR_A

Wolfenstein 3D (1992)


wikipedia.org/wiki/Wolfenstein_3D

float Q_rsqrt( float number )
{
  long i;
  float x2, y;
  const float threehalfs = 1.5F;

  x2 = number * 0.5F;
  y  = number;
  i  = * ( long * ) &y;         // evil floating point 
                                // bit level hacking
  i  = 0x5f3759df - ( i >> 1 ); // what the fuck? 
  y  = * ( float * ) &i;
  y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

  return y;
}					
wikipedia.org/wiki/Fast_inverse_square_root

twitter.com/id_aa_carmack/status/331918309916295168

Czemu?

  • Zapis stanu gry
  • Gra sieciowa

Podsumowanie

  • null
  • zmienne
  • wyjątki
  • void
  • Object
  • pętle
  • przeciążanie metod
  • efekty uboczne

Dziękuję!


nurkiewicz.github.io/talks/2015/haskell