Try assigning or switching with Python: lambda

This is the first post. I look forward to working with you.

I will touch Python for the first time in a while with Raspberry Pi, and make a note of the result I pursued to make it easier for myself.

I am not particularly oriented towards the so-called darkness. So please let me know if you can't do anything.

How about lambda? Can you use it?

I didn't know how to use lambda before. I think I like the fact that there are various restrictions now. I want to write everything possible with lambda.

Let's take an example of quicksort. First of all, in def?

def qs( xs ) :
        le = [x for x in xs[1:] if x<=xs[0] ]
        gt = [x for x in xs[1:] if x>xs[0] ]
        if xs == []:
                return []
        else:
                return qs( le ) + [ xs[0] ] + qs( gt )

Is it like this?

Can you substitute?

If you write it normally with lambda,

qs=(lambda xs :
	[] if xs == [] else
    qs( [x for x in xs[1:] if x<=xs[0] ] ) + [ xs[0] ] 
    + qs( [x for x in xs[1:] if x>xs[0] ] )
)

Not bad, but it feels long and messy. I wonder if this would be def. After all. It is not possible to assign to a variable in the middle, but it seems that it can be bound to a name and defined.

qs=(lambda xs :
    (lambda 
    	le = [x for x in xs[1:] if x<=xs[0] ] ,
    	gt = [x for x in xs[1:] if x>xs[0] ] :
            [] if xs == [] else
            qs( le ) + [ xs[0] ] + qs( gt )
    )()
)

Isn't it okay to be clean? I like it because there is no return ... Create another lambda expression inside and execute it with the default arguments. That means that the default arguments you set can be used as variables as they are, right?

You can use arguments and variables outside of yourself from the inner lambda expression. If you do something like lambda x = 1, y = x :, y will look for x outside of you instead of the next x = 1. Arguments that are not related are in the same lambda expression. If there is a reference relationship, change the stage. The number of stages will increase or decrease as needed.

By the way, it seems better not to set dynamic default arguments other than constants for the outermost arguments. The default value when it was first generated is recorded, and it usually does not work as expected.

Switch, execute sequentially ...

After all, what you want to do with lambda

  1. Branch according to the condition derived from the argument
  2. Impure? While executing operations (input / output, display, etc.) sequentially
  3. Do a pure calculation and return the value.

I feel like I'm on this. It's a little painful that only ~ if ~ else ~ can be used when there are many branches. I want to write the condition first and the operation later!

I wanted to make it. However, it doesn't have that much tech, so I approached it with a deceptive appearance.

First, prepare.

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

When using

switch(
	(Condition 1)    and do(Execution formula 1a,Execution formula 1b,...)
	or (Condition 2) and do(Execution formula 2a,Execution formula 2b,...)
	...
	or            do(Last executable a,Last executable b,...)
)

This will branch conditionally, pass the result as a tuple to switch () while evaluating the expressions in order with do (), and switch () will return the last element of the tuple. The final do () is the expression when all the conditions are false.

I put it in and out of the tuple so that the return value of the executable does not affect the control of and / or.

switch and do have no particular meaning. I thought it might be like that for humans, so I changed it. There is no function to imagine from the name.

In the quicksort example


qs=(lambda xs :
    (lambda 
        le = [x for x in xs[1:] if x<=xs[0] ] , 
        gt = [x for x in xs[1:] if x>xs[0] ] :
        switch(
            xs == [] and do( [] )  
            or do( qs( le ) + [ xs[0] ] + qs( gt ) )
        )
    )()
)

The number of characters has increased a little, but somehow the meaning seems to be conveyed. I don't think I'll make a mistake.

By the way, if you write the same thing normally

qs=(lambda xs :
    (lambda 
        le = [x for x in xs[1:] if x<=xs[0] ] , 
        gt = [x for x in xs[1:] if x>xs[0] ] :
        (
            xs == [] and ( [], )  
            or ( qs( le ) + [ xs[0] ] + qs( gt ) ,)
        )[-1]
    )()
)

This is neat with this. It's a Han. But I'm not sure what I'm doing, and when I write, I'll forget about [-1] and commas.

Recommended Posts

Try assigning or switching with Python: lambda
Try scraping with Python.
Operate TwitterBot with Lambda, Python
Try Python output with Haxe 3.2
Try running Python with Try Jupyter
Try face recognition with Python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
Try scraping with Python + Beautiful Soup
Try to operate Facebook with Python
Face detection with Lambda (Python) + Rekognition
Try singular value decomposition with Python
Run mruby with Python or Blender
Try face recognition with python + OpenCV
Try frequency control simulation with Python
Notify HipChat with AWS Lambda (Python)
Use PostgreSQL with Lambda (Python + psycopg2)
[SAM] Try using RDS Proxy with Lambda (Python) [user/pass, IAM authentication]
[AWS] Using ini files with Lambda [Python]
Try logging in to qiita with Python
Try mathematical formulas using Σ with python
Try working with binary data in Python
Manipulate DynamoDB data with Lambda (Node & Python)
Try HTML scraping with a Python library
Try calling Python from Ruby with thrift
Connect to s3 with AWS Lambda Python
Try drawing a map with python + cartopy 0.18.0
[Continued] Try PLC register access with Python
[For beginners] Try web scraping with Python
Python + Selenium + Headless Chromium with aws lambda
Try to make foldl and foldr with Python: lambda. Also time measurement
nginxparser: Try parsing nginx config file with Python
FizzBuzz with Python3
AWS-Perform web scraping regularly with Lambda + Python + Cron
Scraping with Python
Statistics with python
Try it with Word Cloud Japanese Python JupyterLab.
Scraping with Python
Python with Go
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
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
Twilio with Python
Try implementing a Cisco Spark bot with AWS Lambda + Amazon API Gateway (Python)
Play with 2016-Python
Try to make a "cryptanalysis" cipher with Python
LINE BOT with Python + AWS Lambda + API Gateway
Tested with Python
Try to automatically generate Python documents with Sphinx
Serverless application with AWS SAM! (APIGATEWAY + Lambda (Python))
with syntax (Python)
Try working with Mongo in Python on Mac
[AWS] Try tracing API Gateway + Lambda with X-Ray
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Export RDS snapshot to S3 with Lambda (Python)
Bingo with python
Try to make a dihedral group with Python
Upload files to Google Drive with Lambda (Python)
Little-known Python language specification (compatible with Python 3.4 or later)
Excel with Python
Try to detect fish with python + OpenCV2.4 (unfinished)