Skip to content

Instantly share code, notes, and snippets.

Avatar

CosmoX hackjutsu

View GitHub Profile
@hackjutsu
hackjutsu / bb.js
Last active Oct 10, 2017
[Serialize bluebird promise] each() vs mapSeries() vs map() #promise #bluebird
View bb.js
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 Jun 1, 2017
[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).
View typedef_for_func_ptr.cpp
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 Jun 7, 2020
Use java.io.tmpdir to get system tmp directory.
View TmpDirExample.java
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");
View correct_implementation_equal_hashcode.java
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 Jan 23, 2017
[HTML encoding/decoding via Python standard library] Reference: http://stackoverflow.com/a/7088472
View html_encoding_decoding.py
# 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 Sep 13, 2016
Check whether a file exists using Python
View check_file_exist.py
# 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 Jan 23, 2017
[Define main() function for Python] We can run the script from terminal like: python main_func.py
View main_func.py
# $ python main_func.py
def main():
print("Hello from main()")
if __name__ == '__main__':
main()
@hackjutsu
hackjutsu / file_reading.py
Created Sep 13, 2016
Reading a file in Python
View file_reading.py
## 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 Sep 13, 2016
Demo for functional programming map() in Python
View functional_map.py
def square(x):
return x**2
squares = map(square, range(10))
@hackjutsu
hackjutsu / regex.py
Created Sep 13, 2016
Demo for using regex in Python
View regex.py
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