f#

Select XML node by attribute value

旧时模样 提交于 2019-12-24 03:07:06
问题 <location> <hotspot name="name1" X="444" Y="518" /> <hotspot name="name2" X="542" Y="452" /> <hotspot name="name3" X="356" Y="15" /> </location> I have a point variable and I need to select the node with its coordinates, then change an attribute value. I want to do something similar to: let node = xmld.SelectSingleNode("/location/hotspot[@X='542' @Y='452']") node.Attributes.[0].Value <- "new_name2" but taking attributes value by a variable (variable_name.X / variable_name.Y). 回答1: Personally

F# slicing array by indices array

霸气de小男生 提交于 2019-12-24 02:11:45
问题 How can I overload .[] for F# array to slice an array based on an arbitrary index array? For example: let x = [|1..10|]; let indx = [|4;1|]; although [| for i in indx ->x.[i]|] would work, it would be nicer to be able using x.[indx] directly. 回答1: You can always write an F# extension method to get close on the syntax let a = [| 1..10 |] let idx = [|4;1|] type 'T ``[]`` with //' member this.Slice(indices:int array) = [| for i in indices -> this.[i] |] printfn "%A" (a.Slice idx) but since

F#: Casting to generic type

▼魔方 西西 提交于 2019-12-24 02:00:57
问题 I'm fairly new to F# and coming from a C++ background. I am trying to write a simple vector class which can be of generic type (int, float, etc) but I run into trouble with the default constructor. I want to initialize the values to be zero but to do this I need to somehow cast a concrete zero to a generic type but I'm not sure how to do this. Perhaps some code might help. Here's what I have so far: type Vector3D<'T> (x :'T, y: 'T, z: 'T) = member this.x = x member this.y = y member this.z =

How to do typeof of a module in a fsx file?

老子叫甜甜 提交于 2019-12-24 01:58:09
问题 Let's say I have a Foo.fsx script that contains this code: module Bar = let foobar = "foo"+"bar" open Bar let a = System.Reflection.Assembly.GetExecutingAssembly() let ty = a.GetType("Foo.Bar") // but it returns null here How can I achieve that? Thanks! 回答1: This is a tricky question, because F# interactive compiles all types into types with some mangled names (and the executing assembly can contain multiple versions of the same type). I managed to do it using a simple trick - you'd add an

Can F# Quotations be used to create a function applicable to arbitrary F# record types?

倾然丶 夕夏残阳落幕 提交于 2019-12-24 01:47:34
问题 Given an F# record: type R = { X : string ; Y : string } and two objects: let a = { X = null ; Y = "##" } let b = { X = "##" ; Y = null } and a predicate on strings: let (!?) : string -> bool = String.IsNullOrWhiteSpace and a function: let (-?>) : string -> string -> string = fun x y -> if !? x then y else x is there a way to use F# quotations to define: let (><) : R -> R -> R with behaviour: let c = a >< b // = { X = a.X -?> b.X ; Y = a.Y -?> b.Y } in a way that somehow lets (><) work for

F# OleDb Syntax Error in INSERT INTO Statement Pulling Data from Access to Linked SQL Server

一个人想着一个人 提交于 2019-12-24 01:02:01
问题 I'm running an F# application to pull data from an Access table to a linked SQL Server table. Here is the resulting query: INSERT INTO dbo_TempTerm (UnitID, PolicyTermYear, InsuredName, PolicyNumber, RenewalDate, CovATotal, CovBTotal, CovLTotal, DwellExtn, AllOtherPerilDeductible, MedPay, TotalPremium, HurricaneDeductible, Zone, Subzone, PercentCRC, PercentCRD, YearBuilt, RenYrs, PercentCFD, PercentHA, PercentMLD, GRP1, PercentNH, QCLM, RateV, CRI, AgentCode, AgentName, AFOCode, PolicyType,

Which to use , abstract class or interface in F#?

倖福魔咒の 提交于 2019-12-24 00:56:58
问题 Been grokking F# coming from a C# background. In C# there is a clear difference in deciding when to use interfaces and when to use abstract classes. In F# I see the two blurring almost into one. I understand under the hood that the same is being done in F# as c# as far as the CLR is concerned, but what is the "best practise" when programming in F# to use? Should I avoid class inheritance altogether? 回答1: I think that interfaces tend to be used much more frequently than abstract classes

how to read file and skip some white spaces

[亡魂溺海] 提交于 2019-12-24 00:48:57
问题 this is similiar to my previous question, but there is another improvisation, how is the code if i want to skip some white spaces, for this case is "enter", for example: 5 5 10 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 <- this white space 3 4 4 1 2 3 4 1 2 3 4 1 2 3 4 i try to my best but couldn't find how to skip the white space thank you for the help :) This is the answer, thanks to Ramon : let readMap (path:string) = let lines = File.ReadAllLines path let [|x; y; n|] = lines.[0]

Xamarin, Android. Exception when opening *.axml files in VS2015 using F#,

你。 提交于 2019-12-24 00:38:12
问题 I receive the following error message when opening layout files in F# project. System.NullReferenceException: Object reference not set to an instance of an object. at Xamarin.VisualStudio.Android.MonoAndroidDesignerInterface.VisualStudioCodeModelBridge.get_EnclosingProject() in c:\Users\builder\data\lanes\3822\3b7df6f5\source\xamarinvs\src\Core\VisualStudio.Android\Designer\MonoAndroidDesignerInterface.cs:line 180 at Xamarin.AndroidDesigner.CodeInteractions.CodeModelBridge.d__12.MoveNext() --

Asynchronous Webcrawling F#, something wrong?

人走茶凉 提交于 2019-12-24 00:36:18
问题 Not quite sure if it is ok to do this but, my question is: Is there something wrong with my code ? It doesn't go as fast as I would like, and since I am using lots of async workflows maybe I am doing something wrong. The goal here is to build something that can crawl 20 000 pages in less than an hour. open System open System.Text open System.Net open System.IO open System.Text.RegularExpressions open System.Collections.Generic open System.ComponentModel open Microsoft.FSharp open System