Skip to content

Instantly share code, notes, and snippets.

WJH wongjiahau

Block or report user

Report or block wongjiahau

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Shape8.ts
const getArea = (shape: Shape) => {
switch(shape.label) {
case 'circle': return Math.PI * Math.pow(shape.radius, 2)
case 'triangle': return 0.5 * shape.base * shape.height
case 'rectangle': return shape.height * shape.width
}
}
View Shape7.ts
const newShape2: Shape = {
label: 'circle',
width: 30 // You'll get a compiler error saying `width` does not exists when label is `circle`
}
View Shape6.ts
type Shape = {
kind: 'circle'
radius: number
} | {
kind: 'rectangle'
width: number
height: number
} | {
kind: 'triangle'
base: number
View Shape5.ts
interface Shape {}
class Circle extends Shape {
constructor(public radius: number) {}
}
class Rectangle extends Shape {
construtor(public height: number, public width: number) {}
}
class Triangle extends Shape {
constructor(public height: number, public base: number) {}
}
View Shape4.ts
interface ShapeV2 {
label: 'rectangle' | 'circle' | 'triangle'
radius ?: number // only for circle
height ?: number // only for triangle and rectangle
width ?: number // only for rectangle
base ?: number // only for triangle
}
View Shape3.ts
const newShape1: Shape = { width: 30, base: 40 } // invalid combination of properties
const newShape2: Shape = { height: 40 } // missing base or width
View Shape2.ts
const getArea = (shape: Shape) => {
if (shape.radius) {
return Math.PI * Math.pow(shape.radius, 2)
}
else if (shape.height && shape.width) {
return shape.height * shape.width
}
else if (shape.base && shape.height) {
return 0.5 * shape.base * shape.height
}
@wongjiahau
wongjiahau / Shape1.ts
Last active Jul 2, 2019
Typescript Discriminated Union Example 1
View Shape1.ts
interface Shape {
radius ?: number
height ?: number
width ?: number
base ?: number
}
@wongjiahau
wongjiahau / hm.tex
Last active Mar 23, 2019
The latex code for Hindley Milner Type System.
View hm.tex
\documentclass{article}
\begin{document}
\begin{table}[h!]
\begin{center}
\begin{tabular}{cl}
\\
\(\frac
{x \ : \ \sigma \ \in \ \Gamma}
@wongjiahau
wongjiahau / Main.hs
Created Feb 15, 2019
TeachingHaskell-Exercise 1-Answer
View Main.hs
data Shape
= Circle Float
| Square Float
| Rectangle Float Float
| Null
deriving (Show)
main :: IO ()
main = do
-- Read contents from shape.txt
You can’t perform that action at this time.