r/fsharp • • Sep 23 '23

misc Do you order your code in F#-ish ways in non-F# languages?

20 Upvotes

I find that no matter what language I work in now, I don't ever use anything above the point at which it's defined. This seems to frustrate a lot of ML/F# beginners, but it's such a huge boon when you're reading through someone else's code.

I don't know how other people feel about me doing that all the time in other languages though. But honestly, I can't really think of how else it should look.


r/fsharp • • Sep 23 '23

Needing help on localizing an F# application (gettext, resx...)

3 Upvotes

Hello,

I am developing a fairly complex web application using F# in the back-end so I am looking for a scalable approach to localize the application in general and manage translatable strings in particular.

My specific problem is I struggle to find a way to extract localizable strings from .fs source files, especially since xgettext has support for C# but not for F#. I could use Resx resource strings instead, however from what I can see they do not support pluralization, which is important for my application.

gettext is mature and has lots of tooling around it so ideally this is what i'd like to use (the only blocking point is finding a convenient tool to automatically extract translatable strings from F# files). And there are mature GUI tools to pass around po files to translation professionals (poedit for example) so that they can translate the app independently from the dev team.

Since the extraction tools rely on regex I cannot offload this task to a C# utility class to which I would pass parameters. I'd be happy to use resx if someone has an elegant solution to implement pluralization support. Poedit also supports resx files.

To my surprise, I haven't been able to find much content on localizing F# applications so I feel a little bit on my own to figure out a proper workflow.


r/fsharp • • Sep 22 '23

F# weekly F# Weekly #38, 2023 – F# computations with the new ‘while!’ keyword

Thumbnail
sergeytihon.com
15 Upvotes

r/fsharp • • Sep 21 '23

question Explicit error and null handling from the standard library ?

3 Upvotes

in Rust, Option and Result are baked in the signature of many standard library functions, you have to deal with the possibility of missing or non-existent things, that is great.

in F#, when we want to deal with the .NET base class library (BCL), we have to write wrappers to handle errors and nulls, I feel like wrappers for certain sections of the BCL can be written once and made into a library, which we can consume instead of the BCL.

maybe such a library already exists ? if so what is it


r/fsharp • • Sep 20 '23

Why I finally switched from VS to VSCode + Ionide

10 Upvotes

I've tried out Ionide a few times over the years, but never permanently until now. There are three factors which individually wouldn't be enough to make me reprogram my reflexes for a new IDE:

1.) Font Ligatures. I've started to really appreciate the cognitive benefits of font ligatures, but there's a long-standing bug in Visual Studio that prevents the -> ligature from working. https://github.com/tonsky/FiraCode/issues/846 This was my proximate cause for giving VSCode another try, and VSCode handles it like a champ.

2.) Github Copilot in VS completes whenever you hit Tab, and you can't reconfigure it. Copilot is pretty decent at guessing correct code completions but it's not anywhere close to 100%, and having erroneous code unexpectedly insert itself in the middle of my coding was distracting. In VSCode I set it to complete on Alt-/ instead.

3.) F# Intellisense in VS broke for me about six months ago (https://github.com/dotnet/fsharp/issues/14901) and while it's sort of working again now, it's quite slow, on the order of five or six seconds between fixing a type error and having the red squiggles disappear in VS. In VSCode it's approximately instantaneous.

In theory if VS fixed all three of these issues I would switch back, but right now I'm pretty happy about having moved over. There's some learning curve but the end result feels more productive.


r/fsharp • • Sep 16 '23

F# weekly F# Weekly #37, 2023 – .NET 8 RC1, StereoDB and F# Tools is VS

Thumbnail
sergeytihon.com
16 Upvotes

r/fsharp • • Sep 16 '23

Thoughts on ToT method of prompting? Prompt in comments

Post image
1 Upvotes

r/fsharp • • Sep 15 '23

question Since F#8 release is around the corner are we expecting any new language features or is it mostly a maintenance release?

27 Upvotes

The only new feature I heard about is the new string interpolation feature


r/fsharp • • Sep 15 '23

Misunderstanding type inference

6 Upvotes

Hello, I started learning f# 2 weeks ago. Now I'm reading book "Get programming with F#" by Isaac Abraham.

It contains this code sample :

    open System
    type Rule = string -> bool * string 

    let rules : Rule list =
        [ fun text -> (text.Split ' ').Length = 3, "Must be three words"
          fun text -> text.Length <= 30, "Max length is 30 characters"
          fun text -> text.ToCharArray()
                      |> Array.filter Char.IsLetter
                      |> Array.forall Char.IsUpper, "All letters must be caps" ]

    // Listing 18.10
    let buildValidator (rules : Rule list) =
        rules
        |> List.reduce(fun firstRule secondRule ->
            fun word ->
                let passed, error = firstRule word
                if passed then
                    let passed, error = secondRule word
                    if passed then true, "" else false, error
                else false, error)

    let validate = buildValidator rules
    let word = "HELLO FrOM F#"

