新增了三个字符串函数,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
- 获取链接
- X
- 电子邮件
- 其他应用
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 xsis the list obtained by applyingfto each element ofxs.
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, prepends f of first element to map f of rest of list
Here we can see the common pattern: Each of them has a binary operator, a starting value (typically the right-identity of the operator), and a list:
- in
sumfunction, they are+, 0 and the list; - in
andfunction, they are&&,Trueand the list; - in
mapfunction, things is a litter complicate, but we can still find them:(\x xs -> (f x):xs),[]and the the list.
In Haskell, we have a high order function which applied to a binary operator, a starting value (typically the right-identity of the operator), and a list, reduces the list using the binary operator, from right to left:
foldr f z [x1, x2, ..., xn] == x1 `f` (x2 `f` ... (xn `f` z)...)
So we can rewrite sum, and and map use foldr like this:
mySum' :: (Num t) => [t] -> t
mySum' xs = foldr (+) 0 xs
myAnd' :: [Bool] -> Bool
myAnd' xs = foldr (&&) True xs
myMap' :: (t -> t) -> [t] -> [t]
myMap' xs = foldr (\x xs -> (f x):xs) [] xs
It’s way much easier for us to understand.
That’s all for today, happy coding!
此博客中的热门博文
PHP8 —— Match Expressions
假设我们要保存发生的事件,根据时间的 class 来获取入库时的名称,PHP8 之前可以用 switch 实现 class Conversation {} $obj = new Conversation(); switch (get_class($obj)) { case 'Conversation': $type = 'started_conversation'; break; case 'Replay': $type = 'replied_to_conversation'; break; case 'Comment': $type = 'commented_on_lesson'; break; } echo $type; 显然这样写起来很繁琐。PHP8之后,可以使用 match expression 来改写,用键值对直接表达对应关系,省略了 case 、break等关键字。 $type = match(get_class($obj)) { 'Conversation' => 'started_conversation', 'Replay' => 'replied_to_conversation', 'Comment' => 'commented_on_lesson' } 当然,除了直接返回值之外,也能返回表达式;对相同返回的分支也可以做合并。比如使用match来实现一个请求路由。 $status = match($request_method) { 'post' => $this->handlePost(), 'get', 'head' => $this->handleGet(), default => throw new \Exception...
Java学习笔记 —— Java 14 新特性 Switch 表达式
Switch 表达式 Java 12和13中已经提供了 switch 表达式,不过是作为预览功能提供,默认情况下未启用。 在Java 14中正式发布了。简单来说,这是一种新的简化形式的switch块,带有 case L-> ... 标签。大部分情况下,这么写有助于简化代码。 下面举几个🌰。 假设我们有一个描述工作日的枚举。 我们可以使用新的switch表达式编写以下代码: switch (day) { case MONDAY -> System.out.println("Aweful"); case TUESDAY, WEDNESDAY -> System.out.println("Okay"); case THURSDAY -> System.out.println("Good"); case FRIDAY -> System.out.println("Great"); case SATURDAY, SUNDAY -> System.out.println("Awesome"); } 在这里,我们只为每种情况使用一个表达式。 需要注意的是,我们没有使用 break 语句。 我们也可以用新的switch表达式来返回值: int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; }; 还可以编写多行代码块并使用新关键字yield来返回值: int result = switch (s) { case "Foo" -> 1; case "Bar" -> 2; default -...
评论