↓ Skip to main content
  1. Categories/

工程

2018

react 中setState的更新策略

·138 words·1 min
起因是想更新一个array类型的state,结果setState更新之后用console.log() debug 结果,发现结果特别玄学。。。 查了下发现this.setState是个异步操作。。。

learn java in 21 minutes for C++ Programmers

·761 words·2 mins
先放资料: Learning a New Programming Language: Java for C++ Programmers java package # 先说几条重要的人话: 一个 java 文件第一行可以声明该文件所属于的 package,package 的名字必须与整个工作目录的路径名相同。 同一个 package 下的 class 默认有互相访问的权限。 访问属性设置为 public 的 class,如果该 class 所在的 file 声明了 package,那么可以被其他 package 下的 class 访问到。 .java 的文件名必须与文件中设置为 public 的 class 名保持一致(如果没有 public 的类,那么名称任意)。 Every class is part of some package. All classes in a file are part of the same package. You can specify the package using a package declaration (package name;) as the first (non-comment) line in the file. Multiple files can specify the same package name. If no package is specified, the classes in the file go into a special unnamed package (the same unnamed package for all files). If package name is specified, the file must be in a subdirectory called name (i.e., the directory name must match the package name). You can access public classes in another (named) package using package-name.class-name. You can access the public fields and methods of such classes using package-name.class-name.field-or-method-name. You can avoid having to include the package-name using import package-name.*; or import package-name.class-name; at the beginning of the file (after the package declaration). The former imports all of the classes in the package, and the second imports just the named class. You must still use class-name to access the classes in the packages, and class-name.field-or-method-name to access the fields and methods of the class; the only thing you can leave off is the package name. 下面是一些例子:

前端To do list

·199 words·1 min
20181014update: 可以不写了,开心 迫于生计,要从零开始学习前端。 由于之前的to do list 年代久远+ 前端的技术栈可之前几乎没有关系,因此新开一篇记录。 * css盒子模型 * 布局,flex * 前端debug的方法 * <del>javascript,</del>有空可以使用js刷leetcode练习语法 * <del>typescript</del> * jsx,以及jsx的typescript版tsx * 学习axios https://alligator.io/react/axios-react/ * 学习dva: https://github.com/sorrycc/blog/issues/62 * 学习umijs https://umijs.org/zh/guide/with-dva.html#

typescript学习笔记

·730 words·2 mins
先放参考资料: TypeScript 入门教程 React & Webpack react-typescript-cheatsheet (强推一波,讲了很多 react+ts 的实践) typescript 是 javascript 的语法扩展,好处是提供了类型,可以在编译(结果为 js 文件)的时候提供静态的类型检查。 typescript的问号语法:标记某个参数为可选。

Redux 学习笔记

·96 words·1 min
Redux是Flux架构的一种实现。 至于Flux架构是什么,可以参考Flux 架构入门教程 粗略地讲,和MVC架构是同一类东西,最大的区别是单向数据流,禁止了Model和VIEW层之间数据的流动。

JavaScript 学习笔记

