LANGUAGE | スカ友 | 俺、関東の国王だけど (イケメンだ(28)、彼女がほしい) 人類の品種 言葉の起源 虫は宇宙人です 日本語は海豚の言葉に由来します ペンギン(ウルトラマン) 関東無政府主義国へようこそ! simulationライブラリで純粋な関数式プログラミングを もっと見る
 1 2 3 4 5 ... 50
simulationライブラリで純粋な関数式プログラミングを
0   関東の国王      2019年1月13日 日 14:44
simulationライブラリで純粋な関数式プログラミングをします。
これではすべてのプログラムを記述できます。
C#, C++, F#バーがあります。

サンプル(太陽と地球のシュミレーション)もつけています(C#での)。
あと、このライブラリを用いて書いた「UI」ライブラリ(2D)もありますよ:
http://x0000.net/topic.aspx?id=3688-0

純粋な関数式とは、状態あり変数も関数式です(下記の「class State<T>」を参照)

C#:

namespace Simulation
{
   interface IUpdate
   {
       void Update();
   void Flush();
   }

   class Manager
   {
       public static Manager TheManager = new Manager();
       private LinkedList<IUpdate> Updates = new LinkedList<IUpdate>();
   public void Add(IUpdate update)
   {
   Updates.AddLast(update);
   }
   public void Update()
   {
   foreach(IUpdate i in Updates)
   i.Update();
   }
   public void Flush()
   {
   foreach(IUpdate i in Updates)
   i.Flush();
   }
   }
   

   interface IVar<T>
   {
       T v();
   }

   class CachedVar<T> : IVar<T>, IUpdate
   {
       private Func<T> FuncValue;
   private T Value;
   private bool Calculated;
       public CachedVar(Func<T> funcValue)
   {
   FuncValue = funcValue;
   Calculated = false;
   Manager.TheManager.Add(this);
   }
   public T v()
   {
   if (Calculated)
   return Value;
   else
   return Value = FuncValue();
   }
   public void Update(){}
   public void Flush()
   {
   Calculated = false;
   }
   }

   class State<T> : IVar<T>, IUpdate
   {
       private Func<T,T> FuncState;
   private T Current, Next;
       public State(T defaultValue, Func<T,T> funcState)
   {
           Current = defaultValue;
   FuncState = funcState;
   Manager.TheManager.Add(this);
   }
   public T v()
   {
   return Current;
   }
   public void Update()
   {
   Next = FuncState(Current);
   }
   public void Flush()
   {
   Current = Next;
   }
   }
}

C#のサンプルです:

namespace SimulationCSharp
{
   public partial class Form1 : Form
   {
       bool Exit = false;

       struct Vec
       {
           public double X, Y;
           public Vec(double x, double y) { X = x; Y = y; }
           public double Norm() { return Math.Sqrt(X * X + Y * Y); }
           public static Vec operator +(Vec v1, Vec v2) { return new Vec(v1.X + v2.X, v1.Y + v2.Y); }
           public static Vec operator -(Vec v1, Vec v2) { return new Vec(v1.X - v2.X, v1.Y - v2.Y); }
           public static Vec operator *(double k, Vec v) { return new Vec(k * v.X, k * v.Y); }
           public static Vec operator *(Vec v, double k) { return new Vec(v.X * k, v.Y * k); }
           public static Vec operator /(Vec v, double k) { return new Vec(v.X / k, v.Y / k); }
       }

       public Form1()
       {
           InitializeComponent();
       }

       private double AU = 149597870691; // m
       private double Year = 3600 * 24 * 365.256363004;
       private double G = 6.67300e-11; // m3*kg-1*s-2
       private double M = 1.98892e30; // kg 太陽
       private double m = 5.9742e24; // kg 地球
       private Vec vm0 = new Vec(30287, 0); // m/s 近日点での速度

       double TScale = 3600 * 24 * 7;
       double dt;
       long t1 = DateTime.Now.Ticks / 1000000;

       State<Vec> sM;
       State<Vec> sm;
       State<Vec> vM;
       State<Vec> vm;

       CachedVar<Vec> r_mM;
       CachedVar<Vec> r_Mm;
       CachedVar<Vec> gm;
       CachedVar<Vec> gM;

       private void Form1_Load(object sender, EventArgs e)
       {
           r_mM = new CachedVar<Vec>(() => sM.v() - sm.v());
           r_Mm = new CachedVar<Vec>(() => sm.v() - sM.v());
           gm = new CachedVar<Vec>(() => G * M * r_mM.v() / Math.Pow(r_mM.v().Norm(), 3));
           gM = new CachedVar<Vec>(() => G * m * r_Mm.v() / Math.Pow(r_Mm.v().Norm(), 3));

           sM = new State<Vec>(new Vec(0.0, 0.0), (x) => x + vM.v() * dt);
           sm = new State<Vec>(new Vec(0.0, 0.9832898912 * AU), (x) => x + vm.v() * dt); // 近日点
           vM = new State<Vec>(new Vec(0.0, 0.0), (x) => x + gM.v() * dt);
           vm = new State<Vec>(vm0, (x) => x + gm.v() * dt);
       }

       private void Form1_FormClosed(object sender, FormClosedEventArgs e)
       {
           Exit = true;
       }

       private void Form1_Shown(object sender, EventArgs e)
       {
           while (Exit == false)
           {
               long t2 = DateTime.Now.Ticks / 1000000;
               dt = TScale * Math.Min(t2 - t1, 0.05);
               t1 = t2;

               Manager.TheManager.Update();

               btnSun.Left = this.Width / 2 + (int)(100.0 * sM.v().X / AU);
               btnSun.Top = this.Height / 2 - (int)(100.0 * sM.v().Y / AU);
               btnEarth.Left = this.Width / 2 + (int)(100.0 * sm.v().X / AU);
               btnEarth.Top = this.Height / 2 - (int)(100.0 * sm.v().Y / AU);

               Manager.TheManager.Flush();

               Application.DoEvents();
           }
       }
   }
}
この記事は 2019年8月28日 水 18:11 に作者によって編集されました
1   関東の国王      2019年1月13日 日 14:44
C++:

#include <list>
#include <functional>

namespace simulation
{

class IUpdate
{
public:
virtual void Update()=0;
virtual void Flush()=0;
};

class Manager
{
private:
std::list<IUpdate *> m_pUpdates;
public:
Manager()
{
m_pUpdates = std::list<IUpdate *>();
}
void Add(IUpdate *pUpdate)
{
m_pUpdates.push_back(pUpdate);
}
void Update()
{
for(std::list<IUpdate *>::iterator i = m_pUpdates.begin(); i != m_pUpdates.end(); i++)
(*i)->Update();
}
void Flush()
{
for(std::list<IUpdate *>::iterator i = m_pUpdates.begin(); i != m_pUpdates.end(); i++)
(*i)->Flush();
}
};

Manager *g_pManager = new Manager;
double g_dt = 0.0;

template <class T> class IVar
{
public:
virtual T v()=0;
};

template <class T> class CachedVar : public IVar<T>, IUpdate
{
private:
std::tr1::function<T()> m_fValue;
T m_Value;
bool m_bCalculated;
public:
CachedVar(std::tr1::function<T()> fValue)
{
m_fValue = fValue;
m_bCalculated = false;
Manager->Add(this);
}
virtual T v()
{
if (m_bCalculated)
return m_Value;
else
return m_Value = m_fValue();
}
virtual void Update(){}
virtual void Flush()
{
m_bCalculated = false;
}
};

template <class T> class State : public IVar<T>, IUpdate
{
private:
std::tr1::function<T(T)> m_fState;
T m_Current, m_Next;
public:
State(T defaultValue, std::tr1::function<T(T)> fState)
{
m_fState = fState;
m_Current = defaultValue;
Manager->Add(this);
}
virtual T v()
{
return m_Current;
}
virtual void Update()
{
m_Next = m_fState(m_Current);
}
virtual void Flush()
{
m_Current = m_Next;
}
};

}
この記事は 2019年5月15日 水 4:29 に作者によって編集されました
2   関東の国王      2019年1月13日 日 14:46
F#:

namespace Simulation

   open System
   open System.Collections.Generic

   type IUpdate = interface
       abstract Update : unit -> unit
       abstract Flush : unit -> unit
   end

   module Manager =
       let Updates = new LinkedList<IUpdate>()
       let Add (update : IUpdate) = ignore (Updates.AddLast update)
       let Update () = for i in Updates do i.Update()
       let Flush () = for i in Updates do i.Flush()

   type IVar<'T> = interface
       abstract v : unit -> 'T
   end

   type CachedVar<'T> = class
       interface IVar<'T> with
           member x.v () =
               if x.Calculated then
                   x.Value
               else
                   x.Value <- x.FuncValue.Invoke ()
                   x.Value
       end
       interface IUpdate with
           member x.Update () = ()
           member x.Flush () = x.Calculated <- false
       end
       val mutable FuncValue : Func<'T>
       [<DefaultValue>]
       val mutable Value : 'T
       val mutable Calculated : bool
       new (funcValue : Func<'T>) as x =
           {
               FuncValue = funcValue;
               Calculated = false
           }
           then
               Manager.Add x
   end

   type State<'T> = class
       interface IVar<'T> with
           member x.v () = x.Current
       end
       interface IUpdate with
           member x.Update () = x.Next <- x.FuncState.Invoke(x.Current)
           member x.Flush () = x.Current <- x.Next
       end
       val mutable FuncState : Func<'T,'T>
       val mutable Current : 'T
       [<DefaultValue>]
       val mutable Next : 'T
       new (defaultValue : 'T, funcState : Func<'T,'T>) as x =
           {
               Current = defaultValue;
               FuncState = funcState
           }
           then
               Manager.Add x
   end
この記事は 2019年5月15日 水 4:30 に作者によって編集されました
3   https://pomodorr.com/      2019年5月26日 日 19:33
Thank you for every other great article. The place else could anyone get
that type of information in such a perfect manner of writing?
I've a presentation subsequent week, and I am on the search for such information.
4   pomodor.com      2019年5月28日 火 11:49
It's in fact very difficult in this busy life to listen news on Television, therefore
I simply use internet for that purpose, and take the latest news.
5   Salesforce Gadget      2019年7月4日 木 7:01
Do you have a spam issue on this site; I also am a blogger,
and I was wanting to know your situation; we have created some
nice practices and we are looking to trade solutions with
other folks, why not shoot me an email if interested.
6   Salesforce Gadget      2019年7月4日 木 11:05
Way cool! Some very valid points! I appreciate you writing this write-up plus the rest of the site is also really good.
7   avis vermox      2019年9月11日 水 0:55
comment3, http://achatfrance.fr/retin-a-0%2c025.html retin a,  %PP, http://franceaprix.fr/acheter-sinequan.html sinequan achat,  pjppi, http://achatfrance.fr/albenza.html commander albenza,  8-DDD, http://achatfrance.fr/strattera.html achat strattera,  >:DDD, http://achatfrance.fr/ponstel.html ponstel,  8432, http://franceachat.fr/acheter-clozaril-pilules.html commander clozaril,  8-OOO, http://franceachat.fr/acheter-indinavir-pilules.html indinavir,  >:-(, http://prixachat.fr/acheter-aldactone-fr.html aldactone france,  nabuyw, http://achatfrance.fr/claritin.html avis claritin,  =[, http://franceachat.fr/acheter-combivent-pilules.html acheter combivent,  :]], http://achatfrance.fr/procardia.html acheter procardia,  8DD, http://prixachat.fr/acheter-minomycin-fr.html generique minomycin,  kyca, http://franceaprix.fr/acheter-brand-cialis.html vente brand cialis,  8-), http://franceachat.fr/acheter-anafranil-pilules.html generique anafranil,  %],
8   vente dramamine      2019年9月11日 水 1:21
comment4, http://achatfrance.fr/prevacid.html achat prevacid en ligne,  3012, http://franceachat.fr/acheter-detrol-pilules.html achat detrol,  520929, http://achatfrance.fr/acticin.html achat acticin,  1200, http://franceachat.fr/acheter-glucophage-pilules.html acheter glucophage,  esq, http://achatfrance.fr/glucophage.html glucophage france,  dbr, http://franceaprix.fr/acheter-imuran.html imuran vente,  0092, http://franceachat.fr/acheter-maxalt-pilules.html pas cher maxalt,  llod, http://franceachat.fr/acheter-levitra-super-active-pilules.html levitra super active en ligne,  nruq, http://franceachat.fr/acheter-cordarone-pilules.html acheter cordarone en ligne,  zou, http://franceaprix.fr/acheter-antivert.html prix antivert,  3047, http://prixachat.fr/acheter-caverta-fr.html avis caverta,  5460, http://franceachat.fr/acheter-brand-amoxil-pilules.html acheter brand amoxil en ligne,  %)), http://prixachat.fr/acheter-betapace-fr.html betapace en ligne,  kezcq, http://franceachat.fr/acheter-cialis-jelly-pilules.html acheter cialis jelly france,  xvxcw,
9   Andreas      2019年9月11日 水 1:43
What qualifications have you got? http://www.dhiyafoundation.com/stmap_16hiyk5.html?almond-cucumber.cialis.aciphex is propecia prescription only uk  He also noted how the sciences tend to get a disproportionate amount of the money. “You end up with complete mismatches between the status of professors in the humanities and professors in the sciences, who are essentially entrepreneurs.”
10   Nicholas      2019年9月11日 水 1:43
I'm interested in  http://bandanegativa.ip360.agency/stmap_167wsth.html?maxaquin.cialis.thyroxine duloxetine yahoo  That's according to a new nationwide telephone survey of 1,007 adults ages 40-65with at least one parent who drives. The survey was conducted May 14-20 for insurer Liberty Mutual and has a margin of error of 3.01%.
11   Fermin      2019年9月11日 水 1:43
I'm not interested in football https://coach-sportif.net/stmap_16ltzf2.html?orlistat.viagra.diamox smiths-medical.com  The testimony led traders in futures markets to push back their expectations for when rates will rise to December 2014 from as early October 2014 a day earlier. The Fed said last month that 14 of its 19 policymakers do not believe it would be appropriate to raise rates until sometime in 2015.
12   Adalberto      2019年9月11日 水 1:43
Can you put it on the scales, please? http://www.thetraildog.com/stmap_29dc6ax.html?avandamet.cialis.colospa injectable anavar half life  The rare black jelly fish is also known as black nettle, and its tendrils can grow up to 25 feet. Sightings of the black nettle have been reported recently in Southern California, according to reports of Montery Bay Aquarium.
13   Arron      2019年9月11日 水 1:44
We went to university together http://fashtracks.com/stmap_16k4z5z.html?viagra.keftab.furadantin ooi-pharmacy.com  Andrews answered with a 1-yard TD surge to bring the Hilltoppers within 10, but the Ragin' Cajuns put the game away with a 10-play, 77-yard march that Harris capped with a 2-yard TD run to account for the final margin.
14   Trinity      2019年9月11日 水 1:44
good material thanks http://www.thetraildog.com/stmap_29dc6ax.html?gasex-sr.atenolol.levitra nolvadex ile kosztuje  The EU Tobacco Products Directive calls for mandatory text and picture health warnings covering 75 percent of the front and back of cigarette packs and would ban the sale of specially flavored cigarettes such as menthol and cinnamon.
15   Dennis      2019年9月11日 水 1:44
Would you like to leave a message? https://talassi.net/stmap_16zl2bc.html?prochlorperazine.atorvastatin.viagra.dapsone best generic drugs  "I had a sweet tooth like this — like a tree," he said. "I still dibble and dabble. There's certain cakes you've got to have, maybe a pie here and there. But I know to not do it as much."
16   Marlon      2019年9月11日 水 1:44
I'm afraid that number's ex-directory http://www.childrenmakingtomorrow.com/stmap_1698es4.html?levitra.simvastatin.doxepin purarehealth.com  The baggage-handling side of the business makes bothsoftware to track luggage and mechanical equipment such asconveyor belts. It has installed more than 300 baggage-handlingsystems at airports all over the world.
17   Pierre      2019年9月11日 水 1:44
very best job https://talassi.net/stmap_29chcpz.html?levitra.fluconazole.colcrys olio di neem erboristeria prezzi  Investigators are hoping the public will help identify him by placing digital billboards with his surveillance camera picture across the country. It&#8217;s a move investigators say has worked for them in the past.
18   Jarod      2019年9月11日 水 1:47
I'm in a band http://www.treasureofpandora.com/stmap_299iu4aw.html?vermox.cialis.olanzapine genacol plus  If the notions of financial reform and &#8220;draining the swamp&#8221; , as it were, were an earnest endeavor than why instill the notion the &#8220;revolving door&#8221; of failed government policy is alive and well.
19   Rocky      2019年9月11日 水 1:48
Is it convenient to talk at the moment? http://www.dhiyafoundation.com/stmap_2949lge.html?fertomid.cardizem.cialis tribulex xtreme  Sterling weakened after the data and British government bond prices pared losses as some investors had been betting on stronger growth which would have reduced further the chance of The Bank pumping more money into the economy.
 1 2 3 4 5 ... 50
ヒント: F#は海豚が操りました