Code Snippets F#

Short texts F# 🧑‍💻 Code snippets F# ✍️ Reading lists F# 👀

runtime.fs

module Sdkgen.Runtime open System open Microsoft.AspNetCore open Microsoft.AspNetCore.Builder open Microsoft.AspNetCore.Hosting open Microsoft.AspNetCore.Http open Giraffe open FSharp.Control.Tasks open System.Text.Json open System.Collections.Generic open System.Threading.Tasks open Sdkgen.Helpers open System.Diagnostics open Microsoft.Extensions.Primitives open System.Net open System.Buffers open System.Text open System.IO open System.Text.Json.Serialization let options = JsonSerializerOptions() options.Converters.Add(JsonFSharpConverter()) let randomBytes bytes = let rnd (x: byte array) = Random().NextBytes x […]

Record mic in F# using OpenTK

open System open System.Threading open OpenTK.Audio.OpenAL //based on //https://github.com/opentk/opentk/blob/4.0.0-pre9.7/src/OpenAL/OpenTK.OpenAL/ALTest.cs [] let main argv = let frequency = 44100 // list devices let devices = ALC.GetStringList GetEnumerationStringList.DeviceSpecifier devices |> String.concat “,” |> printfn “Devices:\n %s” //default device let deviceName = ALC.GetString (ALDevice.Null, AlcGetString.DefaultDeviceSpecifier) let device = ALC.OpenDevice(deviceName) let context = ALC.CreateContext(device, Unchecked.defaultof) ALC.MakeContextCurrent(context) |> ignore // […]

silk.net

#nowarn “9” open System open System.Diagnostics open System.Drawing open System.Globalization open System.Numerics open Silk.NET.Input open Silk.NET.Maths open Silk.NET.OpenGL open Silk.NET.Windowing open Microsoft.FSharp.NativeInterop type [] GlVertexArray = GlVertexArray of uint32 let vertexShaderSource = “””#version 300 es precision highp float; #define PI 3.141592654 #define TAU (2.0*PI) uniform vec2 scale ; uniform float time ; in vec4 a_position […]

5g1.fsx

/// 5g1 /// (a) let transposeArr (inputArr : ‘a [,]) = let r1 = Array2D.length1 inputArr let c1 = Array2D.length2 inputArr let transposedArr = Array2D.init c1 r1 (fun r2 c2 -> inputArr.[c2, r2]) transposedArr printfn “(a) %A\n” (transposeArr (Array2D.init 3 5 (fun r c -> (r+1) * (c+1)))) /// (b) /// One unit: tranposeArr /// […]

Broken fsharp project

Exe net5.0 3390;$(WarnOn) // Learn more about F# at http://docs.microsoft.com/dotnet/fsharp open System // Define a function to construct a message to print let from whom = sprintf “from %s” whom [] let main argv = let message = from “F#” // Call the function printfn “Hello world %s” message Console.Write(“sssss”) 0 // return an integer […]