我想在不使用临时变量或lambda的情况下将变量传递给匹配大小写.想法:
let temp =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
let result =
match temp with
| Case1 -> "Output 1"
| Case2 -> "Output 2"
| _ -> "Other Output"
Run Code Online (Sandbox Code Playgroud)
我希望写一些类似于以下内容:
// IDEAL CODE (with syntax error)
let result =
x
|> Function1
|> Function2
// ........ Many functions later.
|> FunctionN
|> match with // Syntax error here! Should use "match something with"
| Case1 -> "Output 1"
| Case2 -> "Output …Run Code Online (Sandbox Code Playgroud) 这个问题很相似: F#管道第一个参数
我目前正在学习F#和函数式编程,我想知道是否有一种简单的方法来管道转发第一个参数(而不是最后一个参数).
例如,如果我想管道最后一个参数,它看起来非常漂亮和干净:
[4;5;6]
|> List.append [1;2;3]
// Result: [1;2;3;4;5;6]
Run Code Online (Sandbox Code Playgroud)
如果我想管道转发第一个参数,我可以使用"有趣的x - >"函数,但我很好奇是否有更简洁的方法.
[1;2;3]
|> fun x -> List.append x [4;5;6]
// Result: [1;2;3;4;5;6]
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
PS我的同事刚刚帮我解决了这个问题.但如果您对F#初学者有任何建议,我将不胜感激.谢谢.
这个问题仅供娱乐.请不要太认真地对待这个问题.
我目前正在学习F#,我很想知道是否有一种简洁的方法来定义map4,使用现有函数List.map2,List.map3,管道前进/后退,前进/后退组合等.
即
let map4 f a b c d = ......
map4 f [a1;a2] [b1;b2] [c1;c2] [d1;d2]
// output: [f(a1,b1,c1,d1); f(a2,b2,c2,d2)]
Run Code Online (Sandbox Code Playgroud)
我可以递归地解决这个问题,或者通过定义一个新的运算符(参见以下URL)
http://www.fssnip.net/9W/title/nary-Seqmap-
http://call-with-cc-en.blogspot.sg/2009/04/applicative-functors-mapping-function.html
我也可以通过组合List.map2和List.map3来解决这个问题,使用部分应用的函数f(a,b,?,?)
let map4 f a b c d =
List.map3 (fun g y -> g y) (List.map2 f a b) c d
Run Code Online (Sandbox Code Playgroud)
我可以尝试使用前向合成来缩短上面的代码(并使其尽可能抽象/混淆)
let map4 f a =
List.map2 f a >> List.map3 id;;
// Output type: f:('a -> 'b -> 'c -> 'd -> 'e) ->
// a:'a list -> ('b list -> 'c …Run Code Online (Sandbox Code Playgroud) 我想添加一个多行XML文档,当我的鼠标悬停在它上面时,它会出现在Intelli-Sense气泡中.参见例如:
当我使用Visual Studio 2015时,以下代码有效(请参见下面的屏幕截图),但现在当我使用Visual Studio 2017时,它不再起作用了:
请参阅VS2015和VS2017中的以下屏幕截图
VS2015:
VS2017:
搜索stackoverflow和其他网站后,我尝试了一些代码,但它仍然无法正常工作.
// My failed guesses
/// This also does not work
/// & This does not work
let EXAMPLE3 = 2.0
/// This also does not work
/// \n This does not work
let EXAMPLE4 = 2.0
/// This also does not work
/// <br> This does not work
let EXAMPLE5 = 2.0
/// <summary>
/// This also does not work
/// <para> This does not work </para>
/// …Run Code Online (Sandbox Code Playgroud) 备注:这是一个自我记录,但如果你有任何其他建议,或者如果我犯了任何错误/错过了明显的东西,我将非常感谢你的帮助.
资料来源:
我是F#和C#的初学者,我想将C#List转换为F#List,反之亦然.执行此操作的代码/命令会根据代码的位置(在C#或F#中)而更改.
请参阅下面的答案.如果您有任何其他建议,请告诉我.谢谢.
F# 中是否有字典的构造函数(以 Seq/List 作为输入,并输出字典)?我编写了以下代码来实现我想要的功能,但我只是好奇它是否已经实现(所以我不需要自己实现它)
let DictionaryBuilder (keyFunc:'a->'b) (valueFunc:'a->'c) aList =
let dict = new Dictionary<'b,'c>()
aList
|> Seq.iter (fun a -> dict.Add(keyFunc a, valueFunc a ))
dict // return
Run Code Online (Sandbox Code Playgroud)
我知道在 C# 中,您可以使用 .ToDictionary (使用 System.Linq)
// using System.Collections.Generic;
// using System.Linq;
List<string> example = new List<string> {"a","b","c"};
Dictionary<string,string> output = example.ToDictionary(x => x+"Key", x => x+"Value");
// Output: {"aKey": "aValue", "bKey": "bValue", "cKey": "cValue"}
Run Code Online (Sandbox Code Playgroud)
非常感谢。
F#
我有兴趣阅读csv文件并输出List <List <string >>
let readCsv (filepath:string) : string list list =
//.......................
input file:
Quote1,Quote2,Quote3
"Hello,World","He said:""Yes""",Example
Output:
// Type: string list list
[["Quote1";"Quote2";"Quote3"];
["Hello,World"; "He said:"Yes"";"Example"]]
Input2:
1,2,3,4,5,6
7,8,9,10,11,12
Output2:
// Type: string list list
[["1";"2";"3";"4";"5";"6"];
["7";"8";"9";"10";"11";"12"]]
Run Code Online (Sandbox Code Playgroud)
但是,一些Nuget包,例如CsvHelper,FileHelper,F#Data依赖于定义一个Class来"捕获"数据,或者通过引用csv文件来定义一个类型.
https://joshclose.github.io/CsvHelper/
http://www.filehelpers.net/example/QuickStart/ReadWriteRecordByRecord/
http://fsharp.github.io/FSharp.Data/index.html
例如:
// In C#, from FileHelper Documentation
[DelimitedRecord(",")]
public class AbstractClass
{
public string Quote1;
public string Quote2;
public string Quote3;
}
Run Code Online (Sandbox Code Playgroud)
要么
// F# Data Documentation
type AbstractType = CsvProvider<"../example.csv">
Run Code Online (Sandbox Code Playgroud)
但是输入文件的列数可能会改变(因此我无法定义抽象类)
当然,我可以编写正则表达式来逐行分解输入文件,但我很想知道其他人是否已经完成它(或者它是标准的库函数).
谢谢.
是否有可能在C#中解构一个元组,类似于F#?例如,在F#中,我可以这样做:
// in F#
let tupleExample = (1234,"ASDF")
let (x,y) = tupleExample
// x has type int
// y has type string
Run Code Online (Sandbox Code Playgroud)
是否有可能在C#中做类似的事情?例如
// in C#
var tupleExample = Tuple.Create(1234,"ASDF");
var (x,y) = tupleExample;
// Compile Error. Maybe I can do this if I use an external library, e.g. LINQ???
Run Code Online (Sandbox Code Playgroud)
或者我是否必须手动使用Item1,Item2?例如
// in C#
var tupleExample = Tuple.Create(1234,"ASDF");
var x = tupleExample.Item1;
var y = tupleExample.Item2;
Run Code Online (Sandbox Code Playgroud) 我目前正在学习函数式编程和F#,我想在n-2之前进行循环控制.例如:
Given a list of doubles, find the pairwise average,
e.g. pairwiseAverage [1.0; 2.0; 3.0; 4.0; 5.0] will give [1.5; 2.5; 3.5; 4.5]
Run Code Online (Sandbox Code Playgroud)
在做了一些实验和搜索后,我有几种方法可以做到:
方法1:
let pairwiseAverage (data: List<double>) =
[for j in 0 .. data.Length-2 do
yield (data.[j]+data.[j+1])/2.0]
Run Code Online (Sandbox Code Playgroud)
方法2:
let pairwiseAverage (data: List<double>) =
let averageWithNone acc next =
match acc with
| (_,None) -> ([],Some(next))
| (result,Some prev) -> ((prev+next)/2.0)::result,Some(next))
let resultTuple = List.fold averageWithNone ([],None) data
match resultTuple with
| (x,_) -> List.rev x
Run Code Online (Sandbox Code Playgroud)
方法3:
let pairwiseAverage …Run Code Online (Sandbox Code Playgroud) 我有兴趣实现fold3,fold4等,类似于List.fold和List.fold2.例如
// TESTCASE
let polynomial (x:double) a b c = a*x + b*x*x + c*x*x*x
let A = [2.0; 3.0; 4.0; 5.0]
let B = [1.5; 1.0; 0.5; 0.2]
let C = [0.8; 0.01; 0.001; 0.0001]
let result = fold3 polynomial 0.7 A B C
// 2.0 * (0.7 ) + 1.5 * (0.7 )^2 + 0.8 * (0.7 )^3 -> 2.4094
// 3.0 * (2.4094) + 1.0 * (2.4094)^2 + 0.01 * (2.4094)^3 -> 13.173
// 4.0 * (13.173) …Run Code Online (Sandbox Code Playgroud) F#管道转发可以表示为:
let (|>) x f = f x
Run Code Online (Sandbox Code Playgroud)
例如:
let SimpleFunction (a : typeA) (b : typeB) (c : typeC)=
printf "OK."
// No problem here.
SimpleFunction a b c
// Using pipe-forward
c |> SimpleFunction a b
// No problem here. Interpreted as the same as above.
Run Code Online (Sandbox Code Playgroud)
但是,根据文档,管道转发操作符是左关联的.
https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/symbol-and-operator-reference/
所以,我期待管道前进声明:
// Original Expression
c |> SimpleFunction a b
// to be equivalent to:
(c |> SimpleFunction) a b
// Which is equivalent to:
SimpleFunction c a b
// Error!! …Run Code Online (Sandbox Code Playgroud) 在F#中是否有Dictionary类"System.Collections.Generic.Dictionary"的缩写/替代名称?
例如,C#List类型"System.Collections.Generic.List"在F#中有一个名为"ResizeArray"的缩写.
所以,如果我在F#中使用C#列表,我可以使用"ResizeArray",而不是在我的文件开头使用"open System.Collections.Generic"(因为这会导致C#List模块和F#List模块,例如,请参阅上面链接的Microsoft网页上的代码示例)
F#中有一个关键字是"System.Collections.Generic.Dictionary"的缩写吗?这样我就不需要使用"open System.Collections.Generic"并导致C#List和F#List之间发生潜在冲突.
非常感谢你.