f#

Is an iteration on a F# map or set in-order traversal?

痴心易碎 提交于 2019-12-10 17:32:59
问题 AFAIK, F# Map and set are implemented as red-black trees, so I guess that an iteration on these would be in-order traversal. I did some test and the iteration results are always sorted. But I want to make it sure. Is it in-order traversal? 回答1: The documentation on MSDN is pretty good for figuring this out. For instance, the return value for Set.toSeq is "An ordered sequence of the elements of set." It looks like the answer to your question is yes, for both maps and sets. 回答2: AFAIK, F# Map

Looping vs recursion with F#

僤鯓⒐⒋嵵緔 提交于 2019-12-10 17:32:47
问题 The example code here solves a project Euler problem: Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows: 21 22 23 24 25 20 7 8 9 10 19 6 1 2 11 18 5 4 3 12 17 16 15 14 13 It can be verified that the sum of the numbers on the diagonals is 101. What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way? but my question is a matter of functional programming style rather than about how to get the

How to deal a card using F#

久未见 提交于 2019-12-10 17:32:15
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 4 years ago . I've been working on modeling a popular card game (Love Letter) using F# to learn more about functional programming. module Game = open Cards open Players type Deck = Card list let dealACard (deck:Deck) = let randomGenerator = System.Random() let index = randomGenerator.Next deck.Length let card = deck.Item index (card, (deck |> List.filter((<>) card))) let

Json.NET make property required based on property type

蹲街弑〆低调 提交于 2019-12-10 17:26:10
问题 I'm struggling with custom json serialization in .Net core, I'm trying to make all properties required by default except if property has specific type. Here is an example of what I'm trying to achieve: Let's assument that I have following type: F#: type FooType = { id: int name: string optional: int option } you can think about below code as similar to following in C#: class FooType = { int Id {get;set;}; string Name {get;set;}; Nullable<int> Optional {get;set;}; } What I'm trying to do is to

F# Visual Power Tools doesn't work

泄露秘密 提交于 2019-12-10 17:23:51
问题 I'm trying to work with F# . For all projects in VS2015 solution i want to have similar code style. I have installed Visual F# Power Tools and setted formatting configuration to: According to this configuration (space after comma) the following code: fun unit ->Log.Information( "Found: {category}\\{name}\\{instance}",category,name,instance ) should be converted to: fun unit ->Log.Information( "Found: {category}\\{name}\\{instance}", category, name, instance ) But when i'm trying to format

How to resolve “Main module of program is empty: nothing will happen when it is run”

跟風遠走 提交于 2019-12-10 17:22:21
问题 I have two projects in an F# solution. 1. main project with [EntryPoint] and set as the StarUp project. 2. support, the second project, holds a group of support modules. I.e. they are only called and never initiate anything nor serve as the entry point nor are the StartUp project. For the last module in the support project, compiling in Visual Studio gives warning FS0988: Main module of program is empty; nothing will happen when it is run While using compiler option nowarn inline as #nowarn

Magic sprintf function - how to wrap it?

∥☆過路亽.° 提交于 2019-12-10 17:19:45
问题 I am trying to wrap a call to sprintf function. Here's my attempt: let p format args = "That was: " + (sprintf format args) let a = "a" let b = "b" let z1 = p "A %s has invalid b" a This seems to work, output is val p : format:Printf.StringFormat<('a -> string)> -> args:'a -> string val a : string = "a" val b : string = "b" val z1 : string = "That was: A a has invalid b" But it wouldn't work with more than one arg: let z2 = p "A %s has invalid b %A" a b I get compile-time error: let z2 = p "A

Why can't the NUnit Test Adapter find my FsUnit tests?

大城市里の小女人 提交于 2019-12-10 17:17:38
问题 I'm using Visual Studio Professional 2015 and I have version 2.0.0.0 of the NUnit Test Adapter installed. It doesn't discover any tests on building the following code: namespace SmallestDivisibleIntegers module Core = let f n = [2..4] |> List.map (fun x -> x + n - n % x) module Tests = open FsUnit open NUnit.Framework open Core [<Test>] let ``Correct answers`` () = f 1 |> should equal [2; 3; 4] f 4 |> should equal [6; 6; 8] f 43 |> should equal [44; 45; 44] f 123 |> should equal [124; 126;

fold or choose till None?

隐身守侯 提交于 2019-12-10 17:17:34
问题 Is there already a way to do something like a chooseTill or a foldTill , where it will process until a None option is received? Really, any of the higher order functions with a "till" option. Granted, it makes no sense for stuff like map, but I find I need this kind of thing pretty often and I wanted to make sure I wasn't reinventing the wheel. In general, it'd be pretty easy to write something like this, but I'm curious if there is already a way to do this, or if this exists in some known

F# remove a certain element in an Array

≯℡__Kan透↙ 提交于 2019-12-10 17:17:09
问题 I've looked in F# array module but it seems like there is no function that could remove a certain element from an array. I was just wondering if there exists any function that does so? E.g. remove 2 [| 0 ; 1 ; 2 ; 3 ; 4 |] val it -> [| 0 ; 1 ; 3 ; 4 |] UPDATE Array filter is what I'm looking for. In addition to that, just a bit more specific with my case though. If the array I have is not a normal type array but an array of a-specific-class's references. Assuming that I want to remove only