C# 在c中获取lambda输入#
我从未真正擅长C#,我正试图通过学习新事物来提高自己。我现在正在尝试兰姆达的 这是我目前的代码:C# 在c中获取lambda输入#,c#,lambda,C#,Lambda,我从未真正擅长C#,我正试图通过学习新事物来提高自己。我现在正在尝试兰姆达的 这是我目前的代码: public static Func<float[], bool[]> CategoricalMap(Func<float, bool> mapper) { Func<float[], bool[]> fun = x => new bool[] { true }; return fun; } publ
public static Func<float[], bool[]> CategoricalMap(Func<float, bool> mapper)
{
Func<float[], bool[]> fun = x => new bool[] { true };
return fun;
}
public static void Main()
{
Func<float, bool> success = x => x == 5.5f;
var result = CategoricalMap(success)(new float[] { 4f, 5.5f, 3.5f, -5.5f, 10.2f });
Console.ReadKey();
}
但我不知道如何从结果到函数获取浮点值。
请帮忙
编辑1
我想我应该把事情说得更清楚一些,几周前,我请了一个人给我lambda挑战,很遗憾我没有办法联系他。(我有点傻)
这是他给我的:
创建lambda success,如果给定值高于或等于5.5f,则返回true
创建以下函数:
分类地图
创建映射器函数
输入:mapper
Func类型的函数
输出:一个Func
类型的函数,它接受布尔值数组,并将函数mapper
应用于每个元素,并将结果存储在整数数组中
Func<float[], bool[]> fun = x => new bool[] { true };
现在您可以将其用作
public static Func<float[], bool[]> CategoricalMap(Func<float, bool> mapper)
{
Func<float[], bool[]> fun = x => {
var result = new bool[x.Length];
for(int i = 0; i < x.Length; ++i) {
result[i] = mapper(x[i]);
}
return result;
};
return fun;
}
public static void Main()
{
Func<float, bool> success = x => x == 5.5f;
var result = CategoricalMap(success)(new float[] { 4f, 5.5f, 3.5f, -5.5f, 10.2f });
Console.ReadKey();
}
公共静态函数分类映射(函数映射器)
{
Func fun=x=>{
var结果=新布尔值[x.长度];
对于(int i=0;ix==5.5f;
var结果=分类映射(成功)(新浮点[]{4f,5.5f,3.5f,-5.5f,10.2f});
Console.ReadKey();
}
但不要那样做。这种方法很糟糕。对于linq来说,这似乎过于复杂了<代码>变量匹配={4f,5.5f,3.5f,…},其中(x=>x==5.5f)代码>@gunr217是的,我知道我只是想不使用linq就尝试一下,通过这种方式我学到了更多。你似乎是为了使用lambdas才使用lambdas的。换句话说,我不明白这里为什么需要lambda
bool[]CategoricalMap(float someFloat,float[]floatArray)
就足够了,不是吗?哦,等等,你是在试图重新发明选择
?所以我猜你期望结果
是假,真,假,假,假
?提示:您需要一个循环。
Func<float[], bool[]> fun = x => {
var result = new bool[x.Length];
for(int i = 0; i < x.Length; ++i) {
result[i] = mapper(x[i]);
}
return result;
};
public static Func<float[], bool[]> CategoricalMap(Func<float, bool> mapper)
{
Func<float[], bool[]> fun = x => {
var result = new bool[x.Length];
for(int i = 0; i < x.Length; ++i) {
result[i] = mapper(x[i]);
}
return result;
};
return fun;
}
public static void Main()
{
Func<float, bool> success = x => x == 5.5f;
var result = CategoricalMap(success)(new float[] { 4f, 5.5f, 3.5f, -5.5f, 10.2f });
Console.ReadKey();
}