Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pattern matching in pure functions

I need to define a pure function that takes two arguments and returns their quotient. If the divisor is 0 then I want to return 0.

If I had a named function then I would do

div[_, 0]   := 0
div[x_, y_] := x / y

how to do the same sort of pattern matching on arguments in a pure function #1 / #2 &?

like image 574
akonsu Avatar asked Sep 02 '25 06:09

akonsu


1 Answers

Try something like

If[#2 == 0, 0, #1/#2] &

for your pure function.

like image 70
High Performance Mark Avatar answered Sep 05 '25 00:09

High Performance Mark