Skip to content

Instantly share code, notes, and snippets.

View kos59125's full-sized avatar

ABE Kosei kos59125

View GitHub Profile
#nowarn "9"
open System.Runtime.InteropServices
[<Struct>]
type T =
[<DefaultValue>]
val mutable underlyingValue : uint32
[<Struct; StructLayout(LayoutKind.Explicit)>]
@kos59125
kos59125 / 4216.fsx
Last active August 29, 2015 13:56
「MCMC のデバッグ」 http://blog.recyclebin.jp/archives/4216
(*
"Debugging MCMC" http://blog.recyclebin.jp/archives/4216
Usage
------
nuget install -x FsRandom
fsi 4216.fsx
fsi --define:DEBUG 4216.fsx
*)
@kos59125
kos59125 / ComputationExpressionPatterns.fs
Created March 11, 2014 01:25
Active patterns for computation expressions
module Quotations.ComputationExpressionPatterns
open Microsoft.FSharp.Quotations.Patterns
let (|MethodNamedAs|_|) name = function
| Call (Some (builder), mi, args) when mi.Name = name -> Some (builder, mi, args)
| _ -> None
let (|Bind|_|) = function
| MethodNamedAs "Bind" (builder, methodInfo, [m; f]) -> Some (builder, methodInfo, m, f)
| _ -> None
@kos59125
kos59125 / stap.fs
Created June 8, 2014 09:07
FsRandom で陽性かくにん!
open FsRandom
let randomCell =
String.randomAlphabet 4
|> Random.map (fun s -> s.ToUpper ())
Seq.unfold (function
| _, true -> None
| s, _ ->
match Random.next randomCell s with
open FsRandom
let search goal state =
// 入力から文字種を得る
let goalSize = String.length goal
let input = Seq.distinct goal |> Seq.toArray
let inputSize = Array.length input
use e =
Utility.chooseOne inputSize
|> Random.map (fun index -> input.[index])
@kos59125
kos59125 / TestBuilder.fs
Last active August 29, 2015 14:07
テストコンピュテーション式 (https://gist.github.com/Gab-km/86dd730a8d8e407a699f 参考)
type TestResult =
| Success
| Failure of string
type TestState<'a, 'b, 'c> =
| NotRun
| Context of 'a
| Action of 'b
| Response of 'c
| Result of TestResult
open FsRandom
let sampleOne array = random {
let! sample = Array.sample 1 array
return sample.[0]
}
let zoi =
[[|"今日"; "ぞい"|]; [|"も"|]; [|"1"; "ぞい"|]; [|"日"; "ぞい"|]; [|"がん"; "ぞい"|]; [|"ばる"; "ぞい"|]; [|"ぞい!"|]]
|> List.map sampleOne
library(magrittr)
takewhile <- function(zoi, count=0L) {
count <- count + 1L
if (eval(zoi)) {
return(count)
} else {
return(takewhile(zoi, count))
}
}
@kos59125
kos59125 / calc_pi.R
Last active August 29, 2015 14:12
Monte Carlo pi in R when the number of iteration is too large
calc_pi <- function(n, k) {
stopifnot(n %% k == 0)
r <- n %/% k
s <- 0
for (loop in seq_len(r)) {
x <- runif(k)
y <- runif(k)
s <- s + sum(x * x + y * y <= 1)
}
4 * s / n
@kos59125
kos59125 / _
Last active August 29, 2015 14:13
R as a Shell
R Package is created: see https://github.com/kos59125/RaaS