·1060 words·3 mins
暂时没空从头开始搞…用到哪里先记录一下好了orz 我觉得不行,还是要先大致了解一下。 参考资料: A re-introduction to JavaScript (JS tutorial) 继承与原型链 详细代码 1// 让我们假设我们有一个对象 o, 其有自己的属性 a 和 b: 2// {a: 1, b: 2} 3// o 的 [[Prototype]] 有属性 b 和 c: 4// {b: 3, c: 4} 5// 最后, o.[[Prototype]].[[Prototype]] 是 null. 6// 这就是原型链的末尾,即 null, 7// 根据定义,null 没有[[Prototype]]. 8// 综上,整个原型链如下: 9// {a:1, b:2} ---> {b:3, c:4} ---> null 10 11console.log(o.a); // 1 12// a是o的自身属性吗?是的,该属性的值为1 13 14console.log(o.b); // 2 15// b是o的自身属性吗?是的,该属性的值为2 16// 原型上也有一个'b'属性,但是它不会被访问到.这种情况称为"属性遮蔽 (property shadowing)" 17 18console.log(o.c); // 4 19// c是o的自身属性吗?不是,那看看原型上有没有 20// c是o.[[Prototype]]的属性吗?是的,该属性的值为4 21 22console.log(o.d); // undefined 23// d是o的自身属性吗?不是,那看看原型上有没有 24// d是o.[[Prototype]]的属性吗?不是,那看看它的原型上有没有 25// o.[[Prototype]].[[Prototype]] 为 null,停止搜索 26// 没有d属性,返回undefined 27 28 29 30 31 hasOwnProperty方法,用来检查对象是否有自己定义的属性,而不是从原型链上继承的属性。 32该方法不需要遍历原型链。 33function Graph() { 34 this.vertices = []; 35 this.edges = []; 36} 37 38Graph.prototype = { 39 addVertex: function(v){ 40 this.vertices.push(v); 41 } 42}; 43 44console.log(g.hasOwnProperty('vertices')); 45// true 46 47console.log(g.hasOwnProperty('nope')); 48// false 49 50console.log(g.hasOwnProperty('addVertex')); 51// false 52 53console.log(g.__proto__.hasOwnProperty('addVertex')); 54// true 55 56var g = new Graph(); 57// g是生成的对象,他的自身属性有'vertices'和'edges'. 58// 在g被实例化时,g.[[Prototype]]指向了Graph.prototype. 59 60 61 62 63 64const array1 = [1, 2, 3, 4]; 65const reducer = (accumulator, currentValue) => accumulator + currentValue; 66 67// 1 + 2 + 3 + 4 68console.log(array1.reduce(reducer)); 69// expected output: 10 70 71// 5 + 1 + 2 + 3 + 4 72console.log(array1.reduce(reducer, 5)); 73// expected output: 15 74 75 76 77 78const object1 = { 79 a: 1, 80 b: 2, 81 c: 3, 82 a: 4 83}; 84 85const object2 = Object.assign({a: 14, d: 5,h:12}, object1); 86 87console.log(object2.a, object2.d,object2.h); 88// expected output: 1 5 12 89// Properties in the target object will be overwritten by properties in the sources if they have the same key. 90//Later sources' properties will similarly overwrite earlier ones. 91 92 93 94 95 96function multiply(multiplier, ...theArgs) { 97 return theArgs.map(function (element) { 98 return multiplier * element; 99 }); 100} 101 102var arr = multiply(2, 1, 2, 3); 103console.log(arr); // [2, 4, 6] 104 105 106 107 108function parentFunc() { 109 var a = 1; 110 111 function nestedFunc() { 112 var b = 4; // parentFunc can't use this 113 return a + b; 114 } 115 return nestedFunc(); // 5 116} 117 118 119 120 121(参数1, 参数2, …, 参数N) => { 函数声明 } 122(参数1, 参数2, …, 参数N) => 表达式(单一) 123//相当于:(参数1, 参数2, …, 参数N) =>{ return 表达式; } 124 125// 当只有一个参数时,圆括号是可选的: 126(单一参数) => {函数声明} 127单一参数 => {函数声明} 128 129// 没有参数的函数应该写成一对圆括号。 130() => {函数声明} 131 132var materials = [ 133 'Hydrogen', 134 'Helium', 135 'Lithium', 136 'Beryllium' 137]; 138 139materials.map(function(material) { 140 return material.length; 141}); // [8, 6, 7, 9] 142 143materials.map((material) => { 144 return material.length; 145}); // [8, 6, 7, 9] 146 147materials.map(material => material.length); // [8, 6, 7, 9] 148 149 150 151 152class ChildClass extends ParentClass { ... }

react学习笔记

·681 words·2 mins
首先介绍一个fb家的快速开发react的工具 create-react-app 这个东西依赖node6.0或者更高版本。 关于在ubuntu 14.04上安装node ,可以参考这个链接 发现执行nvm install 6.0会没有任何响应…但是实际上已经安装好了。

[C++11] promise && future leanrning notes

·322 words·1 min
用人话就是,主线程传给附属线程一个promise Object,然后主线程想要获取附属线程set给promise Object的值(也就是该线程返回的某个结果),需要通过主线程中的promise object 得到对应的future object(每个promise 对应一个 future),然后调用future 的get方法。如果附属线程没有执行作为参数传入的promise的set方法去返回结果,那么程序就会block住。

把二进制文件按字节读到vector中

