33
loading...
This website collects cookies to deliver better user experience
doOperation()
which take a function as a first parameter and two numbers and will return a function that takes two numbers as an arguments, sum()
that returns summation of two numbers.def doOperation(operation, num1, num2):
return operation(num1, num2)
def sum(number1, number2):
return number1 + number2
doOperation(sum, 2, 3)
-----------
Output: 5
-----------
public int DoSomthing(Func<int,int,int> sum, int num1,int num2) =>
sum(num1, num2);
public int sum(int number1, int number2) =>
number1 + number2;
DoSomthing(sum, 2, 3)
----------------
Output: 5
----------------
Func<int,int,int>
which take 2 integer numbers and return integer to assign a sum
function to it.filter()
function to filter an array based on function we can pass it for this filter()
function.def filter(arr, action):
result= []
for num in arr:
if(action(num)):
result.append(num)
return result
def isOddNumber(num):
return num % 2 != 0
filter([1, 2, 3, 4, 5], isOddNumber)
----------------
Output: [1,3,5]
----------------
public static int[] Filter(int[] arr, Func<int, bool> func) =>
arr.Where(func).OrderBy(n => n).ToArray();
public static bool isOddNumber(int number) =>
number % 2 != 0;
Filter([1,2,3,4,5], isOddNumber)
------------------
Output: [1,3,5]
------------------