f#

How can I make an HTTP POST request in F#?

自作多情 提交于 2019-12-23 12:47:57
问题 The following code sends a GET request. This is making me crazy. let postDocRaw (url:string) (data: string) : string = let data' : byte[] = System.Text.Encoding.ASCII.GetBytes(data); let request = WebRequest.Create(url) request.Method <- "POST" request.ContentType <- "application/x-www-form-urlencoded" request.ContentLength <- (int64) data'.Length use wstream = request.GetRequestStream() wstream.Write(data',0, (data'.Length)) wstream.Flush() wstream.Close() (* use writer = new StreamWriter

unit test private methods in F#

眉间皱痕 提交于 2019-12-23 12:44:28
问题 Let's say we have a class type ThisClassIsComplicated () = let calculateSomething a b = a + b In this case calculateSomething is trivial, but if it would be more complicated it may make sense to verify that the calculations done there are correct. It might make sense to use a unit testing framework to test that private methods. My question: how to unit test private methods in F#? Some random thoughts: The selected answer here, suggests to use the InternalsVisibleTo attribute which anyway is

F# How should I think about delimiting items in sequences?

江枫思渺然 提交于 2019-12-23 12:43:49
问题 Apologies for a rookie question. I'm trying to change my mental paradigm from procedural to functional. For instance, suppose I have a list of names that I want to print like this "John, Paul, George, and Ringo." But this code does not satisfy: let names = [ "John"; "Paul"; "George"; "Ringo" ] names |> Seq.iter (fun s -> printf "%s, " s) My procedural instinct is to seek a way to insinuate a predicate into that lambda so that it can branch between ", " or ", and " or ". " depending upon where

ToString throws NullReferenceException for unit value ()

限于喜欢 提交于 2019-12-23 12:40:39
问题 Say we got a very simple function let fn a = a.ToString() It's type gets inferred as a -> string However, passing a unit value to the function results in a NullReferenceException. In case of simple functions like above, this might be worked around easily, but I'm actually in a more complex scenario: let eitherToHttp e = match e with | Either.Ok v -> OK (v.ToString()) | Either.Bad reason -> reasonToErrorCode reason The type of this is Either<'a, RejectReason> -> WebPart (what WebPart and

F# how to return have value a tuple or null

我的未来我决定 提交于 2019-12-23 12:39:08
问题 let retVal = if reader.Read() then (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2)) else null F# don't allow null to returned How can i have value return as a tuple or a null? 回答1: It is not that F# does not allow you to return null. It is because then part and else part have different types. You can use Option type. let retVal = if reader.Read() then Some (reader.GetString(0), getBytesData reader 1, reader.GetDateTime(2)) else None when you use retVal , you use pattern

F# List of Union Types

拥有回忆 提交于 2019-12-23 12:26:52
问题 I want a list of Reports. Report can be either Detail or Section types. module Data type Section = { Header: string; Lines: string list; Total: string } type Detail = { State: string; Divisions: string list; Sections: Section list } type Summary = { State: string; Office: string; Sections: Section list } type Report = Detail | Summary Then in my code, I'd like to do the following: let mutable (reports:Report list) = [] ... reports <- detail::reports // or reports <- summary::reports The

Which defferences “Seq” with “seq” ?

≡放荡痞女 提交于 2019-12-23 12:15:05
问题 I'm worried when don't know when you can use "Seq" , "seq" . Can you tell me which defferences are ? This's my code . Why dont't use "seq" ? let s = ResizeArray<float>() s.Add(1.1) s.Add(2.2) s.Add(3.3) s.Add(4.4) s |> Seq.iter (fun x -> printfn("%f") x ) 回答1: Seq is a module that contains functions that work with seq values: Seq.map string [ 1; 2 ] Seq.sum [ 1; 2 ] seq is a type name: let f1 (xs : seq<int>) = () let f2 (xs : int seq) = () seq is also a function that converts something like a

F# Connect SQLProvider with Postgres

青春壹個敷衍的年華 提交于 2019-12-23 12:09:57
问题 I'm a beginner in both F# and the .Net world, I'm trying to make this F# script work: #r "./packages/SQLProvider/lib/netstandard2.0/FSharp.Data.SqlProvider.dll" #r "./packages/SQLProvider/lib/netstandard2.0/netstandard.dll" #r "./packages/Npgsql/lib/netstandard2.0/Npgsql.dll" open FSharp.Data.Sql open Npgsql let [<Literal>] ConnString = "Host=localhost;" + "Port=5431;" + "Database=suavetododb;" + "Username=postgres;" + "Password=postgres" let [<Literal>] DbVendor = Common

Error FS0752 in F# declaration of a map over list of functions

点点圈 提交于 2019-12-23 12:08:54
问题 I would like to execute a list of functions over a list of corresponding values: let f1 x = x*2;; let f2 x = x+70;; let conslist = [f1;f2];; let pmap2 list1 list2 = seq { for i in 0..1 do yield async { return list1.[i] list2.[i] } } |> Async.Parallel |> Async.RunSynchronously;; Result: seq { for i in 0..1 do yield async { return list1.[i] list2.[i] } } ----------------------------------------------^^^^^^^^^ stdin(213,49): error FS0752: The operator 'expr.[idx]' has been used an object of

Should I null-protect my F# code from C# calls

喜欢而已 提交于 2019-12-23 12:08:33
问题 I am writing a library in F#, with some interfaces and base classes that are publicly visible. Generally, I avoid specifying [<AllowNullLiteral>] on my custom types as this complicates the validation logic in my F# code (see this nice post for goods and bads of null handing in F# to get a picture ), and also, F# does not initially allow null for F# types. So, I validate for nulls only for types that accept the null value as valid. However, an issues arises when my library is used from another