JavaScript checking for null vs. undefined

JS中undefined表示变量没有定义或从来没有赋值,而null是空对象,是有值的。
例如:

var a;
alert(typeof a);
//"undefined"

a = null;
alert(typeof null);
//"object"

JS中==比较为弱类型比较,JS会自动进行类型转换,然后返回值的比较结果。
而===为强类型比较,即必须类型与值同时相同,才会相等。
例如:

alert(0 == "0");
//true

alert(0==="0");
//false

alert(undefined == null);
//true

alert(undefined === null);
//false

判断变量为null:

if (a === null)
// or
if (a == null)

第一种方法,当a为undefined时返回false,
第二种方法,当a为undefined时返回true。

判断变量为undefined:

if (typeof a === "undefined")
// or
if (a === undefined)
// or
if (a == undefined)

第一、二种方法,当a为null时返回false,
第三种方法,当a为null时返回true。

还有一种使用falsey进行检查的方法:

if (!a) {
    // `a` is falsey, which includes `undefined` and `null`
    // (and `""`, and `0`, and `false`)
}

大部分翻译自stackoverflow:
JavaScript checking for null vs. undefined and difference between == and ===

GitHub03SSH授权

用低版本的github做上传的时候,会有提示

Permission denied (publickey). 

最简单的方法,就是安装新版本的GitHub。

如果你实在不愿意升级,那可以用以下步骤来进行:

#1、测试ssh,会提示Permission denied (publickey). 
ssh -T git@github.com 
#2、生成新的授权文件,如果你没有改过配置,那文件名为github_rsa,密码保持为空即可
ssh-keygen -t rsa -C "neohope@yahoo.com"
#3、查看新的key
ssh-add -l
#4、登录GitHub网站,到管理SSH Keys的地方,上传public key
#~/.ssh/github_rsa.pub
#5、测试ssh,会提示成功
ssh -T git@github.com 

搞定!

Prolog101(08)

列表:
放在用方括号中的一组项目的集合,各项目之间使用逗号分割。

空表:
没有项目的列表,用[]表示。

表头与表尾:
[X|Y]可以与任意的列表匹配,匹配成功后,X绑定为列表的第一个项目的值,我们称之为表头(head)。
而Y则绑定为剩下的列表,我们称之为表尾(tail)。
表尾(tail)一定是列表,而表头(head)则是一个项目,该项目可以是表,也可以是其他的任何数据结构。
看下这个例子就清楚了:

1 ?- [a|[b,c,d]] = [a,b,c,d]. 
true.

2 ?- [a|b,c,d] = [a,b,c,d].
ERROR: Syntax error: Unexpected comma or bar in rest of list
...

3 ?- [H|T] = [a]. 
H = a,
T = [].
 
4 ?- [H|T] = [a,b,c,d]. 
H = a,
T = [b, c, d].

5 ?- [H|T] = [a,[b,c,d]]. 
H = a,
T = [[b, c, d]].

6 ?- [H|T] = [].
false.

7 ?- [A,B|T] = [a,b,c,d].
A = a,
B = b,
T = [c, d].

8 ?- [a|[b|[c|[d|[]]]]] = [a,b,c,d].
true.

检查数据是否存在:

1 ?- member(a, [a,b,c]). 
true .

2 ?- member(d, [a,b,c]). 
false.

3 ?- member(d, [a,b,c,[d],e]). 
false.

4 ?- member([d], [a,b,c,[d],e]).
true .

5 ?- member(X, [a,b,c]). 
X = a ;
X = b ;
X = c.

追加

1 ?- append([a,b,c],[d,e,f],X).
X = [a, b, c, d, e, f].

2 ?- append([],[d,e,f],X).
X = [d, e, f].

3 ?- append([a,b],Y,[a,b,c,d]).
Y = [c, d].

4?- append(X,Y,[a,b,c]).
X = [],
Y = [a, b, c] ;
X = [a],
Y = [b, c] ;
X = [a, b],
Y = [c] ;
X = [a, b, c],
Y = [] ;

删除

1 ?- delete([a,b,c,d,e], c, X).
X = [a, b, d, e]

not in 优化为 not exists

最近迁移数据库的时候,发现not in比not exists效率差太多了

--not in 直接卡死
insert into table1(
select * from table2 t2@oraclegate where t2.pk not in (select pk from table3 t3));

--not exist则很快处理完成
insert into table1(
select * from table2 t2@oraclegate where not exists (select pk from table3 t3 where t3.pk=t2.pk));

其实数据量并不大,不知道是不是用gateway的问题。

Prolog101(07)

联合(Unification)

变量与任何项目: 变量可以与任何项目绑定,其中也包括变量
原始项目与原始项目: 两个原始项目(原子或整数)只有当它们相同时才能联合。
结构与结构: 如果两个结构的每个相应的参数能联合,那么这两个结构可以联合。

1 ?- (1,2,3)=(X,Y,Z).
X = 1,
Y = 2,
Z = 3.

2 ?- (1,2,3)=(X,X,Z).
false.

