Creates an anonymous function that returns the expected result.
The argument order is
$substring and then
How does it work?
First, splits the full string by the substring, and counts the number of elements.
When splitting the string:
- If the substring isn't present, returns 1 element.
- If it is present, returns 1 extra element.
&str_split("abcbdbebf", 'b') -> ["a", "c", "d", "e", "f"]
'b's, returns 5 elements.
If the number of elements is higher than
$number, that means there's at least
$number substrings in the full string.
&iff() function is just the same as a ternary operator, in C-like languages.
To remove from the end of the string, I need to reverse the string, since there isn't a way to indicate to remove from the end of the string.
However, it takes an argument that indicates how many times to replace, which means, it will replace up to
$fn = fn($full_string, $substring, $num) => &iff(
(call &len(&str_split($full_string, $substring))) > $num,
Set $fn to an anonymous function ($full_string, $substring, $num)
Set $count to the result of calling &len(Call &str_split($full_string, $substring)).
Return the result of calling &iff(
$count > $num,