Try to make foldl and foldr with Python: lambda. Also time measurement

Exercises while starting to touch Python. Try. foldl,foldr

box=lambda *x:x
unbox=lambda x:x[-1]
do=box
switch=unbox

foldl=(lambda f,acc,xs:
	switch(
		xs == [] and do( acc )
		or 		do( foldl(f, f(acc, xs[0]), xs[1:]) )
	)
)

foldr=(lambda f,xs,acc:
	switch(
		xs == [] and 	do( acc )
		or 		do( foldr(f, xs[:-1], f(xs[-1], acc)) )
	)
)

did it. I can go straight and miss the beat. No, it was amazing H book cheating. Very very on my own ...

Self-measurement tool

Compare it with the original reduce or sum. It seems that timeit can be used, but I didn't know what to do on IDLE, so I'll try to make my own measurement tool. Well, it will be some practice.

import time

measure_t=(lambda f , *arg :
	(lambda m_start = time.time() ,
		 m_stop = time.time :
			( f( *arg),
			m_stop() - m_start ,
			)[-1]
	 )()
   )

did it. Petit fits at the timing of function execution.

It seems that it will be used even though it is made properly.

I measured it.

>>> measure_t(foldl,lambda x,y:x+y,0,range(1,100))
0.00026679039001464844
>>> measure_t(foldr,lambda x,y:x+y,range(1,100),0)
0.000247955322265625
>>> measure_t(reduce,lambda x,y:x+y,range(100))
7.605552673339844e-05
>>> measure_t(sum,range(100))
6.198883056640625e-06

It is good to use builtins as quietly as possible. Does that mean ...

that's all.

Recommended Posts

Try to make foldl and foldr with Python: lambda. Also time measurement
Fractal to make and play with Python
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
WEB scraping with python and try to make a word cloud from reviews
Try to make a command standby tool with python
Try to operate DB with Python and visualize with d3
Make ordinary tweets fleet-like with AWS Lambda and Python
Execution time measurement with Python With
Try to make it using GUI and PyQt in Python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Try to bring up a subwindow with PyQt5 and Python
I tried to make GUI tic-tac-toe with Python and Tkinter
Try to operate Facebook with Python
[Machine learning] Try running Spark MLlib with Python and make recommendations
How to make a surveillance camera (Security Camera) with Opencv and Python
I tried to make a periodical process with Selenium and Python
Try to display google map and geospatial information authority map with python
Throw something to Kinesis with python and make sure it's in
Try to make capture software with as high accuracy as possible with python (1)
[Python] Try to recognize characters from images with OpenCV and pyocr
Try to reproduce color film with Python
Try logging in to qiita with Python
Connect to s3 with AWS Lambda Python
Try assigning or switching with Python: lambda
[TCP / IP] After studying, try to make an HTTP client-like with Python
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Challenge to create time axis list report with Toggl API and Python
Try converting latitude / longitude and world coordinates to each other with python
How to get the date and time difference in seconds with python
Try to make a capture software with as high accuracy as possible with python (2)
Try to make your own AWS-SDK with bash
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
MessagePack-Try to link Java and Python with RPC
Try running Google Chrome with Python and Selenium
Try to solve the man-machine chart with Python
Try to draw a life curve with python
I want to make a game with Python
Try to communicate with EV3 and PC! (MQTT)
python time measurement
Try to automatically generate Python documents with Sphinx
Sample to send slack notification with python lambda
How to measure execution time with Python Part 1
Export RDS snapshot to S3 with Lambda (Python)
Upload files to Google Drive with Lambda (Python)
Try to make client FTP fastest with Pythonista
To represent date, time, time, and seconds in Python
Try to detect fish with python + OpenCV2.4 (unfinished)
How to measure execution time with Python Part 2
[# 1] Make Minecraft with Python. ~ Preliminary research and design ~
[Python] Convert time display (str type) using "" "and"'" to seconds (float type) with datetime and timedelta
Reading, summarizing, visualizing, and exporting time series data to an Excel file with Python
Try running a Schedule to start and stop an instance on AWS Lambda (Python)
filter, map, reduce with js and python (There are also arrow expressions, lambda expressions, comprehension expressions)
Convert timezoned date and time to Unixtime in Python2.7
Extract "current date only" and "current date and time" with python datetime.
Procedure to load MNIST with python and output to png
Try to solve the programming challenge book with python3
Try to make a Python module in C language
I want to handle optimization with python and cplex
Explain in detail how to make sounds with python