Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Python have andmap / ormap?

Tags:

python

I am still a beginner at coding, especially with python. In one of my freshman classes, we were introduced to racket, a functional programming language very similar to scheme. In this class we learned about the functions andmap and ormap.

Is there a built in andmap and ormap in Python?

like image 502
Dzhao Avatar asked Mar 13 '23 01:03

Dzhao


1 Answers

Python has the map function that applies a function to a sequence plus all and any that perform the and and or functions. These short-circuit as soon as their condition is violated.

any(map(somefunction, sequence))
all(map(somefunction, sequence))

Python also lets you iterate and apply functions to a sequence directly in the language

any(somefunction(x) for x in sequence)
all(somefunction(x) for x in sequence)
like image 125
tdelaney Avatar answered Mar 23 '23 15:03

tdelaney