Skip to content

Instantly share code, notes, and snippets.

@vietnt
vietnt / example.fs
Last active December 29, 2020 21:54
unsafe in f#
[<Struct;StructLayout(LayoutKind.Explicit, Pack=1,Size=32)>]
type Bucket =
struct
[<FieldOffset(0)>]val mutable Free : int64
[<FieldOffset(8)>]val mutable FreeCount: int
[<FieldOffset(12)>]val mutable Used: int
[<FieldOffset(16)>]val mutable Size: int
[<FieldOffset(20)>]val mutable Full: bool
end
let isPrime (n: int) =
let max = sqrt (float32 n) |> int
let rec check m =
if m > max then true
elif n % m = 0 then false
else check (m+1)
check 2
let n = 600851475143L //
let rec fib n =
match n with
| 0 -> 1
| 1 -> 2
| x -> fib (x - 1) + fib (x - 2)
Seq.initInfinite fib
|> Seq.takeWhile (fun n -> n <= 4_000_000)
|> Seq.filter (fun n -> n % 2 = 0)
|> Seq.sum
let count = cval 10
let set = cset {
let! c = count
for i = 1 to c do yield int64 i
}
let sum = set |> CSet.sumBy id
count.SetValue 10
let makeQuad a b h=
let x0, y0, z0 = a
let x1, y1, z1 = b
let c = x1, y1 + h, z1
let d = x0, y0 + h, z0
(a, b, c), (a, c, d)
let inline drawQuadTexture (x0, y0, u0, v0, c0) (x1, y1, u1, v1, c1) (x2, y2, u2, v2, c2) (x3, y3, u3, v3, c3) add vbo =
vbo |> add x0 y0 u0 v0 c0
vbo |> add x1 y1 u1 v1 c1
vbo |> add x3 y3 u3 v3 c3
vbo |> add x1 y1 u1 v1 c1
vbo |> add x2 y2 u2 v2 c2
vbo |> add x3 y3 u3 v3 c3
open System
open System.Drawing
open System.Drawing.Imaging
let perlinNoise width height persistence octaves zoom seed bias amplitudeBias handler =
let inline noise x y =
let n = x + y * 57;
let n = (n <<< 13) ^^^ n
1.0 - (float ((n * (n * n * 15731 + 789221) + 1376312589) &&& 0x7fffffff)) / 1073741824.0
//// See the 'F# Tutorial' project for more help.
//
open System
type Command =
| Block of string * TimeSpan
| Unblock of string
| IsBlocked of string
let maxTimeSpan = TimeSpan.FromDays (365*1000 |> float)
@vietnt
vietnt / rules.md
Created February 15, 2013 03:48 — forked from henrik/rules.md
  1. Your class can be no longer than 100 lines of code.
  2. Your methods can be no longer than five lines of code.
  3. You can pass no more than four parameters and you can’t just make it one big hash.
  4. When a call comes into your Rails controller, you can only instantiate one object to do whatever it is that needs to be done.

You can break these rules if you can talk your pair into agreeing with you.

@vietnt
vietnt / gist:2976707
Created June 23, 2012 03:37
transaction_specs
post.View = 0
using (var t = root.NewTransaction())
{
post.View++;
Assert.AreEqual (1, post.View);
}
Assert.AreEqual(0, post.View);