Following code gives me warning at compile time: Warning: use of operator '>' has no effect
rd(a,{x,y}),
List = [#a{x=1,y=2}, #a{x=3,y=4}],
lists:filter(
fun(E) ->
E#a.x > 1, E#a.y =:= 2
end, List).
But when I substitute comma with andalso, there is no warning.
Using comma in this case only separates two actions, without effect to each other: E#a.x > 1
and the next operation (which is result of function) E#a.y =:= 2
It means that in your case, filter function is equal to:
fun( E ) ->
E#a.y =:= 2
end
Only if you're writing guard expressions usage of comma is equal to usage of andalso
, otherwise - comma just a separator between actions.
So, you may rewrite your function in two ways:
1)
fun
(E) when E#a.x > 1, E#a.y =:= 2 ->
true;
(_Othervise) ->
false
end
2)
fun( E ) ->
(E#a.x > 1) andalso (E#a.y =:= 2)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With