Skip to content

Instantly share code, notes, and snippets.

@adacola
adacola / kuso.fsx
Last active July 5, 2018 03:00
クソコードであると表明するだけのコンピュテーション式。これ自体がクソコード
open System
type KusoBuilder() =
member __.Bind(x, f) = f x
member __.Return(x) = x
member __.ReturnFrom(x) = x
member __.Using(x: #IDisposable, f) = try f x finally if x |> box |> isNull |> not then x.Dispose()
member __.Zero() = ()
member __.Combine(_, f) = f()
member __.TryWith(f, g) = try f() with e -> g e
@adacola
adacola / totient.fsx
Created October 24, 2014 06:20
Euler's totient function in F#
let totient n =
let rec totient isFirst (r, n) d =
match n % d with
| 0 -> totient false (r * (d - (if isFirst then 1 else 0)), n / d) d
| _ -> r, n
let nearlyPrimes = seq { yield 2; for i in 3 .. 2 .. n do yield i }
((1, n), nearlyPrimes) ||> Seq.fold (totient true) |> fst
let totientTest() =
let expected = [1; 1; 2; 2; 4; 2; 6; 4; 6; 4]
@adacola
adacola / twiplaToTsv.fsx
Created April 12, 2017 13:32
twiplaの参加者一覧のTSVファイルを作成します
(*
twiplaの参加者一覧のTSVファイルを作成します。
使用方法
twiplaの印刷用ページを表示してページ全体をクリップボードにコピーし、その内容を文字列にしてtwiplaToTsvの引数に渡します。
*)
open System
let twiplaToTsv (content : string) =
let factorialNumberSystem n =
let n = n - 1
let factorials =
if n = 0 then [1]
else
Seq.initInfinite ((+) 2) |> Seq.scan (*) 1 |> Seq.takeWhile ((>=) n)
|> Seq.toList |> List.rev
let factoradic k =
@adacola
adacola / kiyoshi.fsx
Created March 12, 2016 03:45
ズンズンズンズンドコが並んだらキ・ヨ・シ!と表示
let r = System.Random()
let expected = [|0; 0; 0; 0; 1|]
let kiyoshi() =
Seq.initInfinite (fun _ -> r.Next 2) |> Seq.windowed 5
|> Seq.takeWhile ((<>) expected)
|> Seq.map Array.head
|> Seq.append <| expected
|> Seq.iter (Array.get [|"ズン"; "ドコ"|] >> printf "%s")
printfn "キ・ヨ・シ!"
@adacola
adacola / UnitsOfMeasure.fs
Last active December 31, 2015 22:59
F# Advent Calendar 21日目の記事で使用したコードです。 http://qiita.com/adacola/items/b65752b678e81bc8e354
// 単位の定義
[<Measure>] type m
[<Measure>] type s
[<Measure>] type kg
[<Measure>] type 歳
// 単位の別名
[<Measure>] type meter = m
// 組立単位は単位を組み合わせて定義します。単位 N は kg * m / (s * s) の別名扱いです。
[<Measure>] type N = kg * m / (s * s)
@adacola
adacola / force.fsx
Last active December 19, 2015 13:33
The Lazy.Force Awakens
let lazy1 = lazy (printfn "hoge"; 1)
// val lazy1 : Lazy<int> = 値は作成されていません。
// The Force Awakens
lazy1.Force()
// hoge
// val it : int = 1
lazy1.Force()
// val it : int = 1
open FSharp.Control.Reactive
open FSharp.Control.Reactive.Observable
open System
module FizzBuzzBase =
/// 100ミリ秒ごとに1, 2, 3, ...を発行
let numberObservable =
TimeSpan.FromMilliseconds 100.0 |> interval |> map ((+) 1L)
|> publish
@adacola
adacola / fib.fsx
Created September 9, 2015 18:27
FP in Scala EXERCISE2-1 in F#
/// 素朴なフィボナッチ数列の実装
let rec fib = function
| 0 -> 0
| 1 -> 1
| n -> fib (n - 1) + fib (n - 2)
/// 素朴なフィボナッチ数列の実装(メモ化)
let fibMemo =
let memo = System.Collections.Generic.Dictionary(dict [0, 0; 1, 1])
let rec fib n =
@adacola
adacola / zoi.fsx
Last active August 29, 2015 14:11
今日も1日がんばるぞい! http://qiita.com/giiko_/items/8c3442e8e7a83cc0b91a のネタをF#で
type Word = TrueZoi of string | FalseZoi | TrueOtherwise of string
let isTrue = function TrueZoi _ | TrueOtherwise _ -> true | FalseZoi -> false
let toString = function TrueZoi x | TrueOtherwise x -> x | FalseZoi -> "ぞい"
let countZoi = function TrueZoi _ | FalseZoi -> 1 | TrueOtherwise _ -> 0
let dic =
[
[|TrueOtherwise "今日"; FalseZoi|]
[|TrueOtherwise "も"|]
[|TrueOtherwise "1"; FalseZoi|]