前卫管道运营商的怀疑 [英] Foward pipe operator doubt

查看:71
本文介绍了前卫管道运营商的怀疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 abs(float x.[1]-float x.[4]))
|> (有趣的x-> x.[0])

为什么我不能使用下面的代码?

(有趣x-> x.[0])( List.map splitCommas stockData)


解决方案

您必须在List.map周围添加方括号,并告诉编译器第一个x参数是哪种类型(要么显式[类型注释]或隐式地[让推论与Array模块上的函数一起工作来推断]).

您需要它,因为此时编译器没有x的信息,并且不知道它可以expr.[idx]运算符

(fun(x:_ [])-> x.[0])(List.maxBy(fun x-> abs(float x.[1]-float x .[4]))(List.map splitCommas stockData))

//或者
(fun x-> Array.get x 0)(List.maxBy(fun x-> abs(float x.[1]-float x.[4]))(List.map splitCommas stockData))


(对我来说)真正的问题是为什么您要这样做?


from the lesson:

let stockData = [ "2012-03-30,32.40,32.41,32.04,32.26,31749400,32.26"; "2012-03-29,32.06,32.19,31.81,32.12,37038500,32.12"; ............................................ "2012-03-01,31.93,32.39,31.85,32.29,77344100,32.29"; "2012-02-29,31.89,32.00,31.61,31.74,59323600,31.74"; ] let splitCommas (x:string) = x.Split([|','|]) stockData |> List.map splitCommas |> List.maxBy (fun x -> abs(float x.[1] - float x.[4])) |> (fun x -> x.[0])

Why I can not use the code below instead? and How can i code without pipe?

(fun x -> x.[0])(List.maxBy (fun x -> abs(float x.[1] - float x.[4]))

List.map splitCommas stockData)


解决方案

you have to add brackets around the List.map and also tell compiler which type is the first x argument (either explicitely [using a type annotation] or implicitely [letting inference operate with a function on Array module to deduce]).

You need it because at this point the compiler has no info on x and doesn't know it can expr.[idx] operator on it

(fun (x: _[]) -> x.[0]) (List.maxBy (fun x -> abs (float x.[1] - float x.[4])) (List.map splitCommas stockData))

// alternatively
(fun x -> Array.get x 0) (List.maxBy (fun x -> abs (float x.[1] - float x.[4])) (List.map splitCommas stockData))


The real question (for me) is why you would want to do that ?


这篇关于前卫管道运营商的怀疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