Skip to content

Instantly share code, notes, and snippets.

View kos59125's full-sized avatar

Abe Kosei kos59125

  • Connecto Data Inc.
  • Tokyo, Japan
  • X @kos59125
View GitHub Profile
@kos59125
kos59125 / marshal.fs
Created November 26, 2012 02:24
Tried to make a type of C union, but failed
open System
open System.Runtime.InteropServices
(* C code
union S {
uint32_t u32[4];
uint64_t u64[2];
};
*)
@kos59125
kos59125 / terminal.fs
Created December 3, 2012 03:50
F# terminal in the language
open System
open System.IO
type Terminal<'a> =
| Terminal of string * option<'a>
type CommandResult<'a> =
| Success of 'a
| Failure of exn
@kos59125
kos59125 / BinarySearchTree.fs
Created January 9, 2013 08:45
Weighted random sampling without replacement in F#. Related Project: RecycleBin.Random https://github.com/kos59125/RecycleBin.Random
module BinarySearchTree
type Tree<'a> =
| Empty
| Node of 'a * Tree<'a> * Tree<'a>
let empty<'a> = Tree<'a>.Empty
let singleton key value = Tree.Node ((key, value), empty, empty)
@kos59125
kos59125 / HttpStatus.fs
Last active December 14, 2015 00:59 — forked from Gab-km/HttpStatus.fs
type HttpStatus =
{ Code: string; Message: string }
override self.ToString () = sprintf "%s %s" self.Code self.Message
let httpStatus code message = { Code = code; Message = message }
let httpStatusList =[
httpStatus "100" "Continue";
httpStatus "101" "Switching Protocols";
httpStatus "102" "Processing";
open System
type Foo = { Field : int }
let random = Random ()
let hasDuplicate keySelector =
let adjacent = Seq.windowed 2 >> Seq.map (function | [| x; y |] -> x, y | _ -> failwith "invalid")
Seq.scan (fun set value -> Set.add (keySelector value) set) Set.empty
>> adjacent
>> Seq.exists (fun (x, y) -> Set.count x = Set.count y)
@kos59125
kos59125 / default.beamer
Last active December 17, 2015 18:18
pandoc の beamer テンプレート
\documentclass[$if(fontsize)$$fontsize$,$endif$$if(handout)$handout,$endif$$if(beamer)$ignorenonframetext,$endif$]{$documentclass$}
$if(theme)$
\usetheme{$theme$}
$endif$
$if(colortheme)$
\usecolortheme{$colortheme$}
$endif$
\usepackage{amssymb,amsmath}
\renewcommand\mathfamilydefault{\rmdefault}
\usepackage{ifxetex,ifluatex}
@kos59125
kos59125 / naturalSort.R
Last active December 17, 2015 23:49
Natural sort in R language.
moved to https://github.com/kos59125/naturalsort
@kos59125
kos59125 / cast.fs
Created July 11, 2013 11:50
Type casting IEnumerator to generic IEnumerator<>
open System
open System.Collections
open System.Collections.Generic
let cast<'a> : IEnumerator -> IEnumerator<'a> = function
| :? IEnumerator<'a> as e -> e
| e ->
{
new IEnumerator<'a> with
member __.Current = e.Current :?> 'a
@kos59125
kos59125 / takeBy.fs
Last active December 21, 2015 15:19
Seq.takeBy
module Seq =
let takeBy n (source:seq<'a>) = seq {
use e = source.GetEnumerator ()
while e.MoveNext () do
yield e.Current
let skip = ref (n - 1)
while !skip > 0 && e.MoveNext () do
decr skip
}
@kos59125
kos59125 / stan_data.R
Last active December 26, 2015 04:29
Saves R's data in a file and Loads data (*.data.R) into R. See http://blog.recyclebin.jp/archives/4052 (in Japanese/日本語) for details.
save_stan_data <- function(data, file, na.replace=NA) {
cat(file=file)
write <- function(...) {
cat(..., file=file, append=TRUE)
}
variables <- if (is.environment(data)) {
ls(envir=data)
} else {
names(data)
}