f#

Unclear function return type issue

夙愿已清 提交于 2019-12-10 18:12:24
问题 I have the following rather simple F# function: let FormatValue (formatProvider : IFormatProvider) valueSuffix value = match value > Convert.ToDecimal(valueSuffix.MinimumValueRequired) with | true -> let normalizedValue = Convert.ToDecimal(value) / Convert.ToDecimal((Math.Pow(10., Convert.ToDouble(valueSuffix.PowerOfTen)))) in string.Format("{0}{1}", normalizedValue.ToString(valueSuffix.Format, formatProvider), valueSuffix.Text) | false -> "" The return type is correctly inferred as string ,

F#: Is Profile 47 required for Microsoft.FSharp.Data.TypeProviders?

元气小坏坏 提交于 2019-12-10 18:09:46
问题 This is a follow-up to my post yesterday. To recap, I received this error message when trying to build my project: FSC: Error FS2024: Static linking may not use assembly that targets different profile I consulted some kind people in the F# fpchat.com channel, and one of them suggested that the error could be due to the fact that I did not have Profile 47, because FSharp.Data uses Profile 47. I tried downloading the target pack for Profile 47, but was redirected to the Microsoft homepage

F#: Storing and mapping a list of functions

对着背影说爱祢 提交于 2019-12-10 17:50:04
问题 I have a number of events that happen in a game. I want to control the time and order at which these events occur. For example: Event 1: Show some text on screen for N frames & play a sound effect Event 2: Clear the text on the screen My solution (maybe there is a better one), is to have a list of functions that contain the events. The events perform their behavior then return the next events to occur in the game. I thought of using List.map or List.collect because I am essentially mapping a

How can I get this function to be tail-recursive?

被刻印的时光 ゝ 提交于 2019-12-10 17:43:04
问题 I'm still trying to implement 2-3 finger trees and I made good progress (repository). While doing some benchmarks I found out that my quite basic toList results in a StackOverflowException when the tree ist quite large. At first I saw an easy fix and made it tail-recursive. Unfortunately, it turned out that toList wasn't the culprit but viewr was: /// Return both the right-most element and the remaining tree (lazily). let rec viewr<'a> : FingerTree<'a> -> View<'a> = function | Empty -> Nil |

F# automatically inlines some functions even thought they are not marked with `inline`, is this intended?

天大地大妈咪最大 提交于 2019-12-10 17:37:51
问题 It appears that F# automatically inlines some functions, even though they are not marked with "inline". let a x= x + 3 let b x= x * x let funB x y = if x > y then 3 else 1 let funC x = let s = a x let c = funB s (b x) c + 1 By inspecting IL, I see the compiler has aggressively inlined funB & a,b funC: IL_0000: nop IL_0001: ldarg.0 IL_0002: ldc.i4.3 IL_0003: add IL_0004: stloc.0 // s IL_0005: ldarg.0 IL_0006: ldarg.0 IL_0007: mul IL_0008: stloc.1 IL_0009: ldloc.0 // s IL_000A: ldloc.1 IL_000B:

Passing discriminated unions to InlineData attributes

爷,独闯天下 提交于 2019-12-10 17:37:16
问题 I am trying to unit test a parser that parses a string and returns the corresponding abstract syntax tree (represented as a discriminated union). I figured it would be pretty compact to use Xunit.Extensions' attribute InlineData to stack all test cases on one another: [<Theory>] [<InlineData("1 +1 ", Binary(Literal(Number(1.0)), Add, Literal(Number(1.0))))>] ... let ``parsed string matches the expected result`` () = However, compiler complains that the second argument is not a literal

Why aren't F# records allowed to have AllowNullLiteralAttribute?

早过忘川 提交于 2019-12-10 17:37:05
问题 Is there a compiler implementation reason why records can't have the AllowNullLiteralAttribute attribute or is this a chosen constraint? I do see this constraint force cleaner code sometimes but not always. [<AllowNullLiteralAttribute>] type IBTreeNode = { mutable left : IBTreeNode; mutable right : IBTreeNode; mutable value : int} with member this.Insert x = if x < this.value then if this.left = null then this.left <- {left = null; right = null; value = x} else this.left.Insert x else if this

F# LINQ add new rows to SQL Server

自作多情 提交于 2019-12-10 17:36:07
问题 I am learning LINQ with F# 3.0. I want to know how to re-write my old code to use new LINQ features in F# 3.0 For example, I have created a simple data table in SQL Server 2008 R2, the database name is myDatabase. -- Create the Table1 table. CREATE TABLE [dbo].[Table1] ( [Id] INT NOT NULL, [TestData1] INT NOT NULL, [TestData2] FLOAT (53) NOT NULL, [Name] NTEXT NOT NULL, PRIMARY KEY CLUSTERED ([Id] ASC) ); For F# 2.0, I can use Dataset to add new rows, like this: #light open System open System

F# member constraints on tuples

拜拜、爱过 提交于 2019-12-10 17:35:55
问题 I commonly have a "oh yeah" moment writing F# when I realize I need an extra value somewhere. This is generally easily done by adding another value to the tuple being passed around. However, this means that various maps/sorts/collects/etc. need updating, and in particular the functions fst/snd only work on tuples of length 2. It's not a huge issue, but it's annoying enough during exploratory development that I though I'd write a helper to alleviate the annoyance: let inline get2 (t:^a) = (^a

With leftOuterJoin, .DefaultIfEmpty() is unnecessary

狂风中的少年 提交于 2019-12-10 17:33:25
问题 The documentation for leftOuterJoin Query Expressions on MSDN repeatedly implies through the samples that when using leftOuterJoin .. on .. into .. that you must still use .DefaultIfEmpty() to achieve the desired effect. I don't believe this is necessary because I get the same results in both of these tests which differ only in that the second one does not .DefaultIfEpmty() type Test = A | B | C let G = [| A; B; C|] let H = [| A; C; C|] printfn "%A" <| query { for g in G do leftOuterJoin h in