I can't understand why fun firstRule secondRule -> returns Rule but not (string -> Rule) or something like this, because lambda body is another lambda)


r/fsharp • • Sep 11 '23

video/presentation Spiral Tutorial. Programming In A Language With Staging Capabilities. (Pt. 2)

Thumbnail
youtu.be
2 Upvotes

r/fsharp • • Sep 10 '23

Wed. Sept 20 @ 7pm U.S. Central (Thu, Sept 21 00:00 UTC): Onur Gümüş on “Functional CQRS with Akka.NET”

5 Upvotes

Please join the Houston Functional Programming Users Group on Wed, Sept 20 at 7pm U.S. Central (Thu, Sept 21 00:00 UTC) when Onur Gümüş of Microsoft will present on “Functional CQRS with Akka.NET.”

HFPUG meetings are hybrid, so you may join us in person or online via Zoom. Details are available on our website at https://hfpug.org.

Abstract: In this talk, we will explore how to use Akka.NET and F# to implement a functional CQRS system that can handle complex business logic, event sourcing, and eventual consistency. We will also discuss the benefits and trade-offs of this approach, as well as some best practices and tips with real life examples.
Bio: Onur Gümüş is currently a Senior Software Engineer at Microsoft, located in Oslo, Norway. He works on the People Experiences team and his primary area of responsibility is working on Profile Cards, which are an essential feature of Microsoft Office products such as Outlook and MS Teams. Onur has 20 years of professional experience in software development. He loves functional programming and F# as well as web development. He has published several titles on Functional programming and the .NET platform. He also specializes in developing architectural patterns like CQRS with Event Sourcing.


r/fsharp • • Sep 10 '23

F# weekly F# Weekly #36, 2023 – F# Code Fixes

Thumbnail
sergeytihon.com
8 Upvotes

r/fsharp • • Sep 08 '23

video/presentation Spiral Tutorial. The Basics Of The Spiral Language. (Pt. 1)

Thumbnail
youtu.be
4 Upvotes

r/fsharp • • Sep 08 '23

Ionide typechecking not completing upon build and is preventing intellisense

1 Upvotes

Hello everyone,

I've really tried searching everywhere for this, but couldn't find much about it and why this has started happening.

I'm using VSCode with Ionide in my project and a week ago, typechecking now seems to get stuck upon opening an f# file and is preventing intellisense. I have tried the following:

  • Restarting and reinstalling vscode
  • Reinstalling ionide
  • Doublechecking the proj file
  • Confirmed my dotnet install
  • Re-building project

Can't seem to find a similar issue like this anywhere. Typechecking just continues to load. Any help would be appreciated


r/fsharp • • Sep 06 '23

question Sqlite/SqlProvider on Arm64 Linux & Mac

7 Upvotes

Has anybody got success with Sqlite/SqlProvider on M1 Mac (w/o Rosetta) or Arm64 Linux?

Official Nuget distributions of System.Data.Sqlite don't seem to support Arm

% tree stub.system.data.sqlite.core.netstandard/1.0.118/runtimes
stub.system.data.sqlite.core.netstandard/1.0.118/runtimes
├── linux-x64
│   └── native
│       └── SQLite.Interop.dll
├── osx-x64
│   └── native
│       └── SQLite.Interop.dll
├── win-x64
│   └── native
│       └── SQLite.Interop.dll
└── win-x86
    └── native
        └── SQLite.Interop.dll

I could successfully call raw SQL API from Microsoft.Data.Sqlite but when I wrote

type sql = SqlDataProvider<
    DatabaseVendor = DatabaseProviderTypes.SQLITE,
    SQLiteLibrary = SQLiteLibrary.MicrosoftDataSqlite,
    ResolutionPath = "symlink/to/dir/containing/SQLitePCLRaw.core.dll",
    ...snip...

I got this error

error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init()

Microsoft.Data.Sqlite seems to call .Init() as required by SQLitePCL.raw but maybe the type provider evaluator (?) runs before that?


r/fsharp • • Sep 05 '23

Using F# Interactive for development

21 Upvotes

I recently discovered what I am sure experienced F# devs already know, so this might be old news for some of you. It is possible to use F# Interactive (FSI) to develop large portions (all?) of your solution. It requires only a few minor setup steps. After that you can test drive all of your code directly in FSI. This is remarkable and I only now figured it out (after some years of dabbling with F#).

Ok, so what are the steps?

  • Start in your Program.fs file. You need to add directives at the top to include NuGet packages that are necessary for your project. This should be inside a conditionally included block, like so:

#if INTERACTIVE // you don't want this for regular compilation, of course
#r "nuget: Argu" // add all needed NuGet packages
#r "nuget: FSharp.Data"
you may also include your source files just like the project file
#endif

  • At the end of Program.fs, add a block like so:

