function getTotal(list, dir) {
// where if dir is equal to either '>' or.'<'
return list
.filter(item => (dir === '<' ? item < 0 : item > 0) && item)
.reduce((total, item) => (total += item), 0);
}
This method gets vowels (‘a’, ‘e’, ‘i’, ‘o’, ‘u’) found in a string.
#make a function:
def get_vowels(string):
#return is the keyword which means function have to return value:
return [each for each in string if each in 'aeiou']
#assign the words and function will return vowels words.
get_vowels('foobar') # ['o', 'o', 'a']
get_vowels('gym') # []
def unfold(fn, seed):
def fn_generator(val):
while True:
val = fn(val[1])
if val == False: break
yield val[0]
return [i for i in fn_generator([None, seed])]
EXAMPLES
f = lambda n: False if n > 50 else [-n, n + 10]
unfold(f, 10) # [-10, -20, -30, -40, -50]
#define a function
1.Def cube(num)
#write the formula of cube
2.return num*num*num
#give the number to calculate the cube
3.cube(3)
# print the cube of that number simply by using print command
4.print(cube(3))
5.“return” keyword means that function have to return value
# give a name of function after def
1.def sayhi():
#put the statement in the function
2.print(“hello world”)
#call function by name:
3.sayhi()
Output:
Hello world
#define a function
Def cube(num)
#write the formula of cube
return num*num*num
#give the number to calculate the cube
cube(3)
# print the cube of that number simply by using print command
print(cube(3))
“return” keyword means that function have to return value
Use functools.reduce() to perform right-to-left function composition. The last (rightmost) function can accept one or more arguments; the remaining functions must be unary.
from functools import reduce
1.def compose(*fns):
2.return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
EXAMPLES
add5 = lambda x: x + 5
multiply = lambda x, y: x * y
multiply_and_add_5 = compose(add5, multiply)
multiply_and_add_5(5, 2) # 15
add_action( 'wp_body_open', 'wpdoc_add_custom_body_open_code' );
function wpdoc_add_custom_body_open_code() {
echo '<!-- Google Tag Manager (noscript) --> ... <!-- End Google Tag Manager (noscript) -->';
}
Comments