This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let inline readValue< ^T when ^T : (static member TryParse : string -> bool * ^T)> name = | |
let rec readValue() = | |
seq { | |
printf"input %s: " name | |
let str = Console.ReadLine () | |
let success, value = (^T: (static member TryParse : string -> bool * ^T) str) | |
(* TryParse <- このTryParseを型パラメータの型のスタティックメソッドとして呼び出したい *) | |
if success | |
then yield value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WpfTypeProvider | |
open System | |
open System.Reflection | |
open Samples.FSharpPreviewRelease2011.ProvidedTypes | |
open Microsoft.FSharp.Core.CompilerServices | |
open Microsoft.FSharp.Quotations | |
open Xaml | |
open System.Linq.Expressions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void Maybe関数() | |
{ | |
var safeParse = FuncUtil.Maybe<string, int>(int.Parse); | |
Assert.That(safeParse("123"), Is.EqualTo(new Option<int>(123))); | |
Assert.That(safeParse("ababa"), Is.EqualTo(Option<int>.None)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void Eitherに関数を適用_途中で失敗する関数() | |
{ | |
var safeParse = FuncUtil.Either<string, int>(int.Parse); | |
var m = (Either<Exception, string>)Either.Right("hoge"); | |
var result = from result1 in m | |
from result2 in safeParse(result1) | |
select result2; | |
Assert.That(result.Match(l => true, r => false), Is.True); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open Microsoft.FSharp.Compiler.CodeDom | |
open System.Reflection | |
open System.CodeDom.Compiler | |
type EvalResult<'a> = | |
| CompileError of CompilerError seq | |
| RuntimeError of exn | |
| Success of 'a | |
let eval args expr = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
type World<'a> = 'a [,] | |
let tryGet ar x y = | |
try Some <| Array2D.get ar x y | |
with e -> None | |
let countAlive = List.collect Option.toList >> List.filter ((=) true) >> List.length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let (|FormParams|) (stream:Stream) = | |
readText stream |> HttpUtility.ParseQueryString |> nameValueCollections2List |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ThreadLocalEvent<'T>() = | |
let handlers = new ThreadLocal<Handler<'T>>() | |
member this.Publish = this :> IDelegateEvent<Handler<'T>> | |
member this.Trigger(arg:'T) = | |
if handlers.Value <> null then | |
handlers.Value.Invoke(this, arg) | |
interface IDelegateEvent<Handler<'T>> with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type LogicalCallContextEvent<'T>() = | |
let id = Guid.NewGuid() | |
member this.Publish = this :> IDelegateEvent<Handler<'T>> | |
member this.Trigger(arg:'T) = | |
if CallContext.LogicalGetData(id.ToString()) <> null then | |
(CallContext.LogicalGetData(id.ToString()) :?> Handler<'T>).Invoke(this, arg) | |
interface IDelegateEvent<Handler<'T>> with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec move (x, y) r g n = | |
if x < 0 || y < 0 || x > g || y > g then n | |
elif (x,y) = (g, g) then n + 1 | |
else match List.tryFind ((=) (x, y)) r with | |
| Some _ -> n | |
| None -> | |
[(x + 1, y); (x, y + 1); (x - 1, y); (x, y - 1)] | |
|> List.map (fun xy -> move xy ((x, y)::r) g) | |
|> List.fold (|>) n | |
OlderNewer