3 ?- (1,1,3)=(X,X,Z).
X = 1,
Z = 3.

4 ?- (1,X,3)=(X,1,Z).
X = 1,
Z = 3.

5 ?- (1,X,3)=(X,2,Z).
false.
1 ?- (1,2,X)=(1,2,(3,4,5)).
X = (3, 4, 5).

2 ?- (1,2,X)=(1,2,(3,4,Y)),Y=5.
X = (3, 4, 5),
Y = 5.

%_表示不关心匹配内容
3 ?- (1,2,X,Y,7)=(1,2,(3,4,5),6,_).
X = (3, 4, 5),
Y = 6.

4 ?- X = Y, Y = hi, write(X).
hi
X = Y, Y = hi.
%swipl -s objs.pl
%Hansen

%room
room(kitchen).

%objects and location
%object(Name, Color, Size, Weight).
location(object(candle, red, small, 1), kitchen).
location(object(apple, red, small, 1), kitchen).
location(object(apple, green, small, 1), kitchen).
location(object(table, blue, big, 50), kitchen).

%current room
here(kitchen).

%can take something?
can_take(Thing) :-  
here(Room), 
location(object(Thing, _, small, _), Room). 
can_take(Thing) :- 
here(Room), 
location(object(Thing, _, big, _), Room), 
write('The '), write(Thing),  
write(' is too big to carry.'), nl, 
fail. 
can_take(Thing) :- 
here(Room), 
not(location(object(Thing, _, _, _), Room)), 
write('There is no '), write(Thing), write(' here.'), nl, 
fail.

%out put the weight
write_weight(1) :- write('1 pound').  
write_weight(W) :- W > 1, write(W), write(' pounds').

%list all things in a room
list_things(Place) :-  
location(object(Thing, Color, Size, Weight), Place), 
write('A '),write(Size),tab(1), 
write(Color),tab(1), 
write(Thing), write(', weighing '), 
write_weight(Weight), nl, 
fail. 

Prolog101(06)

动态修改全局数据

asserta(X) :把子句X当作此子句的谓词的第一个子句加入到动态数据库中,不可回溯。
assert(X)或assertz(X) :把子句X当作此子句的谓词的最后一个子句加入到动态数据库中,不可回溯。
retract(X) :把子句X从动态数据库中删除,不可回溯。

%swipl -s room.pl
%Hansen

:-dynamic here/1. 
:-dynamic location/2. 
:-dynamic bag/1.

%房间定义
room(kitchen).
room(office).
room(hall). 
room(diningroom).
room(cellar). 

%门定义
door(office, hall).
door(kitchen, office).
door(hall, diningroom).
door(kitchen, cellar).
door(diningroom, kitchen).

%规则:有门的两个房间是相通的
connect(X,Y) :- door(X,Y).
connect(X,Y) :- door(Y,X).

%物品在哪个房间
location(desk, office).
location(apple, kitchen). 
location(flashlight, desk). 
location(washingmachine, cellar).
location(nani, washingmachine).
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).

%背包
bag(tourch).

%哪些物品可以吃
edible(apple).
edible(crackers).
edible(broccoli).

%当前位置
here(hall).

%移动
move(Place):- can_go(Place),retract(here(X)), asserta(here(Place)).
can_go(Place):- here(X), connect(X, Place).  
can_go(Place):- write('You can''t go to '),write(Place),write(' from here.'), nl, fail.

%获取物品
take(X):- can_take(X), retract(location(X,_)), asserta(bag(X)), write(X), write(' taken.'), nl.
can_take(Thing):- here(Place), location(Thing, Place). 
can_take(Thing):- write('There is no '), write(Thing), write(' here.'), nl, fail. 

%放下物品
put(X):- can_put(X), here(Place), retract(bag(X)), asserta(location(X,Place)), write(X), write(' put.'), nl.
can_put(Thing):- bag(Thing). 
can_put(Thing):- write('There is no '), write(Thing), write(' in your bag.'), nl, fail. 

%房间物品列表
list_things(Place):- location(X, Place),tab(2),write(X),nl,fail.
list_things(_).

%相连的
list_connections(Place):- connect(Place, X),tab(2),write(X),nl,fail.
list_connections(_).

%持有物品列表
list_bag(Thing):- bag(X),tab(2),write(X),nl,fail.
list_bag(_).

%吃东西
eat(Thing):- can_eat(Thing), retract(bag(X)), write(Thing), write(' eaten. Yummy!').
can_eat(Thing):- bag(Thing), edible(Thing).
can_eat(Thing):- not(bag(Thing)), write('There is no '), write(Thing), write(' in your bag.'), nl, fail. 
can_eat(Thing):- bag(Thing), write('You can''t eat the '), write(Thing), write('.'), nl, fail. 

%查看房间情况
look :-
here(Place), write('You are in the '), write(Place), nl,
write('You can see:'),nl,list_things(Place),  
write('You can go to:'), nl, list_connections(Place),
write('You have:'),nl,list_bag(Thing).

