Skip to content

Instantly share code, notes, and snippets.

View ruxo's full-sized avatar

Ruxo ruxo

  • Bangkok
View GitHub Profile
@ruxo
ruxo / StrRef.fs
Last active February 9, 2023 23:14
F# String Reference
type StrRef =
{ str: string
start: int
length: int }
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module StrRef =
let substring n s = { s with length=(min s.length n) }
let lastPos sr = sr.start + sr.length - 1
let iterate sr =
@ruxo
ruxo / fp.fs
Last active February 9, 2023 23:14
F# Fundamental libraries Compatibility break (since v9): Option and Result extensions are moved to RZ.Fsharp.Extension lib.
// v9
module TiraxTech.Foundation
let inline sideEffect ([<InlineIfLambda>] f) x = (f x); x
let inline flip f a b = f b a
let inline constant x = fun _ -> x
let inline cast<'t> (x :obj) = x :?> 't
let inline tryCast<'a> (x:obj) =
@ruxo
ruxo / regremover.fsx
Last active February 9, 2023 23:18
Remove garbage from registry
open System
open Microsoft.Win32
let inline constant x = fun _ -> x
type RegistryEntry =
| Key of RegistryKey * string list
| Value of RegistryKey * string list * obj
let inline tryCast<'a> (x:obj) =
@ruxo
ruxo / HTTP.fsx
Last active February 9, 2023 23:18
HTTP !!!
#r "System.Net.Http.dll"
#r "packages/Newtonsoft.Json/lib/net45/Newtonsoft.Json.dll"
#load "paket-files/ruxo/a9244a6dfe5e73337261/common.fs"
#load "paket-files/ruxo/5e1e84865bafd17e3a34/pair.fs"
open System
open System.Net
open RZ
open RZ.Foundation
@ruxo
ruxo / fuzzy.fsx
Last active February 9, 2023 23:18
Fuzzy logic
type CategoryFunc = float -> float
type Category = string * CategoryFunc
module private CategoryRandom =
let r = System.Random()
let from n = r.Next n
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Category =
@ruxo
ruxo / traverse.fsx
Last active February 9, 2023 23:17
type TreeNode<'a> = 'a * 'a seq
let rec traverse (prefix: 'a list) (lookup: 'a -> TreeNode option) (x:'a, subnodes: 'a seq) :'a list seq =
seq {
let routes = x::prefix
yield routes
let nextScan = subnodes |> Seq.filter (fun x' -> not (routes |> List.contains x'))
for n in nextScan do
match lookup n with
@ruxo
ruxo / combinationPair.fsx
Last active February 9, 2023 23:17
Array element combination
let combinationPair array =
seq {
let L = Array.length array
for i = 0 to L-2 do
let first = array.[i]
for j = i+1 to L-1 do
yield first, array.[j]
}
let combinationDoubleSeq a1 a2 = a1 |> Seq.collect (fun x -> a2 |> Seq.map (fun y -> x,y))
@ruxo
ruxo / rectangle.fs
Last active February 9, 2023 23:13
F# Geometry types
type Rect =
{ Left:int; Top:int; Width:int; Height:int }
with
static member create(w, h) = { Left=0; Top=0; Width=w; Height=h }
static member create(l,t,r,b) = { Left=l; Top=t; Width=(r-l)+1; Height=(b-t)+1 }
static member shrink n r = { Left=r.Left+n; Top=r.Top+n; Width=r.Width-2*n; Height=r.Height-2*n }
static member scan r =
seq {
for y = r.Top to r.Top+r.Height-1 do
for x = r.Left to r.Left+r.Width-1 do
@ruxo
ruxo / memoize.rb
Last active October 2, 2021 02:16
Ruby Memoize
def memoize(method_name)
cache = {}
define_method(method_name) do |*args|
cache[args] ||= yield(*args)
end
end
memoize(:add) {|a,b|
p "calc #{a} and #{b}"
a+b
@ruxo
ruxo / fp.rb
Last active May 18, 2016 04:23
Ruby: IPC Task Scheduler
module FP
class Either
def initialize(data)
@data = data
end
def get
@data
end
end
class Right < Either