新增了三个字符串函数,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!
此博客中的热门博文
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 funct...
Turtle Geometry Tutorial 1
We have a little turtle who holds a pen, on our display screen. The turtle can respond to some commands: FORWARD , BACK , RIGHT and LEFT . * FORWARD 100 : The turtle will move 100 step on the screen, in the direction it is facing. * RIGHT 100 : The turtle will turn its head 90° clockwise in place. Repeating the commands above 4 times will get us a square with side 100 units long. forward 100 right 90 forward 100 right 90 forward 100 right 90 forward 100 right 90 We can rewrite the commands use repeat loop, as repeat 4 [ forward 100 right 90 ] This will give us the same square as before. At the next step we’ll teach our little turtle some new command, here we add square to the its vocabulary. to square repeat 4 [forward 100 right 90 ] end then we can use this new square command to draw the same square. Here comes some thing interesting: repeat 4 [ square right 90 ] repeat 12 [ square right 30 ] OK, that is all for today. Happy ...
Java 学习笔记 —— 在Java 8 中创建 Optional
像Java 8 API中的许多其他新类一样,Optional的实例是不可变的。 Optional是基于值的类,其实例有如下特性: 是final且不可变的(可能包含对可变对象的引用) 没有公共构造函数,因此必须通过工厂方法实例化 具有equals,hashCode和toString的实现,这些实现仅基于其状态 创建 Optional 的静态工厂方法为 empty , of 和 ofNullable ,其签名为: static <T> Optional<T> empty() static <T> Optional<T> of(T value) static <T> Optional<T> ofNullable(T value) empty 会返回一个空的Optional。 of 方法返回一个包装指定值的Optional,如果参数为null则引发异常。 public static <T> Optional<T> createOptionalTheHardWay(T value) { return value == null ? Optional.empty() : Optional.of(value); } 上面是一种复杂写法,简单点可以使用ofNullable 方法,如下: public static <T> Optional<T> createOptionalTheEasyWay(T value) { return Optional.ofNullable(value); } 实际上,Java 8 中ofNullable实现逻辑就是: 检查所包含的值是否为null,是则返回空的Optional,不是则使用Optional.of对其进行包装。 顺便说一下,类OptionalInt,OptionalLong和OptionalDouble包装了不会为null的primitives,所以他们只有一个 of 方法。 static OptionalInt of(int value) static OptionalLong of(longvalue) static OptionalDouble...
评论