Skip to content

Instantly share code, notes, and snippets.

View wcang's full-sized avatar
🌴
On vacation

Ang Way Chuang wcang

🌴
On vacation
View GitHub Profile
@wcang
wcang / Main.java
Created April 25, 2022 12:00
PrimeNumberCollector that collects prime numbers into a list from an Integer stream. OptimizedPrimeNumberCollector is an optimized version of the collector using the accumulated list of prior prime numbers to weed out a prime number candidate. Refers to Java 8 In Action for details
package com.wcang;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
@wcang
wcang / AddTwoNumberList.kt
Last active April 23, 2022 13:52
Add Two Numbers. Problem is described here: https://leetcode.com/problems/add-two-numbers/
import kotlin.math.max
fun add(left: List<Int>, right: List<Int>) : List<Int> {
val l = left.asReversed()
val r = right.asReversed()
val result = mutableListOf<Int>()
val max = max(l.size, r.size)
var carry = 0
repeat(max) {
@wcang
wcang / Finally.java
Created February 20, 2022 13:05
I just realized that the code in finally block will be called when if the method exits in prior code block
public class Finally {
public static int tryFinally() {
try {
return 1;
} finally {
System.out.println("Finally block");
}
}
@wcang
wcang / BoardImpl.kt
Created October 17, 2021 02:36
Solution to Coursera's Kotlin for Java Developer Week 4 Board Assignment
package board
import board.Direction.*
fun createSquareBoard(width: Int): SquareBoard = ConcreteSquareBoard(width)
fun <T> createGameBoard(width: Int): GameBoard<T> = ConcreteGameBoard(createSquareBoard(width))
private fun Cell.coordinate(direction: Direction) =
when (direction) {
open class Something<WTF>(val wtf: WTF)
{
init {
println("init something")
}
}
interface Blargh<T> {
fun getArg(): T
}
@wcang
wcang / elojs_13.1.js
Created January 19, 2015 17:41
Exercise 13.1 for Eloquent Javascript
<style>
/* Defines a cleaner look for tables */
table { border-collapse: collapse; }
td, th { border: 1px solid black; padding: 3px 8px; }
th { text-align: left; }
</style>
<script>
var MOUNTAINS = [
{name: "Kilimanjaro", height: 5895, country: "Tanzania"},
@wcang
wcang / vector.js
Last active August 29, 2015 14:12
Javascript Object Orientation with getter function
// Your code here.
function Vector(x, y) {
this.x = x;
this.y = y;
this.plus = function(v) {
var res = new Vector(this.x, this.y);
res.x += v.x;
res.y += v.y;
return res;
@wcang
wcang / deepEqual.js
Created December 20, 2014 06:49
Deep comparison in Javascript
function deepEqual(obj1, obj2)
{
if (obj1 === obj2) {
return true;
}
if (typeof(obj1) == typeof(obj2)) {
if (typeof(obj1) == 'object') {
var obj1Prop = 0;
var obj2Prop = 0;
@wcang
wcang / fork.c
Created February 19, 2014 14:23
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
static void child_process(int fd)
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h> /* this is needed for struct sockaddr_in (IPv4 address) */
#include <unistd.h> /* close syscall */
#include <arpa/inet.h> /* for inet_ntop */