边缘化多元 lambda 函数

问题描述

我有一个函数 f 取决于可变数量的参数。例如,假设它取决于 4 个参数。

f = lambda x_1,x_2,x_3,x_4: *function’s body*

给定一个任意大小为 3 的整数元组 t,我想通过每次省略一个参数来评估这个元组上的函数 f:

f_1 = lambda x_1 : f(x_1,*t)
f_2 = lambda x_2 : f(t[0],*t[1:])
f_3 = lambda x_3 : f(t[0],t[1],*t[2:])
f_4 = lambda x_4 : f(*t,x_4)

对于任意数量的参数,是否有一种通用方法来计算这些函数?我想遍历参数,但绑定值似乎很麻烦,尤其是当参数数量可变时。

这将帮助我计算函数的边际。我希望我的问题很清楚!

解决方法

<div class="modal fade" id="exampleModal{{$form->id}}" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Form Information</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <p class="badge badge-pill badge-dark">Name: {{$form->user->name}}</p> <p>Purpose : {{$form->purpose->name}}</p> <p>Location : {{$form->location->location}}</p> <!-- <table class="table"> <tbody> <tr> <th scope="row">Connection</th> <td><input type="checkbox" name="connection[]" value="request">Request</td> <td><input type="checkbox" name="connection[]" value="connection">Connection</td> </tr> </tbody> </table> --> @foreach() <div class="col-md-6 mt-2"> <label for="">waw</label> </div> @endforeach <p>Division : {{$form->division}}</p> <div class="modal-footer"> <button type="button" class="btn btn-primary" data-bs-dismiss="modal">Close</button> </div> </div> </div> </div> 接受一个函数和一个固定参数列表,并返回一个接受一个参数的子函数列表。

  1. 结果函数应使用 make_subfunctions 调用原始函数。我们需要 f(*fixed_args[:i],the_only_argument,*fixed_args[i:]) 或代理来完成这项工作。
  2. 每个包装器的 wrapper()fixed_args[:i] 都是固定的,我们可以使用 fixed_args[i:] 来预填充这两个参数,并让调用者填充其余的动态参数。因此,包装签名是 functools.partial
  3. (head,tail,x)start 参数指定要插入固定参数的动态参数的(包含)索引。

输出:

last
('a','b','c','d')
('a',1,2,'d',3)