Skip to content

Instantly share code, notes, and snippets.

View kos59125's full-sized avatar

ABE Kosei kos59125

View GitHub Profile
@kos59125
kos59125 / TypeUtility.fs
Created July 1, 2012 06:02
Get the type of the argument value.
module TypeUtility
[<CompiledName("SafeGetType")>]
let safeGetType<'a> (value : 'a) = typeof<'a>
@kos59125
kos59125 / NUnitAssemblyPathConvertor.fsx
Created September 1, 2012 04:45
NUnit のプロジェクトファイルは, Windows 環境の GUI で作成すると,対象アセンブリのパスのディレクトリ区切り文字がバックスラッシュになって Linux や Mac OS の環境でうまく動かないのでスラッシュに変換する。
// Usage:
// 1. Fsi.exe NUnitAssemblyPathConvertor.fsx BarTest.nunit
// 2. Get-Content FooTest.nunit | Fsi.exe NUnitAssemblyPathConvertor.fsx
#r "System.Xml.Linq.dll"
open System
open System.IO
open System.Xml.Linq
open Microsoft.FSharp.Core.LanguagePrimitives
@kos59125
kos59125 / fizzbuzz.proj
Created September 4, 2012 02:43
MSBuild で FizzBuzz
<?xml version="1.0" encoding="utf-8" ?>
<!--
To run: MSBuild /nologo /verbosity:minimal fizzbuzz.proj
-->
<Project DefaultTargets="FizzBuzz"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(Current)' == '' ">
<Current>1</Current>
@kos59125
kos59125 / repeat.fs
Created September 6, 2012 10:00
同じことを n 回繰り返す
let invoke action = action ()
let repeat n action = Seq.initInfinite (fun _ -> action) |> Seq.take n |> Seq.iter invoke
let slowPrint (delay : int) =
fun () ->
System.Threading.Thread.Sleep (delay)
System.Console.WriteLine (System.DateTime.Now)
slowPrint 500 |> repeat 120
@kos59125
kos59125 / RandomAccessList.fs
Created September 14, 2012 09:43
Purely Functional Random Access List in F# (Okasaki 1995)
module RandomAccessList
open System
let indexOutOfBounds = ArgumentOutOfRangeException ("index")
let emptyList = ArgumentException ("Empty list")
type Tree<'a> =
| Leaf of 'a
| Node of 'a * 'a Tree * 'a Tree
@kos59125
kos59125 / transform.fs
Created September 17, 2012 11:28
Convert XDocument using XSLT
let transform (xslt : string) (document : XDocument) =
let transformer = XslCompiledTransform ()
transformer.Load (xslt)
let builder = StringBuilder ()
use writer = XmlWriter.Create (builder, transformer.OutputSettings)
use reader = document.CreateReader ()
transformer.Transform (reader, writer)
builder.ToString () |> XDocument.Parse
@kos59125
kos59125 / oftype.fs
Created September 25, 2012 06:03
Enumerable.OfType in F#
let ofType<'a> (source : System.Collections.IEnumerable) : seq<'a> =
let resultType = typeof<'a>
seq {
for item in source do
match item with
| null -> ()
| _ ->
if resultType.IsAssignableFrom (item.GetType ())
then
yield (downcast item)
@kos59125
kos59125 / buffered.fs
Created September 26, 2012 01:06
Seq.windowed があるなら buffered があっても良いじゃない。
let buffered<'a> bufferSize (source : seq<'a>) =
seq {
use enumerator = source.GetEnumerator ()
let incomplete = ref true
while !incomplete do
let buffer =
seq {
let request = ref bufferSize
while !request > 0 && enumerator.MoveNext () do
yield enumerator.Current
@kos59125
kos59125 / fizzbuzz.ts
Created October 1, 2012 18:54
FizzBuzz in TypeScript
module FizzBuzz
{
var convert = function (fb : FizzBuzz, n : number) : string
{
var buffer = ""
for (var current = fb; current != null; current = current.next)
{
buffer = current.consume (n, buffer)
}
if (buffer.length == 0)
@kos59125
kos59125 / whileloop.fs
Created November 25, 2012 09:50
while ループと do-while ループ的な
let whileLoop condition f seed =
seq {
yield seed
yield! Seq.unfold (fun s -> if condition s then let next = f s in Some (next, next) else None) seed
}
|> Seq.last
let doWhileLoop condition f seed = whileLoop condition f (f seed)