Skip to content

Instantly share code, notes, and snippets.

View meowpunch's full-sized avatar
🏄
Enjoy Development

Jinhoon Bae meowpunch

🏄
Enjoy Development
View GitHub Profile
@meowpunch
meowpunch / ProceduralShape.java
Last active January 10, 2022 17:48
Procedural Shape, Chapter 6: Objects and Data Structures, Clean Code, Robert C Martin
public class Square {
public Point topLeft;
public double side;
}
public class Rectangle {
public Point topLeft;
public double height;
public double width;
}
@meowpunch
meowpunch / PolymorphicShape.java
Created January 10, 2022 17:49
Polymorphic Shape, Chapter 6: Objects and Data Structures, Clean Code, Robert C Martin
public class Square implements Shape {
private Point topLeft;
private double side;
public double area() {
return side * side;
}
}
public class Rectangle implements Shape {
@meowpunch
meowpunch / Geometry.java
Last active January 11, 2022 23:18
Improve geometry class with pattern matching, seal classes and records
sealed interface Shape {}
record Point(double x, double y) {}
record Square (Point topLeft, double side) implements Shape {}
record Rectangle (Point topLeft, double height, double width) implements Shape {}
record Circle (Point center, double radius) implements Shape {}
@meowpunch
meowpunch / Geometry.scala
Created January 11, 2022 23:17
improved geometry class with deconstruction patterns
sealed trait Shape
case class Point(x: Double, y: Double) {}
case class Square(topLeft: Point, side: Double) extends Shape {}
case class Rectangle(topLeft: Point, height: Double, width: Double) extends Shape {}
case class Circle(center: Point, radius: Double) extends Shape {}
@meowpunch
meowpunch / is-operator.kts
Created January 12, 2022 06:56
Smart casts in Kotlin
// x is automatically cast to String
if (obj is String) { return obj.length }
x is String && x.length == 0
// compile error
x is String || x.length == 0
// with `when` expression
when (x) {
is String -> x.length
@meowpunch
meowpunch / InstanceofOperator.java
Last active January 12, 2022 07:24
Type patterns, `instanceof` operator, Java
// without type patterns
if (obj instanceof String) { return ((String) s).length(); }
// type patterns, cast to String and assign to s
if (obj instanceof String s) { return s.length(); }
x instanceof String s && s.length()
// compile error, out of scope
x instanceof String s || s.length()
@meowpunch
meowpunch / SwitchEnum.java
Last active January 12, 2022 14:29
switch statements in Java
enum DayOfWeek { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }
public class SwitchEnum {
// switch statement
public boolean isWeekendStatement(DayOfWeek day) throws NoSuchDayOfWeekException {
boolean isWeekend;
switch (day) {
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY:
isWeekend = false;
@meowpunch
meowpunch / RecordPatterns.java
Last active January 13, 2022 08:17
an example of JEP 405: Record Patterns & Array Patterns (Preview)
record Point(int x, int y) {}
enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) {}
record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {}
static void printXCoordOfUpperLeftPointBeforePatterns(Rectangle r) {
if (r == null) return;
ColoredPoint ul = r.upperLeft();
if (ul == null) return;
Point p = ul.p();