f#

Converting seq<string> to string[] in F#

与世无争的帅哥 提交于 2019-12-24 10:12:42
问题 The example from this post has an example open System.IO let lines = File.ReadAllLines("tclscript.do") |> Seq.map (fun line -> let newLine = line.Replace("{", "{{").Replace("}", "}}") newLine ) File.WriteAllLines("tclscript.txt", lines) that gives an error when compilation. error FS0001: This expression was expected to have type string [] but here has type seq<string> How to convert seq to string[] to remove this error message? 回答1: Building on Jaime's answer, since ReadAllLines() returns an

Polymorphism for both function inputs and outputs

元气小坏坏 提交于 2019-12-24 10:09:53
问题 Polymorphism for inputs was solved using functional idiomatic approach and now looking forward to solve it for return types that involved a JSON serializer which actually demands to know the type of the record. Input parameters were enforced to have some specific fields as mandatory and the solution allowed to pass different types as long as the field members existed. Solution was referred to as SRTP or structural (or duck) typing. Now I've a roadblock where this function is essentially a web

F# Sequences - how to return multiple rows

≯℡__Kan透↙ 提交于 2019-12-24 09:28:10
问题 let x = [for p in db.ParamsActXes do if p.NumGroupPar = grp then yield p.Num, p.Name] Here is my sequence but the problem is it returns the list of tuples, I can't access single tuple element like let str = "hello" + x.[1] and that is the trouble. how can I realize this functionary ? 回答1: To access the second element of a 2-tuple you can either use snd or pattern matching. So if tup is a 2-tuple, where the second element of tup is a string, you can either do: let str = "hello" + snd tup or

returning boolean value in function

心已入冬 提交于 2019-12-24 08:49:08
问题 I have this function: let myFunction list (var1, var2) : bool = for (x,y) in list do match (var1, var2) with | (1, 11) | (2, 22) -> true | (_, _) -> if not (x = y) then true // error is here false This returns an error saying that the function expects the value returned to have type bool, not unit. What I want to achieve is to return true whenever x != y so the loop should stop there; otherwise to return false at the end. 回答1: In F#, if statements can return. As a result, if you put the true

F# How to extract keys from a boxed Map object

浪子不回头ぞ 提交于 2019-12-24 07:38:02
问题 This is a follow-up of this post and that post. I need to write a function which takes an object ( obj type) and a key (also an obj type), and if the object happens to be a Map, that is any Map<'k,'v> then extracts its keys and values. The difficulty is that I cannot parametrize the function with generic types and that we cannot pattern-match objects on generic types. I am not familiar with F# Reflection, but I found a way to get the Map's values, once I know its keys. With this example code

Weird C# / F# difference in a declaration, code compiling in C# but not in F#

本小妞迷上赌 提交于 2019-12-24 06:06:37
问题 The line is to instantiate a queue of data points for the InfluxDB driver: C# Events = new ConcurrentQueue<InfluxDatapoint<InfluxValueField>>(); F# let Events = new ConcurrentQueue<InfluxDatapoint<InfluxValueField>>() in C#, it compiles without problem, but in F#, I get this: [FS0001] The type 'InfluxValueField' is not compatible with the type 'IComparable' Following the comment from canton7, here is the source for both external elements: InfluxValueField: https://github.com/AdysTech/InfluxDB

F# WsdlService type provider proxy

我与影子孤独终老i 提交于 2019-12-24 05:59:20
问题 I am following the MSDN tutorial for the WsdlService type provider found here. When I run it at home, it works as expected. When I write the same code at work, I am getting a design time exception: The type provider 'Microsoft.FSharp.Data.TypeProviders.DesignTime.DataProviders' reported an error: Error: Cannot obtain Metadata from http://msrmaps.com/TerraService2.asmx?WSDL Work does use a proxy and I have to alter the web.config to use a default proxy when consuming WSDL from a C# project in

Why am I getting access denied to these files?

寵の児 提交于 2019-12-24 05:47:15
问题 Suddenly I cannot rebuild my project in VS 2017. I get these messages: 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(4222,5): error MSB3021: Unable to copy file "C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.4.1.0\FSharp.Core.dll" to "bin\Debug\FSharp.Core.dll". Access to the path 'bin\Debug\FSharp.Core.dll' is denied. 1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community

Joining on the first finished thread?

老子叫甜甜 提交于 2019-12-24 05:38:50
问题 I'm writing up a series of graph-searching algorithms in F# and thought it would be nice to take advantage of parallelization. I wanted to execute several threads in parallel and take the result of the first one to finish. I've got an implementation, but it's not pretty. Two questions: is there a standard name for this sort of function? Not a Join or a JoinAll, but a JoinFirst? Second, is there a more idiomatic way to do this? //implementation let makeAsync (locker:obj) (shared:'a option ref)

F# exception handling multiple “Tries”

笑着哭i 提交于 2019-12-24 05:34:15
问题 I'm trying to read a bunch of csv files in SQL Server using SQL Bulk Insert and DataContext.ExecuteCommand. (Maybe this isn't the best way to do it, but it does allow me stay in the Type Provider context--as opposed to with SqlBulkCopy I think.) Now the upload is glitchy with intermittent success. Some files read in, some fail with "Data conversion error (truncation)". I think this has to do with the row terminators not always working. When the upload works, it seems to be with the '0x0A'