Skip to content

Instantly share code, notes, and snippets.

View hackjutsu's full-sized avatar

CosmoX hackjutsu

View GitHub Profile
@hackjutsu
hackjutsu / bb.js
Last active October 10, 2017 19:33
[Serialize bluebird promise] each() vs mapSeries() vs map() #promise #bluebird
var Promise = require('bluebird');
var funcs = Promise.resolve([500, 100, 400, 200].map((n) => makeWait(n)));
funcs
.each(iterator) // logs: 500, 100, 400, 200
.then(console.log) // logs: [ [Function], [Function], [Function], [Function] ]
funcs
.mapSeries(iterator) // logs: 500, 100, 400, 200
@hackjutsu
hackjutsu / typedef_for_func_ptr.cpp
Last active June 1, 2017 05:24
[Function pointer example] Function #pointers are somewhat different than all other types because the syntax does not follow the pattern typedef <old type name> <new alias>; Instead, the new alias for the type appears in the middle between the return type (on the left) and the argument types (on the right).
typedef int (*MathFunc)(float, int);
int do_math(float arg1, int arg2) {
return arg2;
}
int call_a_func(MathFunc call_this) {
int output = call_this(5.5, 7);
return output;
}
@hackjutsu
hackjutsu / TmpDirExample.java
Last active June 7, 2020 00:37
Use java.io.tmpdir to get system tmp directory.
import java.io.File;
import java.io.IOException;
public class TmpDirExample {
public static void main(String[] args) {
String tmpdir = System.getProperty("java.io.tmpdir");
System.out.println("The default value of the java.io.tmpdir system property is: \""
+ tmpdir + "\"\n");
@hackjutsu
hackjutsu / correct_implementation_equal_hashcode.java
Last active January 23, 2017 06:46
[Snippet for correct implementation of equals() and hashcode()] http://www.javaranch.com/journal/2002/10/equalhash.html
public class Test {
private int num;
private String data;
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if ((obj == null) || (obj.getClass() != this.getClass())) {
@hackjutsu
hackjutsu / html_encoding_decoding.py
Last active January 23, 2017 06:46
[HTML encoding/decoding via Python standard library] Reference: http://stackoverflow.com/a/7088472
# HTML Encoding
try:
from html import escape # python 3.x
except ImportError:
from cgi import escape # python 2.x
print(escape("<"))
# HTML Decoding
try:
@hackjutsu
hackjutsu / check_file_exist.py
Created September 13, 2016 21:45
Check whether a file exists using Python
# Python 2.x
import os.path
os.path.isfile(fname)
# Python 3.4
from pathlib import Path
my_file = Path("/path/to/file")
if my_file.is_file():
# file exists
@hackjutsu
hackjutsu / main_func.py
Last active January 23, 2017 06:45
[Define main() function for Python] We can run the script from terminal like: python main_func.py
# $ python main_func.py
def main():
print("Hello from main()")
if __name__ == '__main__':
main()
@hackjutsu
hackjutsu / file_reading.py
Created September 13, 2016 21:56
Reading a file in Python
## Open the file with read only permit
f = open('myTextFile.txt', "r")
## use readlines to read all lines in the file
## The variable "lines" is a list containing all lines
lines = f.readlines()
## close the file after reading the lines.
f.close()
@hackjutsu
hackjutsu / functional_map.py
Created September 13, 2016 22:09
Demo for functional programming map() in Python
def square(x):
return x**2
squares = map(square, range(10))
@hackjutsu
hackjutsu / regex.py
Created September 13, 2016 22:12
Demo for using regex in Python
import re
testStr = "//gss0.baidu.com/6b1IcTe9R1gBo1vgoIiO_jowehsv/maps/services/thumbnails?width=525&height=295&quality=100&align=middle,middle&src=http://gss0.baidu.com/7LsWdDW5_xN3otqbppnN2DJv/lvpics/pic/item/8cb1cb134954092315ec8fd29258d109b2de4991.jpg"
pattern = "http.*jpg"
m = re.search(pattern, testStr)
print(m.group(0))
# http://gss0.baidu.com/7LsWdDW5_xN3otqbppnN2DJv/lvpics/pic/item/8cb1cb134954092315ec8fd29258d109b2de4991.jpg