Skip to content

Instantly share code, notes, and snippets.

View cjackie's full-sized avatar

cjackie cjackie

  • New York
View GitHub Profile
@cjackie
cjackie / demo.py
Last active December 11, 2021 03:34
Python asyncio demo
import time
import asyncio
async def async_call():
print("async_call")
async def main():
# Schedule a async_call()
task = asyncio.create_task(async_call())
# await task
#solution to data science class.
import MapReduce
import sys
ROW = 5
COL = 5
mr = MapReduce.MapReduce()
def mapper(entry):
matrix = entry[0]
@cjackie
cjackie / email.md
Last active August 29, 2015 14:04
what I learned today from arduino

I just wanna send a freaking email

Today at work, we tried to make a alarm for our arduino system. It's going to be text message or email. Ironically, arduino with gsm connect from T-mobile won't be able to send the message! So, we had to found another way.
We knew how to make a http connection through arduino gsm shield. So, we are going to take that as the base to send some kind of "alarm". Here are some proposals we had:

  • find a vendor that will provide restful API to send email or text message
  • connect email directly from arduino.

As a result, we spent a lot of time looking for a good vendor.. Good means free vendor. LMAO. It turned out they all need monthly fee, which is very cheap. After exploring their APIs, which obviously has not arduino support. So we had to found out how http request API works underneath the hood, which I'm very glad we did. In this process, we, at least I, learned alot about http protocol and how it works.

How http works.

Have worked with http for a long time, s

@cjackie
cjackie / www.md
Last active August 29, 2015 14:03
how html, css and js work in browsers

Introduction

We often take things for granted, and web is one of them. Lots of things going on behind the scene when you open up a website. Once you understand what's going on underneath the hook, you will be able to optimize your website, and make the best out of browsers.

Brief History

At the beginning of Internet, Browsers were implemented to process a protocol called HTTP. Back then, web pages were all static, which means once you load the content, it won't change. Then Javascript came around, which makes it possible to change elements on the page dynamically. Javascript wasn't taken seriously until the book called JavaScript: The Good Parts in 2008. Later we saw a huge progress in web is made in last few years. Websites became more interactive, more dynamic, and more complex.

How websites work in browsers

Here I'm going to make a over-simplified version of what's going on. Although different browsers works different, but they all follow more or less same blue-print. First, when you open up

@cjackie
cjackie / handleAsyncCode.md
Last active August 29, 2015 14:03
promise vs function generator for handling async functions in a sync way.

Yesterday, I came across function generators in Javascript, which is very similar to Python's function generator. The function generator is characterized by the keyword yield, which will suspend the function when it's called with next(). That's my simple understanding of function generator. I think it's trying to mimic lazy evaluation in languages like Haskell, and this makes infinite data structure easy to be expressed.
Anyway, I found function generator was because I found koa is using this new feature from Ecmascript 6 to construct async codes in a sync way. I didn't understand how koa can achieve it using yield at first (even though I understand function generator). But after I searched more info about it, I started to understand it. From what I understand, koa take all generator functions and call them behind the scene until no more yield. I think it's very neat solution to avoid callback hell and makes asynchronous code more readable if you want to make them ex

前几天,我在hacker news上看到一个人的博客,我感觉挺值得分享的。在他那个博客里,主要说了自己在做程序员那段时间里所学到的9点东西。在那9样中,我感觉第一项与第八项目最为重要了。
第一项,他说,Intelligence is overrated。中文来说就是,一个人的智商其实没那么重要。我非常的同意这一点。一个人如果很聪明,他考试可以考的很好,学起来可以十分的轻松。对于脑袋不是很灵活的人,可能他学的比较吃力,学不好。不过,后者很可能会比前者学到很多更重要的东西:努力,坚持不懈,等等。在社会中,后者也更容易适应下来。对于学习一帆风顺的人,一旦面对困难就很容易退缩下来。然而,在社会里,困难挫折这多不能免掉的。相对于聪明,努力与坚持不懈的品格的人更容易成功(事业上)。
第八项,自己看吧。我总是做不到的一项。不过我会努力去做到的。

@cjackie
cjackie / AOP_in_javacript.md
Last active August 29, 2015 14:03
demonstrate aspect-oriented programming in javascript

##Aspect-Oriented Programming It might sound complicated, but the idea is in fact very simple, which is adding new behavior to a function. This can be done using in object-oriented way, but for some problems, AOP is a better solution. Let's say you would like to print something before the call, and print something after that call for debugging purposes. Of course you can just make a subclass and add those functionality. However, it feels very unnatural. With AOP, it's straight forward: add one behavior before the call and another one after the call. The abstraction really free you from thinking details about class relationships, and other methods, and make you focus more on what the code does.
Let's give an example of AOP:

function Person(name) { 
  this.name = name
}

Person.prototype.say = funciton() {
 console.log(this.name);
@cjackie
cjackie / break.js
Last active August 29, 2015 14:02
lol... not working.. but it's well written..
//config. this what you can modify
var config = {
elmType: ["a"],
elmNum: 20,
speed: 2,
timeInterval: 200
};
/**