A story about using Python's reduce

Functional languages have functions such as foldr and foldl that do things like collapse an array. So, recently I wondered if there was a similar implementation in Python, but I realized that there was something called reduse.

For example, if you want to guarantee that everything is True in an array consisting of only Booleans, if you do not use reduse, you may end up with the following dirty writing style.

python


def check_all_true(check_array):
    result = True
    for elem in check_array:
        result = result and elem
    return result

if __name__ == '__main__':
    print check_all_true([True, True, True])
    print check_all_true([True, False, True])

If you rewrite this to reduce, it will look refreshing as shown below.

python


def check_all_true(check_array):
    return reduce(lambda prev, nxt: prev and nxt, check_array)

if __name__ == '__main__':
    print check_all_true([True, True, True])
    print check_all_true([True, False, True])

However, the substance of reduce seems to be foldl, so if you want to operate like foldr, you need to devise a little.

Recommended Posts

A story about using Python's reduce
A refreshing story about Python's Slice
A sloppy story about Python's Slice
A story about simple machine learning using TensorFlow
A story about using Resona's software token with 1Password
[Linux] A story about mounting a NAS through a firewall using NFS
A story about installing matplotlib using pip with an error
Learn about logging using Python's logging module ①
A story about remodeling Lubuntu into a Chromebook
A memorandum of using Python's input function
A story about machine learning with Kyasuket
A story about Python pop and append
A story about a 503 error on Heroku open
A story about trying to connect to MySQL using Heroku and giving up
A addictive story when using tensorflow on Android
A story about operating a GCP instance from Discord
A story about Go's global variables and scope
A story about displaying article-linked ads on Jubatus
A story about implementing a login screen with django
A story about running Python on PHP on Heroku
A story about modifying Python and adding functions
A story about data analysis by machine learning
Escape from Python's virtual environment ~ A story about being trapped in a virtual environment I created ~
A story about making 3D space recognition with Python
A story about predicting exchange rates with Deep Learning
A story about migrating entire Linux disk via SSH
A memorandum about matplotlib
A memorandum about Nan.
A story about making Hanon-like sheet music with Python
A story about trying a (Golang +) Python monorepo with Bazel
A story about a Python beginner trying to get Google search results using the API
SoC FPGA: A small story when using on Linux
A story about reflecting Discord activity in Slack Status
A note about __call__
A story about struggling to loop 3 million ID data
Try using Python's Tkinter
A note about subprocess
A story about changing the master name of BlueZ
A note about mprotect (2)
A story about a Linux beginner passing LPIC101 in a week
A swampy story when using firebase on AWS lamda
A story about a Linux beginner putting Linux on a Windows tablet
A story about stumbling through PATH after installing anaconda
(Note) A story about creating a question answering system using Spring Boot and machine learning (SVM)
Machine learning A story about people who are not familiar with GBDT using GBDT in Python
Instantly create a diagram of 2D data using python's matplotlib
A story that took time to understand python's argsort (memorial)
A story about creating a UNIX / Linux compatible OS from scratch
A story about how to specify a relative path in python.
[Python] Chapter 01-03 About Python (Write and execute a program using PyCharm)
A story about competing with a friend in Othello AI Preparation
A story that stumbled when using pip in a proxy environment
A little deeper story about blockchain, ticking the digital world
A story about an amateur making a breakout with python (kivy) ②
A story about how to deal with the CORS problem
A story about an amateur making a breakout with python (kivy) ①
A story about a war when two newcomers developed an app
A story about making a tanka by chance with Sudachi Py
A story about clustering time series data of foreign exchange
A story about a 40-year-old engineer manager passing "Deep Learning for ENGINEER"
A story about trying to implement a private variable in Python.