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.
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?
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.
After all, what you want to do with lambda
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