| simulation library (F# version) |
|---|
|
Download source files:
この記事は 2026年2月7日 土 19:45 に作者によって編集されましたhttp://www.jkofsp.net/files/ //F# の詳細 (http://fsharp.net) namespace Simulation open System open System.Collections.Generic type ICached = interface abstract Flush : unit -> unit end module ManagerCached = let Cacheds = new LinkedList<ICached>() let Add (cached : ICached) = ignore (Cacheds.AddLast cached) let Remove (cached : ICached) = ignore (Cacheds.Remove cached) let Flush () = for i in Cacheds do i.Flush() type Cached<'T> = class interface ICached with member x.Flush () = x.Calculated <- false end val mutable FuncValue : unit -> 'T [<DefaultValue>] val mutable Value : 'T val mutable Calculated : bool new (funcValue : unit -> 'T) as x = { FuncValue = funcValue; Calculated = false } then ManagerCached.Add x member x.Remove () = ManagerCached.Remove x member x.v () = if x.Calculated then x.Value else x.Calculated <- true x.Value <- x.FuncValue () x.Value end type IState = interface abstract Update : unit -> unit abstract Flush : unit -> unit end module ManagerState = let States = new LinkedList<IState>() let Add (state : IState) = ignore (States.AddLast state) let Remove (state : IState) = ignore (States.Remove state) let Update () = for i in States do i.Update() let Flush () = for i in States do i.Flush() type State<'T> = class interface IState with member x.Update () = x.Next <- x.FuncState x.Current member x.Flush () = x.Current <- x.Next end val mutable FuncState : 'T -> 'T val mutable Current : 'T [<DefaultValue>] val mutable Next : 'T new (defaultValue : 'T, funcState : 'T -> 'T) as x = { Current = defaultValue; FuncState = funcState } then ManagerState.Add x member x.Remove () = ManagerState.Remove x member x.v () = x.Current end type IDiff = interface abstract Update : unit -> unit end module ManagerDiff = let Diffs = new LinkedList<IDiff>() let Add (diff : IDiff) = ignore (Diffs.AddLast diff) let Remove (diff : IDiff) = ignore (Diffs.Remove diff) let Update () = for i in Diffs do i.Update() type Diff<'T> = class [<DefaultValue>] static val mutable private FuncSub : 'T -> 'T -> 'T interface IDiff with member x.Update () = let _new = x.FuncValue () x._d <- Diff<'T>.FuncSub _new x.Old x.Old <- _new end val mutable FuncValue : unit -> 'T val mutable Old : 'T [<DefaultValue>] val mutable _d : 'T new (defaultValue : 'T, funcValue : unit -> 'T, funcSub : 'T -> 'T -> 'T) as x = { Old = defaultValue; FuncValue = funcValue } then ManagerDiff.Add x member x.Remove () = ManagerDiff.Remove x member x.d () : 'T = x._d end type ISimulationRemovable = interface abstract RemoveSimulation : unit -> unit end type Bind<'T> = delegate of unit -> 'T |
|
「Dual<'T>」の実装です
namespace Simulation open System open System.Collections.Generic type IDual = interface abstract Flush : unit -> unit end module ManagerDual = let Duals = new LinkedList<IDual>() let Add (dual : IDual) = ignore (Duals.AddLast dual) let Remove (dual : IDual) = ignore (Duals.Remove dual) let Flush () = for i in Duals do i.Flush() type Dual<'T> = class interface IDual with member x.Flush () = x._Current <- x._Next end val mutable _Current : 'T [<DefaultValue>] val mutable _Next : 'T new (defaultValue : 'T) as x = { _Current = defaultValue } then ManagerDual.Add x member x.Remove () = ManagerDual.Remove x member x.Current with get () = x._Current member x.Next with set (next : 'T) = x._Next <- next end |
|
追記
「type Bind<'T> = delegate of unit -> 'T」 は 「type Bind<'T> = unit -> 'T」 に直しても可能でしたよ |