Skip to content

Instantly share code, notes, and snippets.

View limabeans's full-sized avatar

Angel Lim limabeans

View GitHub Profile
@limabeans
limabeans / callback.js
Last active August 27, 2016 02:29
tricky callback situations
/*
* Prints 'here', first, then roughly less than 1000 ms later, prints a b c d.
* For each element in the array, a setTimeout callback is set up, but since there's a 1000 ms delay, that function isn't
* called yet. The forEach won't block on any of these callback setups, so it skips through and prints 'here' first.
*/
function ex1() {
var arr = ['a', 'b', 'c', 'd'];
arr.forEach(function(e) {
setTimeout(function() {
console.log(e);
@limabeans
limabeans / euler.py
Created September 7, 2016 02:52
euler rotations for robotics
import numpy as np
# NOTE: numpy uses radians for its angles
# constants
PI = np.pi
cos = lambda ang: np.cos(ang)
ncos = lambda ang: -1*np.cos(ang)
sin = lambda ang: np.sin(ang)
nsin = lambda ang: -1*np.sin(ang)
@limabeans
limabeans / cell-segue.swift
Created November 13, 2016 02:32
UICollectionViewCell and segue
/*
There is a button in each UICVCell, and we want to pass data based on a specific cell when we segue.
*/
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destController = segue.destination as! CommentController
if let selectedFeedCell = (sender! as AnyObject).superview!?.superview as? FeedCell {
let indexPath = self.collectionView!.indexPath(for: selectedFeedCell)!
let row = indexPath.row
destController.feedItemInd = row
@limabeans
limabeans / cv.swift
Created November 17, 2016 07:03
embedded CV
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var embeddedCV: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
// make ourselves the delegate and data source of the CV.
embeddedCV.delegate = self
@limabeans
limabeans / bind.js
Created November 1, 2017 03:39
bind in js
var a=2;
var obj={a: 3};
function getA() { return this.a; }
console.log(getA()); // 2
var tmp=getA.bind(obj);
console.log(tmp()); // 3
@limabeans
limabeans / button.html
Last active November 1, 2017 03:47
several examples of how context in event handler function actually changes
for reference: https://stackoverflow.com/questions/5490448/how-do-i-pass-the-this-context-into-an-event-handler
ex1
<html>
<body>
<button id="but">button</button>
<script>
var but=document.getElementById('but');
function Button() {
this.init = function() {
@limabeans
limabeans / App.js
Last active November 4, 2017 23:11
react redux todolist in 2 files, i will inevitably forget syntax, plz refer here
import React, { Component } from 'react';
import logo from './logo.svg';
import { connect } from 'react-redux';
import './App.css';
let nextTodoId = 0
const addTodo = (text) => ({
type: 'add',
id: nextTodoId++,
@limabeans
limabeans / UnionFind.py
Created November 7, 2017 21:09
union find data structure in python
class UnionFind:
def __init__(self, N):
self.parent=[i for i in range(N+1)]
def findParent(self, v):
if self.parent[v]==v: return v
else: return self.findParent(self.parent[v])
def union(self, v, w):
vp=self.findParent(v)
wp=self.findParent(w)
self.parent[vp]=wp
@limabeans
limabeans / UnionFind.cpp
Created November 7, 2017 22:00
union find in c++
class UnionFind {
public:
vector<int> parent;
int getParent(int v) {
if (parent[v]==v) return v;
return getParent(parent[v]);
}
void unionn(int v, int w) {
int vp=getParent(v);
int wp=getParent(w);
@limabeans
limabeans / cab.js
Created November 14, 2017 03:29
call,apply,bind in js
// CallApplyBind examples, cuz i occassionally forget call and apply
function fn(x,y) {
return x+y+this.z
}
var obj={ z: 4 };
var objfn=fn.bind(obj);