·197 words·1 min
1 std::vector<unsigned char> readFromFile1(const char* filePath) { 2 FILE* file = fopen(filePath, "rb"); 3 std::vector<unsigned char> result; 4 if (file == nullptr) { 5 return result; 6 } 7 8 // 获取文件大小,尽量一次读完 9 size_t fileSize = getFileSize(file); 10 if (fileSize != 0) { 11 result.resize(fileSize); 12 size_t n = fread(&result[0], 1, fileSize, file); 13 assert(n <= fileSize); 14 if (n != fileSize) { 15 result.resize(n); 16 } 17 } 18 19 // 在读取过程当中,有可能文件大小有变化,再尝试读取 20 const size_t read_len = 1024; 21 char buf[read_len]; 22 for (;;) { 23 size_t n = fread(buf, 1, read_len, file); 24 result.insert(result.end(), buf, buf + n); 25 if (n < read_len) { 26 break; 27 } 28 } 29 fclose(file); 30 return result; 31 } 另外一种C++风格,但是性能较差的方法:

boost:property_tree 学习笔记

·254 words·1 min
先放资料: How to use boost::property_tree to load and write JSON How to iterate a boost property tree? 不出现key的方法遍历一个json文件: 详细代码 1 /* *********************************************** 2 Author :111qqz 3 mail: renkuanze@sensetime.com 4 Created Time :2018年08月17日 星期五 15时29分23秒 5 File Name :ptree.cpp 6 ************************************************ */ 7 #include <boost/property_tree/ptree.hpp> 8 #include <boost/property_tree/json_parser.hpp> 9 #include <bits/stdc++.h> 10 #include <vector> 11 using namespace std; 12 using boost::property_tree::ptree; 13 14 string indent(int level) { 15 string s; 16 for (int i=0; i<level; i++) s += " "; 17 return s; 18 } 19 20 void printTree (ptree &pt, int level) { 21 if (pt.empty()) { 22 cerr << "\""<< pt.data()<< "\""; 23 } 24 25 else { 26 if (level) cerr << endl; 27 28 cerr << indent(level) << "{" << endl; 29 30 for (ptree::iterator pos = pt.begin(); pos != pt.end();) { 31 cerr << indent(level+1) << "\"" << pos->first << "\": "; 32 33 printTree(pos->second, level + 1); 34 ++pos; 35 if (pos != pt.end()) { 36 cerr << ","; 37 } 38 cerr << endl; 39 } 40 41 cerr << indent(level) << " }"; 42 } 43 44 return; 45 } 46 int main() 47 { 48 ptree root; 49 read_json("terr.json",root); 50 printTree(root,0); 51 52 53 return 0; 54 55 }

使用python计算误差代码

·126 words·1 min
import os 1import math 2ave_err=0.0 3max_err=0.0 4max_err_rate=0.0 5length=0 6with open("cpu_result.txt","r") as fp1, open("cuda_ppl_result.txt","r") as fp2: 7 for l1 in fp1: 8 l2 = fp2.readline() 9 l1=l1[:-2] 10 l2=l2[:-2] 11 lst = l1.split(' ') 12 lst2 = l2.split(' ') 13 #print lst 14 lst = [float(x) for x in lst ] 15 length = length + len(lst) 16 lst2 = [float(x) for x in lst2] 17 #print (lst) 18 #print (lst2) 19 20 for index,x in enumerate(lst): 21 y = lst2[index] 22 ave_err = ave_err + abs(x-y) 23 max_err = max(max_err,abs(x-y)) 24 max_err_rate = max(max_err_rate,abs(x-y)/x) 25 26 print("len=",length) 27 print("max_err=",max_err) 28 print("max_err_rate=",max_err_rate*100,"%") 29 print("ave_err=",ave_err/length) 需要提供两个文件,并且两个文件的数据格式相同。

c++11 function 与bind 学习笔记

·315 words·1 min
C++11 std::function 是一种通用、多态的函数封装,它的实例可以对任何可 以调用的目标实体进行存储、复制和调用操作 见下面的例子 1 /* *********************************************** 2 Author :111qqz 3 mail: renkuanze@sensetime.com 4 Created Time :2018年07月19日 星期四 17时41分00秒 5 File Name :bind.cpp 6 ************************************************ */ 7 #include <functional> 8 #include <iostream> 9 using namespace std; 10 float foo( int x,int y,int z){return x+y+z+1.;} 11 int main() 12 { 13 function<int(int,int)>func = foo; 14 int y = 10; 15 function<int(int)>fun = [&]( int value)->int 16 { 17 return 1+value+y; 18 }; 19 cout<<func(15,4,9)<<endl; 20 cout<<fun(8)<<endl; 21 return 0; 22 } std::bind 则是用来绑定函数调用的参数的,它解决的需求是我们有时候可 能并不一定能够一次性获得调用某个函数的全部参数,通过这个函数,我们可以将 部分调用参数提前绑定到函数身上成为一个新的对象,然后在参数齐全后,完成调 用