Skip to content

Instantly share code, notes, and snippets.

@HaoyangFan
HaoyangFan / this_in_react.md
Created July 15, 2019 01:36 — forked from amitai10/this_in_react.md
How to use "this" keyword in JavaScript in general and react in particular

Using "this” in react functions

JavaScript is great, but comparing to other programing languages it has many confusing parts. One of them is the use of this.

In this blog post I will explain how to use it right, and what are the tools that JavaScript provide us to ease the use of it. In the end, I will focus on react.

3 types of functions

@HaoyangFan
HaoyangFan / PriorityQueue.java
Last active July 8, 2019 00:39
Array-based implementation of binary heap, which can be used as priority queue
import java.util.Arrays;
/**
* Implementation of a generic max heap (priority queue) following the idea learned.
*
* @param <K> Type of element
*/
public class PriorityQueue<K extends Comparable<? super K>> {
// array-based implementation
@HaoyangFan
HaoyangFan / bit_tricks.c
Created January 22, 2019 04:00
Useful Bit Tricks Summary in C (To Be Continued)
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
/**
* References:
* http://www.catonmat.net/blog/low-level-bit-hacks
*
*/
@HaoyangFan
HaoyangFan / QuickSort2.java
Last active December 29, 2018 03:16
Quick Sort Implementation #2 that put logic of partition into a separate function which return partition index
public class QuickSort2 {
/**
* @param A: an integer array
* @return: nothing
*/
public void sortIntegers2(int[] A) {
if (A == null || A.length <= 1) return;
quicksort(A, 0, A.length - 1);
}
@HaoyangFan
HaoyangFan / monotonestack.py
Created December 23, 2018 20:09
Monotone Stack Implementation in Python 3
# Simple implementation of monotone stack using deque
# @author Haoyang Fan
# @since 12-23-2018
# why deque? because it is fast to append and remove on its both end
import itertools
from collections import deque
'''
@HaoyangFan
HaoyangFan / QuickSort3.java
Last active September 27, 2024 07:09
Quick Sort implementation #3 using 3-way partition
// idea from Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne p.298, 299
public class QuickSort3 {
/**
* @param A: an integer array
* @return: nothing
*/
public void sort(int[] A) {
if (A == null || A.length <= 1) return;
quicksort(A, 0, A.length - 1);
}
@HaoyangFan
HaoyangFan / ArrayInitialization.java
Created December 19, 2018 03:21
Quick notes on Java array initialization
public class ArrayInitialization {
public static void main(String[] args) {
// first way of initializing array with values
int[] arr = {1,2}; // NOTE: this only work for declaration
// System.out.println(Arrays.toString({1,2})); will throw error
// second way of initializing array with values
int[] arr2 = new int[]{1,2,3}; // "anonymous array", NOTE: no count inside bracket!
System.out.println(Arrays.toString(new int[]{1,2})); // work perfectly fine
// System.out.println(Arrays.toString(new int[2]{1,2})); // will throw error
@HaoyangFan
HaoyangFan / BlockScope.java
Last active December 19, 2018 02:36
Quick Notes on Java Block Scope
public class BlockScope {
public static void main(String[] args) {
/************ define own block scope without if, for, while is possible ************/
int n = 3;
// here the block will create a new scope for the variables that are declared inside
{
int i = 5;
// int n = 6; variables that have already been declared outside this scope cannot be redeclared here
n++; // access the variables from outer scope is possible though
@HaoyangFan
HaoyangFan / BitwiseOps.java
Last active December 18, 2018 00:31
Quick notes on Java Bitwise Operator
/*
When applied to boolean values, the & and | operators yield a boolean value.
These operators are similar to the && and || operators,
except that the & and |operators are not evaluated in “short circuit” fashion—that is,
both arguments are evaluated before the result is computed.
*/
public class BitwiseOps {
public static void main(String[] args) {
/* Bitwise operators: & (and), | (or), ^ (xor), ~(not) */
System.out.println(true & false); // "AND" : false
@HaoyangFan
HaoyangFan / RecursiveBinarySearch.java
Created November 13, 2018 06:32
A simple implementation of recursive binary search in Java.
/**
* A simple implementation of recursive binary search in Java.
*
* @author Haoyang Fan
* @since 11-12-2018
*/
public class RecursiveBinarySearch {
/**
* @param nums an integer array sorted in ascendin order