右と左
動機
右と左に同じ処理をする場合、2回書くのはめんどくさいよね ってことで、書いてしまった。
概要
Unityで上のC#で動くコードです。 Unityじゃなかったら適当にコード変えればいい。
右と左という関心事の分離をしたよ。 あと、ついでにC#に参照はないので、参照つくったよ。
コード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace Utility
{
public enum LR
{
Left,
Right
}
static public class Selector
{
static public LR TheOther(this LR val) { return (LR)(1 - (int)val); }
static public Pair<Type> Cons(Type a, Type d) { return new Pair<Type>(a, d); }
static public readonly LR[] selector = new LR[] { LR.Left, LR.Right };
}
[System.Serializable]
public struct Pair<Type>
{
public Pair(Type left, Type right)
{
this.left = left;
this.right = right;
}
[SerializeField]
Type left, right;
public Type Get(LR pos)
{
if (pos == LR.Left)
return left;
else return right;
}
public void Set(LR pos, Type value)
{
if (pos == LR.Left)
left = value;
else right = value;
}
}
public struct Reference<Type>
{
public Reference(Func<Type> get, Action<Type> set)
{
this.get = get;
this.set = set;
}
public Func<Type> get;
public Action<Type> set;
}
}