Questions tagged [f#]
F# is a succinct, expressive and efficient functional and object-oriented language for .NET which helps you write simple code to solve complex problems.
16,743
questions
3
votes
1
answer
34
views
How can you model a complex type in multiple abstract dimensions?
Background
I am trying to use the style of functional DDD laid out in "Domain Modeling Made Functional" by Scott Wlaschin. One particular point of interest to me is the concept of breaking ...
0
votes
0
answers
53
views
Basic Disposable Pattern in F#
According to
https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/dispose-pattern#basic-dispose-pattern
The Basic Disposable Pattern requires a protected virtual void Dispose(bool) ...
0
votes
2
answers
39
views
Picking a discriminated union of type from a list
I have a list of Buffs which are part of a discriminated union
type Buff =
| Block of int
| Thorns of int
let xs = [Block(5); Thorns(3)]
To get a buff from the list I do the following:
let ...
2
votes
1
answer
78
views
F# - Avoid repeating constraints
With version 7, F# allows grouping and naming of SRTP constraints to avoid repetition. The problem with constraints repetition in general remains though:
type U() = class end
type A<'B, 'C when
...
0
votes
0
answers
29
views
Where does functional composition occur in an ASP.NET app written in F#?
I'm using the built in template for an ASP.NET WebAPI type application with the F# language option as a starting point. The one with the WeatherController. The WeatherController is implemented as a ...
4
votes
1
answer
31
views
When to add a unit argument to an existing function in F#
If I am already passing other parameters into a function, do I have any reason to pass in a unit argument as well, assuming the function is non-deterministic? Do I need to worry about memoization ...
1
vote
1
answer
42
views
Early return from a loop in a task expression (CE)
I'd like to implement a retry utility for TPL-Tasks in F#, for retrying things like HttpClient.GetAsync(...).
Specification: it will retry a given task-starting function up to n times by swallowing ...
1
vote
0
answers
47
views
Silent Fake (F# Make) Console Application
I would like to run a standalone console application with Fake targets less noisy.
For example
open Fake.Core
open Fake.IO
let initializeContext () =
let execContext = Context....
0
votes
0
answers
23
views
error FS0001: This expression was expected to have type 'cexp' but here has type 'Label'
Sorry for the long code. I've been trying to make a function that takes in a string representation of a GCL program and produces a string representation of a program graph.
I can't seem to get the ...
1
vote
1
answer
45
views
How to write a type test to normalize related types?
I'm trying to inherit from an external abstract class with methods like this:
abstract member Plant: KeyValuePair<string, FlowerPot> -> bool
abstract member DigUp: KeyValuePair<string, ...
1
vote
2
answers
49
views
Can an F# query expression filter for items that are NOT in a sub-query?
I have the following sample that finds the numbers in s1 that are not in s2.
let s1 = seq { 1..3 }
let s2 = seq { 3..4 }
s1
|> Seq.filter (fun x -> s2 |> Seq.forall (fun y -> x <> y)...
0
votes
1
answer
41
views
Why does module rec result in a warning?
I am writing some JSON decoders for some types.
type FullName =
{
FirstName : string
LastName : string
}
type Person =
{
Name : FullName
Role : string
}
This code works fine:
...
0
votes
0
answers
34
views
F# TypeInitializationException
I stumbled upon a weird runtime error in my code.
The code base is rather big so it is hard to post it here.
but my problem is, I have made a search tree data structure which is used to find specific &...
0
votes
0
answers
41
views
SRTP member constraits can't see raw fields of C# classes?
I was surprised by the "The type 'Lib1.Class1' does not support the operator 'get_Id'"
error below:
F# Project Lib0.fsproj
namespace Lib0
type T0 =
val Id: string
new s={Id=s}
type T1 =
...
0
votes
0
answers
28
views
System.Net.Sockets.SocketException (10061): No connection could be made because the target machine actively refused it
I'm writing an integration test for a component that does a transaction and after that, a webhook is called.
For that, I want to mock a webhook endpoint using suave so I can know if the webhook was ...
0
votes
1
answer
53
views
ASP.NET Core JWT authorization failing with "AuthenticationScheme: Bearer was not authenticated"
I'm trying to set up JWT authentication in my F# ASP.NET Core + Fable project, and I cannot for the life of me get it to work.
Here are some snippets of the important server-side parts.
API model for ...
-1
votes
1
answer
45
views
Identify DU level when partially applied
Suppose we have a DU with 3x levels parameterised by reference types as shown:
type DU =
| X of Object
| Y of Object
| Z of Object
Noting that the GetUnionCases function within the FSharp....
0
votes
0
answers
73
views
VSCode + F# : How to make "//#region" act like C# #region
//#region appears in Intellisense, I wrapped some code:
//#region Graph
type AnnotationDataType =
| Text
| Image
| Audio
| Video
type Node =
{ NodeID: int
NodeLabel: string
...
1
vote
2
answers
40
views
F# - Determine if an argument is a function
let isFunction ``functionOrValue`` =
???
let aFunction p1 p2 ... = <code>
let aValue = 5 // or obj() or whatever type
isFunction aFunction // true
isFunction aValue // false
How to check ...
0
votes
0
answers
39
views
What library can I use to automate website tasks?
I want to automate a tasks with F# and am at a loss which library to use.
I don't want to build a website but instead I want to automate an action on a website.
I thought I could use Canopy but as I ...
0
votes
2
answers
45
views
How to subscribe to an F# event from a C# WPF usercontrol code-behind
in F#, I have the event defined as:
type ProgressNoteEvent() =
let event1 = new Event<string>()
let standardDotNetEventArgsEvent = new Event<EventHandler, EventArgs>()
...
1
vote
1
answer
71
views
SkiaSharp performance worse than Processing?
Edit: The F# code has been updated such that it only uses the Silk.NET and SkiaSharp NuGet packages. It reproduces the same lower performance.
In Processing, you can find a performance example ...
0
votes
1
answer
26
views
Reader Monad without explicit call to ask: is it possible?
I'm trying to understand how the Reader monad is used in this blog post as a mechanism to perform functional dependency injection.
The part that confuses me is this bit (taken from the blog post):
...
0
votes
1
answer
28
views
Problem with F# Interactive and Plotly (XPlot)
After installing XPlot and Plotly on VS Code on a new machine, they no longer work properly. When creating a line chart in F# Interactive, the following shows up in the terminal window:
Binding ...
0
votes
2
answers
43
views
How to do Seq.takeWhile + n items in F#
This is piggybacking on the classic question how to do Seq.takeWhile + one item in F# by extending it to the general case of extracting n items after the predicate returns false.
The obvious bare-bone ...
0
votes
2
answers
53
views
How to merge all CSV files in a directory to a single CSV file
I'm trying to merge all CSV files in a directory to a single CSV file. When I ran the code below I get the following error:
System.IO.IOException: 'The process cannot access the file 'C:\Users\james\...
4
votes
2
answers
83
views
How to "concat" two options
I'm looking through the F# docs and am wondering about a "missing" function in the Option module. There is a map2 that takes two options and seems to do a logical AND on it. If either is ...
0
votes
1
answer
70
views
F# library that subclasses a class from a C# library
I want to convert the C# classes in my console app to F#, one at a time. With stunning help from Co-pilot, I got one class done. But I can’t build with it. So I made a tiny test solution, to ...
0
votes
0
answers
20
views
fsharp fsi - sprintf function fails with System.MissingMethodException: Method not found: 'Void Microsoft.FSharp.Core.PrintfFormat`5..ctor
I'm running dotnet core 6 (6.0.406) on ubuntu. I've got various dev projects all working fine but I have this annoying issue with fsx scripts where fsharp interactive seems to have issues referencing ...
0
votes
1
answer
69
views
F# Convert string to Discriminated Union with complex args
Im trying to convert a string to a DU in F#. The DU cases have arguments that are quite complex. Is there a way to create the union case using FSharpType.GetUnionCases and FSharpValue.MakeUnion ...
0
votes
1
answer
38
views
F#: CSV cleaning
I'm trying using F# to clean a CSV dataset. For example I want to change string values in one column to lower case. I don't know if it's better to work with loaded data as CsvProvider Rows or I should ...
1
vote
1
answer
45
views
Count amount of mappings
I have the following code:
let xs = [ "1"; "1"; "-"; "1"; "1"; "1" ]
let ys = [ "0"; "0"; "-"; "1"; &...
2
votes
2
answers
48
views
Apply transformation to combine some items within a list
I would like to know how I can apply the following transformation in F#.
I find it's similar to the fold because I need the previous and current value to take my decision but fold output isn't a list ...
0
votes
1
answer
76
views
Unification of types
I am doing this exercise on unifications of types and I am having a hard time understanding if it works. This exercise is specifically being done with f# in mind.
To my understanding my goal is to ...
0
votes
1
answer
45
views
How should Task returning functions be called in the F# main method?
The default template for an F# web API project has this main method (abbreviated here):
module Program =
let exitCode = 0
[<EntryPoint>]
let main args =
let builder = ...
0
votes
1
answer
39
views
F# 'function' functions cannot inline
The following 2 functions
let episodeDict1 (a: obj) =
match a with
| :? (Episode -> _) as generator ->
let episodes = [D; W; M]
let values = episodes |> List.map ...
0
votes
0
answers
41
views
F# not understanding TCP cancellation
I have a NET 7 application, which runs as a Windows service, and starts a HostedService which instantiates one or more instances of a TCP handling class based on end user configuration. A requirement ...
1
vote
1
answer
45
views
In F#, how to take an integer defined by an Enum and Match to the same name in a discriminated union?
I'm stuck.
In F#, I have the following enum:
type PenEnum =
| InkPen =0
| FreehandPen =1
| CirclePen =2
| LinePen =3
| RectanglePen ...
0
votes
1
answer
39
views
How do I get 'T from seq<'T> from a PropertyInfo array
I'm using reflection to try and create a datatable based on a record type. This works fine for a simple record type, but some of my records have sub records (also fine, just made the function ...
1
vote
1
answer
44
views
F# `Map` algorithm
What's the algorithm used for the balanced search tree implementation behind F#'s Map type?
https://github.com/dotnet/fsharp/blob/main/src/FSharp.Core/map.fs
I had guessed it might be an AVL tree but ...
1
vote
2
answers
106
views
MSBuild: how do I detect a project's language (C# or F#) in an MSBuild variable / function?
We have a shared .props file throughout a large .NET solution (OSS project) that ships primarily C# libraries with some F# APIs built on top of them. I want to globally set the C# version through the ...
0
votes
1
answer
74
views
Run F# script in Visual Studio
I've been looking into running some F# scripts to automate some things. But I can't seem to be able to find a way to run the scripts. I've to an F# file.
Test.fsx
let helloWolrd = "Hello World&...
0
votes
0
answers
83
views
MailboxProcessor Scan memory leak
Consider the following agent which uses Scan to process every message, with a message posted every millisecond.
open System.Threading
let mp = MailboxProcessor<string>.Start (fun inbox ->
...
2
votes
1
answer
35
views
Dotnet Reflection: Calling Invoke on MethodInfo With 'out' Parameter from F#
I retrieve a TryParse method for a given type like this:
open System.Reflection
let stringParser<'T> =
typeof<'T>.GetMember("TryParse")
|> Seq.cast<MethodBase>
...
0
votes
1
answer
36
views
How can I create a CustomEvent with detail object in Fable?
In Javascript it is possible to create a CustomEvent and include a second argument containing detail data to pass when the event is dispatched. For example:
let evt = new CustomEvent("hello",...
0
votes
2
answers
47
views
Why Does F# Pattern Matching for a String Type Match a ReadOnlySpan<char>?
Let's say I wanted to find a particular TryParse method for a given type, namely the one that parses from a string. To my surprise, the following code returns 2 methods: one with a string as first ...
1
vote
1
answer
71
views
How to access F# results from C#
First of all, I am new to F#, so sorry if this question already has answers here (I've tried looking around, but couldn't find what I was looking for). With this out of the way, let's go to my issue......
1
vote
1
answer
63
views
Can an object expression refer to itself?
I'm exploring converting our C# codebase to F#. This codebase uses an external library that provides an interface; one of the methods allows the library to create new instances of our class:
public ...
0
votes
1
answer
61
views
F# - how can I convert an F# function into a System.Delegate using reflection?
Say I have a basic function, like:
let double x = x * 2
Since I know the types, I can convert it into a Func<_,_> explicitly, which can then be used as a Delegate:
let doubleFuncExplicit = Func&...
0
votes
1
answer
62
views
Async<'T> Completion Via Callback [duplicate]
Noting that the F# MailboxProcessor instance provides a PostAndAsyncReply member function.
My understanding is that this function provides both:
An AsyncReplyChannel<T> instance which provides ...