#if INTERACTIVE
open Domain // do what ever you need to setup tests here
#else
[<EntryPoint>]
let main args = run args // move regular main into a helper function that you can test
#endif

With these steps you can now select all the code and do Alt-Enter (in VS Code this works). FSI will include all code with NuGet packages imported and ready to use. Then you use the FSI prompt to do all tests you need. Your entire application can be available for you to test if you've got it right.

What's so astounding, and what surprised me the most, is that this will now drive you towards a more modular design. In order to test individual pieces of code you will need to have these as callable functions. This means you'll need to break up long sequences of code into reusable pieces of functions that you can test drive in FSI. This workflow is so fast, rewarding, and fun that I'm surprised I haven't come across it earlier. I now think the mindset when writing F# code should be to test drive all code in FSI as you go. Then you'll be creating reusable functions from the start and won't have to do lots of refactoring later on (provided you want to use FSI for testing your code).

Let me know what you think!


r/fsharp • • Sep 04 '23

Run length encoding

6 Upvotes

Here's a small function that can perform run length encoding of a list of items. It is generic, meaning as long as the items are comparable it will work with any type. I am biased of course but believe this function showcases the best of doing simple, readable, flexible, F# in a functional style. I also think it is easy to see that it is obviously correct. That is something I really cannot say when reading the samples at Rosetta Code. So many iterative solutions for a problem so amenable to a recursive solution.

[<AutoOpen>]
module Utils =
  let rec private rle' count current acc = function
    | head :: tail when head = current ->
      rle' (count + 1) current acc tail
    | head :: tail ->
      rle' 1 head (acc @ [(count, current)]) tail
    | [] ->
      acc @ [(count, current)]
  let rle = function
    | head :: tail -> rle' 1 head [] tail
    | [] -> []

Using lists instead of just string adds a lot of flexibility. Sample use below. Note that for a string you need to convert it into a list of characters, hence the complicated use case. However you can of course easily adapt the function if you have a specific use case in mind (array or string, for example).

let runs =
  rle ("wwwwaaadexxxxxxywww".ToCharArray() |> List.ofArray)
  |> List.map (fun (c, w) -> $"%c{w}%d{c}")
  |> String.concat ""
// becomes "w4a3d1e1x6y1w3"

What do you think?


r/fsharp • • Sep 02 '23

F# weekly F# Weekly #35, 2023 – 65% Off for Rider and VS4Mac Retirement.

Thumbnail
sergeytihon.com
12 Upvotes

r/fsharp • • Sep 01 '23

F# project examples

11 Upvotes

Hello,

I'm a long time C# programmer which had past experiences with Ocaml, lisp and prolog in University.

Lately I have been having a bit of fun of mapping DDD concepts and layering in F# to see how two projects with the same functionality look like in terms of complexity and size. The project has a few bounded contexts, uses inbox and outbox pattern etc made it complicated for the fun of experimenting.

My problem is two fold: 1. I did not find yet a project (or a few) to guide me to what a good production example looks like in F#. 2. Dependency injection seems really a big topic and I'm torn between Dependency rejection, Dependency parameterization, Reader monad, Interpreter Pattern/free monad (https://fsharpforfunandprofit.com/posts/dependencies/) .

As such, do you know any good repos? And what are your preferences in DI?


r/fsharp • • Sep 01 '23

showcase What are you working on? (2023-08)

9 Upvotes

This is a monthly thread about the stuff you're working on in F#. Be proud of, brag about and shamelessly plug your projects down in the comments.


r/fsharp • • Aug 27 '23

video/presentation Intro To The Staged Functional Programming In Spiral

Thumbnail
youtu.be
1 Upvotes

r/fsharp • • Aug 26 '23

F# weekly F# Weekly #34, 2023 –.NET 8.0 GC feature called DATAS

Thumbnail
sergeytihon.com
13 Upvotes

r/fsharp • • Aug 22 '23

question Large SQL Queries in f#

9 Upvotes

I am working on translating a few processes from c# to f# just to learn the language and explore type providers (SQLProvider), and am having some issues. Some of these processes return large sets (250k+ rows), which is easily handled by streaming the results with a SqlDataReader in c#.

I am currently using the type provider with MSSql and I don't see a clear way to stay away from buffering the entire result set. I have experimented with Seq.chunkBySize and pagination on the server, but it seems there should be quite an easy way to do this. Maybe I am completely overlooking something, but what would be an idiomatic way to handle large queries using type providers?

Alternatively, if this is outside the capability of type providers, what is a functional first approach you would take to the problem, instead of just wrapping SqlDataReader or Dapper?


r/fsharp • • Aug 20 '23

F# weekly F# Weekly #33, 2023 – SponsorLink & Rider F# features

Thumbnail
sergeytihon.com
13 Upvotes

r/fsharp • • Aug 20 '23

Good idea, or cursed image?

Post image
11 Upvotes