Skip to content

Instantly share code, notes, and snippets.

View yodiz's full-sized avatar

Mikael Kjellqvist yodiz

  • Yodiz AB
  • Sweden
View GitHub Profile
let a =
if true
then
1
else
2
|> (fun x -> sprintf "%i" x)
let (|>>) a b = a b
#r "nuget:Fenix.Dry"
module Translate =
open Fenix.Dry.Http
open Fenix.Dry.Http.HttpResponse
let translate q =
let body =
System.Text.Json.JsonSerializer.Serialize({| text = [|q|]; target_lang = "EN-US"; source_lang = "NB" |})
let resp =
@yodiz
yodiz / lcs.fs
Created December 5, 2023 13:09
longest common substring f#
//Skapa griden, det är en tabell med sträng 1 på x och sträng 2 på y
let longest_common_substring (word_a:string) (word_b:string) =
let cell = Array.init word_a.Length (fun _ -> Array.init word_b.Length (fun _ -> 0))
for i = 0 to word_a.Length - 1 do
for j = 0 to word_b.Length - 1 do
if word_a[i] = word_b[j] then
if i > 0 && j > 0 then
cell[i][j] <- cell[i-1][j-1] + 1
open System
let inline middle a b = min a b + (abs (a - b) / (LanguagePrimitives.GenericOne + LanguagePrimitives.GenericOne))
type Point = { x:int; y:int }
module Point =
let create x y ={ x = x; y = y }
let middle p1 p2 = create (middle p1.x p2.x) (middle p1.y p2.y)
let a = 2.0**2.0
let pow n p = System.Math.Pow(n, p)
let private mapRequest (context : Microsoft.AspNetCore.Http.HttpContext) : Svea.Dry.Http.HttpRequest =
if context.Request.HasFormContentType then
let newStream : System.IO.Stream =
let ms = new System.IO.MemoryStream()
use sw = new System.IO.StreamWriter(ms, System.Text.Encoding.UTF8, 4096, true)
let mutable isFirst = true
for item in context.Request.Form do
sw.Write(sprintf "%s=%s" (System.Web.HttpUtility.UrlEncode(item.Key)) (item.Value |> Seq.map (fun x -> System.Web.HttpUtility.UrlEncode(x)) |> String.concat ","))
if isFirst then
//LibA.dll 1.0
module LibA =
let helloWorld name = sprintf "Hello there %s" name
//LibB.dll
module LibB =
let helloThere name = sprintf "(Through LibB) %s" (LibA.helloWorld name)
//App
LibB.helloThere "Micke"
type Transaction<'a> = string -> 'a
module Transaction =
let map<'a,'b> (mapper:'a -> 'b) (t:Transaction<'a>) : Transaction<'b> =
(fun x -> mapper (t x))
let bind<'a,'b> (binder:'a -> Transaction<'b>) (t:Transaction<'a>) : Transaction<'b> =
(fun x -> binder (t x) x)
let execute connectionString (t:Transaction<'a>) =
t ConnectionString
type SqlReader<'state, 'element> =
type Rec = { A : int; B : string }
let customServiceSchema : JsonSchemaType<Rec> =
recordSchema<Rec> "My Record" ""
[|
qouteField "id" "" SchemaJSON.int32 <@ fun x -> x.A @>
qouteField "status" "" SchemaJSON.string <@ fun x -> x.B @>
|]
type CircularBuffer<'b>(bufferSize) =
let buffer : 'b option array = Array.init bufferSize (fun _ -> None)
let mutable currentIndex = bufferSize - 1
let lockObj = System.Object()
member x.Add item =
lock (lockObj)
(fun () ->
let newIndex =
@yodiz
yodiz / time_fold.fs
Created May 16, 2019 20:06
Timing of fold-functions in F#
let inline fold f a (xs: _ []) =
let mutable a = a
for i=0 to xs.Length-1 do
a <- f a xs.[i]
a
let afold f a (xs: _ []) =
let mutable a = a
for i=0 to xs.Length-1 do
a <- f a xs.[i]