Skip to content

Instantly share code, notes, and snippets.

@hyakugei
Last active April 28, 2018 18:38
Show Gist options
  • Save hyakugei/1f3d15b45ea80ca07e310f20a51a487c to your computer and use it in GitHub Desktop.
Save hyakugei/1f3d15b45ea80ca07e310f20a51a487c to your computer and use it in GitHub Desktop.
Unity3d IEnumerator, Coroutines and Partial Function Application
// Learning from https://codeblog.jonskeet.uk/2012/01/30/currying-vs-partial-function-application/
public void DoThings()
{
Func<Vector3, PlayerController, IEnumerator> orig = MoveToLocationAction;
var t = ApplyPartial(orig, data.eventObjects[0].point);
var t1 = ApplyPartial(t, player);
var tt = ApplyPartial(orig, data.eventObjects[1].point);
var tt1 = ApplyPartial(t, player);
StartCoroutine(DoActionItems(t1(), tt1()));
}
IEnumerator DoActionItems(params IEnumerator[] list)
{
foreach(var item in list)
{
yield return StartCoroutine(item);
}
yield break;
}
IEnumerator MoveToLocationAction(Vector3 point, PlayerController player)
{
bool notArrived = true;
player.Run();
player.SetDestination(point);
System.Action<PlayerController> lambda = (x) => {
Debug.Log("Callback! " + x.name);
notArrived = false;
};
player.OnArrivedEvent += lambda;
while(notArrived)
{
yield return null;
}
Debug.Log("Arrived!");
player.OnArrivedEvent -= lambda;
}
static Func<T2, T3, TResult> ApplyPartial<T1, T2, T3, TResult>
(Func<T1, T2, T3, TResult> function, T1 arg1)
{
return (b, c) => function(arg1, b, c);
}
static Func<T2, TResult> ApplyPartial<T1, T2, TResult>
(Func<T1, T2, TResult> function, T1 arg1)
{
return arg2 => function(arg1, arg2);
}
static Func<TResult> ApplyPartial<T1, TResult>
(Func<T1, TResult> function, T1 arg1)
{
return () => function(arg1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment