博文

目前显示的是 2017的博文

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, '?');

Fold function in haskell

let’s first take a look at some list functions, and see what’s the characteristic they all share sum : computes the sum of the numbers of a structure; and : returns the conjunction of a container of Bools; map : map f xs is the list obtained by applying f to each element of xs . There three are all common list function, and act very differently, but can be defined recursively using pattern matching as follow: mySum :: ( Num t) => [t] -> t mySum [] = 0 -- sum of empty is 0 mySum (x : xs) = x + mySum xs -- otherwise, add first element to sum of rest of the list myAnd :: [ Bool ] -> Bool myAnd [] = True -- and of empty is True myAnd (x : xs) = x && myAnd xs -- otherwise, return the conjunction of first element and the rest of list myMap :: (t -> t) -> [t] -> [t] myMap _ [] = [] -- map any function to empty is empty myMap f (x : xs) = f x : myMap f xs -- otherwise, prepend