%帮助
game :-
write('Look around: look/0'),nl,
write('Move around: move/1'),nl,
write('Take something: take/1'),nl,
write('Eat something: eat/1').
1 ?- game.
Look around: look/0
Move around: move/1
Take something: take/1
Eat something: eat/1
true.

2 ?- look.
You are in the hall
You can see:
You can go to:
  diningroom
  office
You have:
  tourch
true.

3 ?- move(diningroom).
true .

4 ?- look.
You are in the diningroom
You can see:
You can go to:
  kitchen
  hall
You have:
  tourch
true.

5 ?- move(kitchen).
true .

6 ?- look.
You are in the kitchen
You can see:
  apple
  broccoli
  crackers
You can go to:
  office
  cellar
  diningroom
You have:
  tourch
true.
7 ?- take(apple).
apple taken.
true .

8 ?- look.
You are in the kitchen
You can see:
  broccoli
  crackers
You can go to:
  office
  cellar
  diningroom
You have:
  apple
  tourch
true.

9 ?- eat(apple).
apple eaten. Yummy!
true .

Prolog101(05)

递归定义都包括两个部分:边界条件与递归部分。
边界条件定义最简单的情况。而递归部分,则首先解决一部分问题,然后再调用其自身来解决剩下的部分,
每一次都将进行边界检测,如果剩下的部分已经是边界条件中所定义的情况时,那么递归就圆满成功了。

递归解决阶乘问题

%swipl -s factorial.pl
%Hansen

fun(N):- N>0,fact(N,1).
fun(N):- write(N), write(' is smaller than 1. '),fail.

fact(1,M):- write(M),!.
fact(N,M):- N1 is N-1, M1 is M*N, fact(N1,M1).
 1 ?- fun(0).
0 is smaller than 1. 
false.

 2 ?- fun(1).
1
true .

 3 ?- fun(2).
2
true .

 4 ?- fun(3).
6
true .

 5 ?- fun(4).
24
true .

 6 ?- fun(5).
120
true .

 7 ?- fun(6).
720
true .

递归解决汉诺塔问题

%swipl -s hanoi.pl
%Hansen

%N个盘子从A到C的问题,用递归解决的思路:
%N-1个盘子,从A到B
%1个盘子,从A到C
%N-1个盘子,从B到C

hanoi(N):-move(N,a,b,c).
move(1,A,_,C):-fromto(A,C),!.
move(N,A,B,C):-N1 is N-1,move(N1,A,C,B),fromto(A,C),move(N1,B,A,C).
fromto(Loc1,Loc2):-nl,write('move one disk from '),write(Loc1),write(' to '),write(Loc2).

执行查询:

 1 ?- hanoi(1).

move one disk from a to c
true.

 2 ?- hanoi(2).

move one disk from a to b
move one disk from a to c
move one disk from b to c
true.

 3 ?- hanoi(3).

move one disk from a to c
move one disk from a to b
move one disk from c to b
move one disk from a to c
move one disk from b to a
move one disk from b to c
move one disk from a to c
true.

%这里就会出错啦
 4 ?- hanoi(0).
ERROR: Out of local stack

WSDL生成代码

1、axis2 java

rem axis2 生成client代码
wsdl2java -uri file:///C:/Users/Hansen/Desktop/test.wsdl
rem axis2 生成server代码
wsdl2java -ss -uri file:///C:/Users/Hansen/Desktop/test.wsdl

2、cxf java

rem jaxws2.2 生成代码
wsdl2java  -p com.neohope -d src -all PATH_TO_WSDL
rem jaxws2.1 生成代码
wsdl2java  -frontend jaxws21 -p com.neohope -d src -all PATH_TO_WSDL

3、wsdl soap csharp

rem cs生成client代码
wsdl C:/Users/Hansen/Desktop/test.wsdl
rem cs生成server代码
wsdl /serverInterface C:/Users/Hansen/Desktop/test.wsdl

4、svcutil wcf csharp

svcutil.exe http://localhost:1168/Service1.xamlx?wsdl /out:MyService.cs /config:app.config

使用时,WCF服务端直接实现服务接口即可。
WCF客户端,将app.config文件拷贝到项目中,然后直接调用代理类中的方法即可。

Prolog101(04)

数学计算:
X is <数学表达式> 

数学运算:
+-*/
()

比较运算:
X > Y 
X < Y 
X >= Y 
X =< Y  
1 ?- X is 1*2+3/4-(5-6)*7.
X = 9.75.

2 ?- 3=<4.
true.

3 ?- 3>=4.
false.

4 ?- 3>4.
false.

5 ?- 3<4.
true.

6 ?- X is 2+2, 3>=X.  
false.

看一下摄氏度与华氏度互转的程序:

%swipl -s calc.pl
%Hansen

c_to_f(C,F) :- F is C*9/5+32.
f_to_c(F,C) :- C is (F-32)*5/9.
1 ?- c_to_f(11,F).
F = 51.8.

2 ?- f_to_c(51.8,F).
F = 10.999999999999998.