Skip to content

Instantly share code, notes, and snippets.

@mcelaney
Created May 30, 2017 11:31
Show Gist options
  • Save mcelaney/aa8a12dbeeda8c1519bdeb7dede8b5a5 to your computer and use it in GitHub Desktop.
Save mcelaney/aa8a12dbeeda8c1519bdeb7dede8b5a5 to your computer and use it in GitHub Desktop.

Erlang

-module( geometry1).
-export([ test/ 0, area/ 1]).
test() ->
  12 = area({ rectangle, 3, 4}),
  144 = area({ square, 12}),
  tests_worked.

area({ rectangle, Width, Height}) -> Width * Height;
area({ square, Side}) -> Side * Side.
area({ circle, Radius}) -> 3.14159 * Radius * Radius;

Java

abstract class Shape {
  abstract double area();
}

class Circle extends Shape {
  final double radius;
  Circle( double radius) { this.radius = radius; }
  double area() { return Math.PI * radius* radius; }
}
 
class Rectangle extends Shape {
  final double ht;
  final double width;
  Rectangle( double width, double height) {
    this.ht = height;
    this.width = width;
  }
  double area() { return width * ht; }
} 

class Square extends Shape {
  final double side;
  Square( double side) {
    this.side = side;
  }
  double area() { return side * side; }
} 

"If you compare the Erlang code with Java code, you’ll see that in the Java program the code for area is in three places. In the Erlang program, all the code for area is in the same place."

Armstrong, Joe (2013-09-23). Programming Erlang: Software for a Concurrent World (Pragmatic Programmers) (p. 49). Pragmatic Bookshelf. Kindle Edition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment