c#-to-f#

Converting C# code to F# (if statement)

…衆ロ難τιáo~ 提交于 2020-01-02 03:50:27
问题 I'd like to know how to convert this code line by line from C# to F#. I am not looking to use any kind of F#'s idioms or something of the like. I am trying to understand how to map directly C#'s constructs to F#. Here is the C# code: //requires l.Length > 0 int GetMinimumValue(List<int> l) { int minVal = l[0]; for (int i = 0; i < l.Length; ++i) { if (l[i] > minValue) { minVal = l[i]; } } return minVal; } And here is my F# attempt: let getMinValue (l : int list) = let minVal = l.Head for i = 0

How to consume HttpClient from F#?

匆匆过客 提交于 2020-01-02 01:05:10
问题 I'm new to F# and stuck in understanding async in F# from the perspective of a C# developer. Say having the following snippet in C#: var httpClient = new HttpClient(); var response = await httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); How to write the same in F#? 回答1: Here is a function that should do what you're looking for (note that you'll have to wrap the code in an asynchronous computation expression in order to

C# to F# Convert public partial class Device : MarshalByRefObject

∥☆過路亽.° 提交于 2019-12-30 21:59:46
问题 public partial class Device : MarshalByRefObject { internal bool FindTagName(string name, OneTag tag) { foreach (FuncSect fs in listFuncSect) { foreach (OneTag ot in fs.listTags) { if (ot != tag && ot.Name == name) return true; } } return false; } still have no idea how to convert this "partial" and "internal" to F# thank you 回答1: As leppie says, there's no direct support for partial , although you could achieve a similar effect with intrinsic type extensions. F# does support internal methods

C# to F# Convert public partial class Device : MarshalByRefObject

╄→гoц情女王★ 提交于 2019-12-30 21:59:45
问题 public partial class Device : MarshalByRefObject { internal bool FindTagName(string name, OneTag tag) { foreach (FuncSect fs in listFuncSect) { foreach (OneTag ot in fs.listTags) { if (ot != tag && ot.Name == name) return true; } } return false; } still have no idea how to convert this "partial" and "internal" to F# thank you 回答1: As leppie says, there's no direct support for partial , although you could achieve a similar effect with intrinsic type extensions. F# does support internal methods

F# Extension Methods on Lists, IEnumerable, etc

雨燕双飞 提交于 2019-12-25 04:45:32
问题 In C#, if I had a widget definition, say: class widget { public string PrettyName() { ... do stuff here } } and I wanted to allow for easy printing of a list of Widgets, I might do this: namespace ExtensionMethods { public static PrintAll( this IEnumerable<Widget> widgets, TextWriter writer ) { foreach(var w in widgets) { writer.WriteLine( w.PrettyName() ) } } } How would I accomplish something similar with a record type and a collection (List or Seq preferrably in F#). I'd love to have a

Converting complex Map data structure to F#

牧云@^-^@ 提交于 2019-12-24 18:12:51
问题 I am pretty new to the language F#, let alone functional programming, and I am having trouble implementing a Map data structure. The C# type equivalent of this Map would be: var map = new Dictionary<int, Dictionary<int, Tuple<char, Tuple<int, int>[]>[]>>(); I've tried to implement this myself and searching online, but my inexperience with the language is letting me down. Is anyone able to show me: An immutable implementation of this structure A mutable implementation 回答1: For example like

Quartz.NET and F# - SystemTime and KeyMatcher

99封情书 提交于 2019-12-24 14:45:02
问题 I am trying to work with Quartz.NET in F# and have run into a few issues with the fact that, while Quartz.NET is usable in F#, there does not seem to be much documentation on it, and I've had some difficulty with differences between it and what can find in C#. One issue I have currently run into is setting SystemTime such as shown in this question, Quartz.net + testing with SystemTime.UtcNow. I could be wrong, but I thought that the code in F# should be: SystemTime.Now = fun () -> DateTime

Public Mutable Field in Object

拈花ヽ惹草 提交于 2019-12-24 05:32:27
问题 Is it possible to create a simple public mutable field in F#? I'm creating a library that I will be accessing from a C# program and I need to be able to set a field from C#. //C# Equivalent public class MyObj { public int myVariable; } //F# type MyObj = //my variable here member SomeMethod() = myVariable <- 10 //C# Usage MyObj obj = new MyObj(); obj.myVariable = 5; obj.SomeMethod() 回答1: type MyObj() = [<DefaultValue>] val mutable myVariable : int member this.SomeMethod() = this.myVariable <-

Public Mutable Field in Object

依然范特西╮ 提交于 2019-12-24 05:32:18
问题 Is it possible to create a simple public mutable field in F#? I'm creating a library that I will be accessing from a C# program and I need to be able to set a field from C#. //C# Equivalent public class MyObj { public int myVariable; } //F# type MyObj = //my variable here member SomeMethod() = myVariable <- 10 //C# Usage MyObj obj = new MyObj(); obj.myVariable = 5; obj.SomeMethod() 回答1: type MyObj() = [<DefaultValue>] val mutable myVariable : int member this.SomeMethod() = this.myVariable <-

Token access, reuse and auto refresh within the SDK

寵の児 提交于 2019-12-23 04:17:11
问题 Coming from C# and other languages, and new to F# and trying to port an SDK Library that I built in an OO language. The SDK is responsible for retrieving access token first, setting on a static field and then setting a specific interval to continuosly refresh the token before it's expiry. The token is set as static field on the Authentication class and is updated every time before it reaches expiry. Then other actors within the SDK reach out to Authentication class, read it's static field