一线匿名方法

时间:2019-03-18 12:15:12

标签: c#

我有一个问题。考虑那个场景:

np.random.seed(2019)

L = ['s2', 's3', 's4', 's5','s6', 's7', 
     'k1', 'k2', 'k3', 'k4','k5', 'k6', 'k7', 
     'n1', 'n2', 'n3', 'n4','n5', 'n6', 'n7']

N = 5000000 
df = pd.DataFrame(np.random.choice([0,1], p=(.8,.2), size=(N, len(L))), columns= L)
#print (df)

In [311]: %timeit df['new_column'] = np.any(df[L].values.astype(bool), axis=1)
544 ms ± 18.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [312]: %timeit df['new_column'] = df[L].values.max(axis=1).astype(bool)
504 ms ± 16.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [313]: %timeit df['new_column'] = np.any(df[L].values, axis=1)
546 ms ± 36.9 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [315]: %timeit df['new_column'] = df[L].values.sum(axis=1).astype(bool)
428 ms ± 11 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

我的问题是,我们可以将 public override string Encrypt(string input) { Func<char, char> encryption = (c) => (char)chars[(c * k1 + k0) % 26]; string result = string.Empty; foreach(char c in input) result += encryption(c); return result; } 行更改为类似result += encryption(c)声明中的行吗?我们可以在一行中写这个匿名方法吗?

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

// Disables monitoring
mBeaconScannerServiceManager.disableMonitoring();

// Closes UI
finishAffinity();

// Destroys app completely after it had time to turn off services correctly
mContainerView.postDelayed(() -> System.exit(0), 2000);

答案 1 :(得分:2)

您可以尝试使用 Linq 。 e。

public override string Encrypt(string input)
{ 
    Func<char, char> encryption = (c) => (char)chars[(c * k1 + k0) % 26]; 

    return string.Concat(input.Select(c => encryption(c)));
}

我们甚至可以摆脱encryption

public override string Encrypt(string input) => 
   string.Concat(input.Select(c => (char)chars[(c * k1 + k0) % 26]));

答案 2 :(得分:2)

不是很容易阅读,但是整个方法简化为一行:

public override string Encrypt(string input) => string.Concat(input.Select(c => (char)chars[(c * k1 + k0) % 26]));