python反比例函数拟合_推广两个与两个属性成反比的函数(Generalizing two functions which act inversely on two properties)...

推广两个与两个属性成反比的函数(Generalizing two functions which act inversely on two properties)

我有这两个功能,我觉得必须有一个优雅的功能方式来概括它们。

$scope.togglePick = function(){

let targetPhoto = $scope.photoImport.data[indexService.current];

targetPhoto.pick = !targetPhoto.pick;

if(targetPhoto.reject && targetPhoto.pick) {

targetPhoto.reject = false;

}

}

$scope.toggleReject = function() {

let targetPhoto = $scope.photoImport.data[indexService.current];

targetPhoto.reject = !targetPhoto.reject;

if (targetPhoto.reject && targetPhoto.pick) {

targetPhoto.pick = false;

}

}

我想我可以传入字符串参数togglePick = toggle('pick', 'reject')并在toggle函数中使用[]表示法,但这感觉就像魔术字符串......即使它们是属性名称,我也是不确定。 我也可以:

function toggle(magicFlag){

let primary = magicFlag ? 'pick' : 'reject';

let secondary = magicFlag ? 'reject' : 'pick';

...

targetPhoto[primary] = !targetPhoto[primary];

...

}

再次,ick,魔术旗。 除了每个函数中的longhand之外,还有更优雅的方法吗?

I have these two functions, and I feel there must be an elegant functional way to generalize them.

$scope.togglePick = function(){

let targetPhoto = $scope.photoImport.data[indexService.current];

targetPhoto.pick = !targetPhoto.pick;

if(targetPhoto.reject && targetPhoto.pick) {

targetPhoto.reject = false;

}

}

$scope.toggleReject = function() {

let targetPhoto = $scope.photoImport.data[indexService.current];

targetPhoto.reject = !targetPhoto.reject;

if (targetPhoto.reject && targetPhoto.pick) {

targetPhoto.pick = false;

}

}

I suppose I could pass in string params togglePick = toggle('pick', 'reject') and use [] notation in the toggle function, but this feels like magic strings... even though they're property names, I'm uncertain. I could also have:

function toggle(magicFlag){

let primary = magicFlag ? 'pick' : 'reject';

let secondary = magicFlag ? 'reject' : 'pick';

...

targetPhoto[primary] = !targetPhoto[primary];

...

}

Again, ick, magic flag. Is there a more elegant way to do this than longhand in each function?

原文:https://stackoverflow.com/questions/37063533

更新时间:2019-12-10 04:28

最满意答案

这些函数中的代码量和细微的差别使它们变得很好。 如果没有处理属性字符串怪异,我不会看到你如何真正使这个泛型保持可读性 。 有时可以不使用最通用的代码。 必须在某处画出沙子中的那条线。

The amount of code in these functions and the slight differences makes them fine the way they are. Without working the property string weirdness, I don't see how you really make this generic while remaining readable. Sometimes it's okay not to have the most generic code you possibly can. That line in the sand has to be drawn somewhere.

2016-05-06

相关问答

如果满足以下条件,我倾向于使用属性: 该属性将返回单个逻辑值 很少或没有逻辑涉及(通常只返回一个值,或者做一个小的检查/返回值) 如果满足以下条件,我倾向于使用方法: 在返回价值时将会有重大的工作 - 即:它将从数据库中获取,或者可能需要“时间” 在获取或设置价值时,有相当多的逻辑 此外,我建议您查看Microsoft的“物业使用设计指南” 。 他们建议: 当成员是逻辑数据成员时使用属性。 使用以下方法: 该操作是一个转换,如Object.ToString。 该操作是足够昂贵的,您希望与用户沟通,

...

这些函数中的代码量和细微的差别使它们变得很好。 如果没有处理属性字符串怪异,我不会看到你如何真正使这个泛型保持可读性 。 有时可以不使用最通用的代码。 必须在某处画出沙子中的那条线。 The amount of code in these functions and the slight differences makes them fine the way they are. Without working the property string weirdness, I don't see h

...

首先,请注意您的函数mapInToOutEncode和mapInToOutDecode是相同的。 为了获得您的目标, mapInToOutDecode应该是: def mapInToOutDecode(inChar):

index = 0

found = 0

for char in keyString:

if(char == inChar):

found = index #remember it in a variable found

...

您可以传递lambda表达式: private void SetPurchaseOrderAddress(OrderAddress newAddress, Func addressIDGetter, Action addressIDSetter)

这样叫: SetPurchaseOrderAddress(newAddress, () => basketHelper.BillingAddressID, a => basketHelper.BillingAddres

...

不,但您可以轻松地传入一个匿名函数,调用您的两个(或更多)函数,如下所示: .animate(properties, function(){

callback1();

callback2();

});

callback1和callback2是函数。 No, but you could easily pass in an anonymous function that calls your two (or more) functions like this: .animate(pr

...

属性和方法的数量: Object.keys(obj).length

物业数量: #test used by underscore.js

function isMethod(obj, func) {

return !!(obj.func && obj.func.constructor && obj.func.call && obj.func.apply);

}

function num_properties(obj){

var size = 0;

for(key i

...

具有两个参数的属性将是UFCS设置器。 假设我们有: struct Whatever {

void setProperty(int);

}

并且您想要添加属性setter而不是调用setProperty,但无论出于何种原因都无法更改struct本身。 您可以添加这样的外部函数: @property int my_prop(ref Whatever _this, int prop_value) {

_this.setProperty(prop_value);

re

...

你可以使用1/Rank : ggplot(data, aes(x = x, y = y)) + geom_point(aes(size = 1/Rank))

You could use 1/Rank: ggplot(data, aes(x = x, y = y)) + geom_point(aes(size = 1/Rank))

JavaScript允许您为setter和属性定义getter(甚至在原型上): Object.defineProperty(Array.prototype, 'count', {

get: function () {

return this.length;

}

});

console.log([1, 2, 3].count);

谨慎使用。 颜色,具体而言, 使用非标准__defineGetter__函数 ,但效果相同。 JavaScript allows yo

...

您可以使用 (Public|Friend|Private)\s+(Property|Sub|Function)(\s+[GS]et)?\s+(\w+)\s*(\([^()]*\))(\s+As\s+\w+)?

看到这个正则表达式演示 说明: (Public|Friend|Private) # keyword

\s+ # blank

(Property|Sub|Function) # keyword2

(\s+[GS]et)?

...

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值