JavaScript之reduce()的用法
首先,参考了这位大佬的码源码破总结,敲了一遍,解源解器收获很大。码源码破一线战天下指标源码免费记录下来。解源解器参考链接背景:之前是码源码破接触vue源码的时候,发现使用reduce的解源解器地方很多。当时也是码源码破在看别人分享,简单带过,解源解器发现很好用。码源码破简单研究后,解源解器maven 找不到源码很少用到。码源码破导致理解不深。解源解器今天碰到到使用场景,码源码破特意花时间研究了一番,解源解器可以说理解的dalo流控源码很到位了。
语法arr.reduce(function(prev,cur,index,arr){ ...},init);其中,\arr表示原数组;\prev表示上一次调用回调时的返回值,或者初始值init;\cur表示当前正在处理的数组元素;\index表示当前正在处理的数组元素的索引,若提供init值,则索引为0,httpclient4.5源码否则索引为1;\init表示初始值。这个初始值在很多技巧上要用到,要重点留意
在实际使用当中,其实常用的参数只有两个:prev和cur。结合例子来分析一番。tomcat调试需要源码
实例以下举的例子除了用reduce来实现,还有很多其他的方法。不过使用reduce有它独有的技巧性和方便。
1、求数组的和方式一:
letarr=[3,4,5,6,9,2,4,6];letsum=arr.reduce((pre,cur,index,arr)=>{ //console.log('index:',index);//console.log('arr:',arr);returnpre+cur;})console.log(sum);方式二:
letsum=arr.reduce((pre,cur,index,arr)=>{ //console.log('index:',index);//console.log('arr:',arr);returnpre+cur;},0)console.log(sum);注意:方式二,由于传入了初始值0,所以pre的初始值就是0,cur的值,就是数组的第一个值为3;如果不传初始值的话,就如方式一,这时,pre的值是3,cur的值是4;相加后为7,作为下一轮的pre的值,cur是5,依次类推下去。
2、求数组的最大值方式一:
letarr=[1,4,5,6,9,2,4,6];letmax=arr.reduce((pre,cur)=>{ returnMath.max(pre,cur)})console.log(max);方式二:
letmax2=arr.reduce((pre,cur)=>{ returnpre>cur?pre:cur;})console.log('max2:',max2);3、给数组去重letnewArr=arr.reduce((pre,cur)=>{ pre.indexOf(cur)===-1&&pre.push(cur)returnpre},[])console.log(newArr);进阶用法1.求字符串中字幕出现的次数conststr='sfhjasfjgfasjuwqrqadqeiqsajsdaiwqdaklldflas-cmxzmnha';constres=str.split('').reduce((pre,cur)=>{ pre[cur]?pre[cur]++:pre[cur]=1;returnpre},{ })console.log('汇总次数:',res);2.数组转数组letarr1=[2,3,4,5,6,7];letnewArr1=arr1.reduce((pre,cur)=>{ pre.push(cur*cur)returnpre;},[])console.log('数组转数组:',newArr1);3.数组转对象letstreams=[{ name:'博士',id:1},{ name:'硕士',id:2},{ name:'本科',id:3}];letobj1=streams.reduce((pre,cur)=>{ pre[cur.id]=cur;returnpre;},{ })console.log('数组转对象:',obj1);高级用法1.多维的叠加执行操作例子:各科成绩占比不一样,求结果
constresult=[{ subject:'math',score:},{ subject:'chinese',score:},{ subject:'english',score:},];constdis={ math:0.5,chinese:0.2,english:0.4};letres2=result.reduce((pre,cur)=>{ returndis[cur.subject]*cur.score+pre},0)console.log('多维叠加:',res2);加大难度:商品对应不同国家汇率不同,求价格
letarr=[3,4,5,6,9,2,4,6];letsum=arr.reduce((pre,cur,index,arr)=>{ //console.log('index:',index);//console.log('arr:',arr);returnpre+cur;})console.log(sum);、扁平一个二维数组letarr=[3,4,5,6,9,2,4,6];letsum=arr.reduce((pre,cur,index,arr)=>{ //console.log('index:',index);//console.log('arr:',arr);returnpre+cur;})console.log(sum);1多维数组扁平化
3、对象数组去重letarr=[3,4,5,6,9,2,4,6];letsum=arr.reduce((pre,cur,index,arr)=>{ //console.log('index:',index);//console.log('arr:',arr);returnpre+cur;})console.log(sum);、compose函数reduxcompose源码实现
letarr=[3,4,5,6,9,2,4,6];letsum=arr.reduce((pre,cur,index,arr)=>{ //console.log('index:',index);//console.log('arr:',arr);returnpre+cur;})console.log(sum);3å¦ä½å¨ç³»ç»å®è£ ARGB
nginx php-fpmå®è£ é ç½®nginxæ¬èº«ä¸è½å¤çPHPï¼å®åªæ¯ä¸ªwebæå¡å¨ï¼å½æ¥æ¶å°è¯·æ±åï¼å¦ææ¯php请æ±ï¼ååç»php解éå¨å¤çï¼å¹¶æç»æè¿åç»å®¢æ·ç«¯ã
nginxä¸è¬æ¯æ请æ±åfastcgi管çè¿ç¨å¤çï¼fascgi管çè¿ç¨éæ©cgiåè¿ç¨å¤çç»æ并è¿å被nginx
æ¬æ以php-fpm为ä¾ä»ç»å¦ä½ä½¿nginxæ¯æPHP
ä¸ãç¼è¯å®è£ php-fpm
ä»ä¹æ¯PHP-FPM
PHP-FPMæ¯ä¸ä¸ªPHP FastCGI管çå¨ï¼æ¯åªç¨äºPHPç,å¯ä»¥å¨ 2.php.net/distributions/php-5.4.7.tar.gztar zvxf php-5.4.7.tar.gz
cd php-5.4.7./configure --prefix=/usr/local/php --enable-fpm --with-mcrypt \--enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath \--enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \--with-gd --with-jpeg-dir
make all install
æ§çæå¨æè¡¥ä¸php-fpmå®è£ ï¼æ§çç¨åºå·²ç»æ²¡æäºï¼å¤§å®¶æ°ççå§ï¼è¿éå个å±ç¤ºï¼
wget 2.php.net/get/php-5.2..tar.gz
wget tl -enable-sockets -with-bz2 -with-curl -with-curlwrappers\
-enable-mbregex -with-gd -enable-gd-native-ttf -enable-zip -enable-soap -with-iconv -enable-bcmath\
-enable-shmop -enable-sysvsem -enable-inline-optimization -with-ldap -with-ldap-sasl -enable-pdo\
-with-pdo-mysql
make all install
以ä¸ä¸¤ç§æ¹å¼é½å¯ä»¥å®è£ php-fpmï¼å®è£ åå 容æ¾å¨/usr/local/phpç®å½ä¸
以ä¸å°±å®æäºphp-fpmçå®è£ ã
ä¸é¢æ¯å¯¹php-fpmè¿è¡ç¨æ·è¿è¡è®¾ç½®
cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf
ä¿®æ¹
user = www-data
group = www-data
å¦æwww-dataç¨æ·ä¸åå¨ï¼é£ä¹å æ·»å www-dataç¨æ·
groupadd www-data
useradd -g www-data www-data
äºãç¼è¯å®è£ nginx
ç¶ånginxï¼
nginxå¯ä»¥ä½¿ç¨åå¹³å°çé»è®¤å æ¥å®è£ ï¼æ¬ææ¯ä»ç»ä½¿ç¨æºç ç¼è¯å®è£ ï¼å æ¬å ·ä½çç¼è¯åæ°ä¿¡æ¯ã
æ£å¼å¼å§åï¼ç¼è¯ç¯å¢gcc g++ å¼ååºä¹ç±»çéè¦æåè£ å¥½ï¼è¿éé»è®¤ä½ å·²ç»è£ 好ã
ububtuå¹³å°ç¼è¯ç¯å¢å¯ä»¥ä½¿ç¨ä»¥ä¸æ令
1
2
apt-get install build-essential
apt-get install libtool
centoså¹³å°ç¼è¯ç¯å¢ä½¿ç¨å¦ä¸æ令
å®è£ makeï¼
1
yum -y install gcc automake autoconf libtool make
å®è£ g++:
1
yum install gcc gcc-c++
ä¸é¢æ£å¼å¼å§
---------------------------------------------------------------------------
ä¸è¬æ们é½éè¦å è£ pcre, zlibï¼åè 为äºéårewriteï¼åè 为äºgzipå缩ã
1.éå®æºç ç®å½
å¯ä»¥æ¯ä»»ä½ç®å½ï¼æ¬æéå®çæ¯/usr/local/src
1
cd /usr/local/src
2.å®è£ PCREåº
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ ä¸è½½ææ°ç PCRE æºç å ï¼ä½¿ç¨ä¸é¢å½ä»¤ä¸è½½ç¼è¯åå®è£ PCRE å ï¼
1
2
3
4
5
6
7
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8..tar.gz
tar -zxvf pcre-8..tar.gz
cd pcre-8.
./configure
make
make install
3.å®è£ zlibåº
e to nginx! å表示 Nginx å·²ç»å®è£ 并è¿è¡æåã
-----------------------------------------------------
å°è¿énginxå°±å®è£ å®æäºï¼å¦æåªæ¯å¤çéæhtmlå°±ä¸ç¨ç»§ç»å®è£ äº
å¦æä½ éè¦å¤çphpèæ¬çè¯ï¼è¿éè¦å®è£ php-fpmã
ä¸é¢å®è£ æé
éï¼å¯è½éå°çé误åä¸äºå¸®å©ä¿¡æ¯
1.1ç¼è¯pcreé误
1
2
3
4
5
libtool: compile: unrecognized option `-DHAVE_CONFIG_H'
libtool: compile: Try `libtool --help' for more information.
make[1]: *** [pcrecpp.lo] Error 1
make[1]: Leaving directory `/usr/local/src/pcre-8.'
make: *** [all] Error 2
解å³åæ³ï¼å®è£ g++,å«å¿äºéæ°configure
1
2
3
4
5
apt-get install g++
apt-get install build-essential
make clean
./configure
make
1.2 makeåºé
1
2
3
4
5
make: *** No rule to make target `build', needed by `default'. Stop.
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
æç §ç¬¬4æ¥çå®è£ æ¹æ³æ
ubuntuä¸
1
2
apt-get install openssl
apt-get install libssl-dev
centosä¸
1
yum -y install openssl openssl-devel
2.nginxç¼è¯é项
makeæ¯ç¨æ¥ç¼è¯çï¼å®ä»Makefileä¸è¯»åæ令ï¼ç¶åç¼è¯ã
make installæ¯ç¨æ¥å®è£ çï¼å®ä¹ä»Makefileä¸è¯»åæ令ï¼å®è£ å°æå®çä½ç½®ã
configureå½ä»¤æ¯ç¨æ¥æ£æµä½ çå®è£ å¹³å°çç®æ ç¹å¾çãå®å®ä¹äºç³»ç»çå个æ¹é¢ï¼å æ¬nginxç被å 许使ç¨çè¿æ¥å¤ççæ¹æ³ï¼æ¯å¦å®ä¼æ£æµä½ æ¯ä¸æ¯æCCæGCCï¼å¹¶ä¸æ¯éè¦CCæGCCï¼å®æ¯ä¸ªshellèæ¬ï¼æ§è¡ç»ææ¶ï¼å®ä¼å建ä¸ä¸ªMakefileæ件ãnginxçconfigureå½ä»¤æ¯æ以ä¸åæ°ï¼
--prefix=path å®ä¹ä¸ä¸ªç®å½ï¼åæ¾æå¡å¨ä¸çæ件 ï¼ä¹å°±æ¯nginxçå®è£ ç®å½ãé»è®¤ä½¿ç¨ /usr/local/nginxã
--sbin-path=path 设置nginxçå¯æ§è¡æ件çè·¯å¾ï¼é»è®¤ä¸º prefix/sbin/nginx.
--conf-path=path 设置å¨nginx.confé ç½®æ件çè·¯å¾ãnginxå 许使ç¨ä¸åçé ç½®æ件å¯å¨ï¼éè¿å½ä»¤è¡ä¸ç-cé项ãé»è®¤ä¸ºprefix/conf/nginx.conf.
--pid-path=path 设置nginx.pidæ件ï¼å°åå¨ç主è¿ç¨çè¿ç¨å·ãå®è£ å®æåï¼å¯ä»¥éæ¶æ¹åçæ件å ï¼ å¨nginx.confé ç½®æ件ä¸ä½¿ç¨ PIDæ令ãé»è®¤æ åµä¸ï¼æ件å 为prefix/logs/nginx.pid.
--error-log-path=path 设置主é误ï¼è¦åï¼åè¯ææ件çå称ãå®è£ å®æåï¼å¯ä»¥éæ¶æ¹åçæ件å ï¼å¨nginx.confé ç½®æä»¶ä¸ ä½¿ç¨ çerror_logæ令ãé»è®¤æ åµä¸ï¼æ件å 为prefix/logs/error.log.
--pilationâï¼1.1.ä¸ï¼ pcre_jitæ令ï¼ã
--with-zlib=path â设置çzlibåºçæºç è·¯å¾ãè¦ä¸è½½ä» zlibï¼çæ¬1.1.3 - 1.2.5ï¼ç并解åãå ¶ä½çå·¥ä½æ¯Nginxç./ configureåmakeå®æãngx_/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install
7. php-fpm 5.4.7 å¦ä½å ³é éå¯ï¼
php 5.4.7 ä¸çphp-fpm ä¸åæ¯æ php-fpm 以åå ·æç /usr/local/php/sbin/php-fpm (start|stop|reload)çå½ä»¤ï¼éè¦ä½¿ç¨ä¿¡å·æ§å¶ï¼
masterè¿ç¨å¯ä»¥ç解以ä¸ä¿¡å·
INT, TERM ç«å»ç»æ¢ QUIT å¹³æ»ç»æ¢ USR1 éæ°æå¼æ¥å¿æ件 USR2 å¹³æ»éè½½ææworkerè¿ç¨å¹¶éæ°è½½å ¥é ç½®åäºè¿å¶æ¨¡å
示ä¾ï¼
php-fpm å ³éï¼
kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
php-fpm éå¯ï¼
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
æ¥çphp-fpmè¿ç¨æ°ï¼
ps aux | grep -c php-fpm
8.å½ä»¤è¡ä¸æ§è¡phpï¼æ示æ¾ä¸å°å½ä»¤
-bash: /usr/bin/php: No such file or directory
vi /etc/profile
å¨æ件åºé¨å¢å ä¸è¡é ç½®
export PATH=/usr/local/php/bin:$PATH
ä¿åéåº
source /etc/profile
éå pathinfoé ç½®
location ~ ^(.+\.php)(.*)$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
2024-12-24 00:55
2024-12-24 00:25
2024-12-23 23:58
2024-12-23 23:39
2024-12-23 23:25