PHP8 —— New String Helpers

新增了三个字符串函数,str_starts_with, str_ends_with, str_contains, PHP 的函数这么方便,很难想象竟然一直没有这几个。 str_starts_with 判断字符串是否以另一个字符串开头,在PHP7以及之前 $id = 'inv_abcdefgh'; $result = strpos($id, 'inv_') === 0; var_dump($result); // true PHP8 中可以直接这么写 $result = str_starts_with($id, 'inv_'); str_ends_with 判断字符串是否以另外一个字符串结尾,在 PHP7 及之前,比较麻烦,通常是这么写 $id = 'abcd_inv'; $result = strpos(strrev($id), strrev('_inv')) === 0; 或者 $result = substr($id, -1 * strlen('_inv')) === '_inv'; 或者上正则吧 $result = preg_match('/_inv$/', $id) === 1; 看起来都是比较麻烦的。PHP8 里面可以简化成下面这样了 $id = 'abcd_inv'; $result = str_ends_with($id, '_ind'); str_contains 字符串包含,PHP8 之前一般就是 strpos 来实现了 $url = 'https://example?for=bar'; $result = strpos($url, '?') !== FALSE; PHP8 就直接一点 $result = str_contains($url, '?');

Lambda structure

Function

$$\lambda x.x$$ is a simple function. It has a head, which is a lambda ($\lambda$) followed by a variable name, and a body, and the head and body of the function is separated by a dot ($.$). When we applied this function to an argument, the x in the body will have the value of that given argument.

Alpha equivalence

$$\lambda x.x$$ $$\lambda y.y$$ $$\lambda z.z$$ all there three are the same thing. The letters here are not meaningful, just a way of expression. This equivalence is called alpha equivalence.

Beta reduction

Beta reduction is the process in which we apply a function to arguments, substitute the input expression for all bound variables in the body, and eliminate the head. If we apply $\lambda x.x$ to a number, for example 42, we can substitute 42 for each $x$ in the body, and eliminate the head: $$(\lambda x.x) 42$$ $$42$$ Another example: apply function $(\lambda x.x*4)$ to 8, we got $$(\lambda x.x*4) 8$$ $$8*4$$ $$32$$ We can apply function to another function or variables or both: $$(\lambda x.x)(\lambda y.y)z$$ $$[x := (\lambda y.y)]$$ $$(\lambda y.y)z$$ $$ [y := z]$$ $$ z$$

评论

此博客中的热门博文

D3js Data-binding basics

JavaScript 数据类型

Vue3新特性(1) —— Vite