F#:存储和映射函数列表 [英] F#: Storing and mapping a list of functions

查看:183
本文介绍了F#:存储和映射函数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些事件发生在游戏中。我想控制这些事件发生的时间和顺序。



例如:



事件1:在屏幕上显示一些N帧和发挥音效



事件2:清除屏幕上的文本



我的解决方案(也许有一个更好的一个),是一个包含事件的函数的列表。事件执行他们的行为,然后返回下一个事件发生在游戏中。我想到使用List.map或List.collect,因为我基本上将事件列表映射到新的事件列表,同时执行一些行为代码。



在示例中上面,Event1可以由两个功能组成:一个显示文本和一个播放声音(因此需要一个列表)。显示文本的函数将返回自身的N-1个帧的副本,那么它将返回清除文本的Event2。播放声音功能将返回相当于no-op。



如果这是一个很好的解决方案,我可以在C ++或C#中执行。我的目标是在F#中做一个相当或更好的解决方案。

解决方案

你的意思是这样吗? b
$ b

  let myActions = 
[fun() - > printfn你醒了一条龙。
fun() - > printfn你击中了龙的0点伤害。
fun() - > printfn龙bel bel。
fun() - > printfn你已经死了]

让actionBuilder actionList =
let actions = ref actionList
fun() - >
match!action with
| [] - > ()
| h :: t - > H();操作:= t

用法(F#interactive):

 >让doSomething = actionBuilder myActions ;; 

val doSomething:(unit - > unit)

>做一点事();;
你醒了一条龙。
val it:unit =()
>做一点事();;
你击中了龙的0点伤害。
val it:unit =()
>做一点事();;
龙bel。。
val it:unit =()
>做一点事();;
你已经死了
val it:unit =()
>做一点事();;
val it:unit =()
>

**编辑:**如果你想要添加动作,也许最好是做一个动作分配器,在内部使用一个队列,因为追加是O(N)与一个列表,O(1)与一个队列:

  type actionGenerator(myActions:(unit-> unit)list)= 
let Q = new System.Collections.Generic.Queue< _>(Seq.ofList myActions)

member g.NextAction =
fun() - >
如果Q.Count = 0 then()
else Q.Dequeue()()

member g.AddAction(action)= Q.Enqueue(action)


I have a number of events that happen in a game. I want to control the time and order at which these events occur.

For example:

Event 1: Show some text on screen for N frames & play a sound effect

Event 2: Clear the text on the screen

My solution (maybe there is a better one), is to have a list of functions that contain the events. The events perform their behavior then return the next events to occur in the game. I thought of using List.map or List.collect because I am essentially mapping a list of events to a new list of events while performing some behavior code.

In the example above, Event1 can be composed of two functions: One that shows text and one that plays a sound (hence the need for a list). The function that shows text would return a copy of itself for N-1 frames then it would return Event2 which clears the text. The play sound function would return the equivalent of a no-op.

If this is a good solution, I could probably do it in C++ or C#. My goal is to do an equivalent or better solution in F#.

解决方案

Did you mean something like this?

let myActions =
    [fun () -> printfn "You've woken up a dragon."
     fun () -> printfn "You hit the dragon for 0 points of damage."
     fun () -> printfn "The dragon belches."
     fun () -> printfn "You have died."] 

let actionBuilder actionList = 
    let actions = ref actionList
    fun () ->
        match !actions with
        | [] -> ()
        | h::t -> h(); actions := t

Usage (F# interactive):

> let doSomething = actionBuilder myActions;;

val doSomething : (unit -> unit)

> doSomething();;
You've woken up a dragon.
val it : unit = ()
> doSomething();;
You hit the dragon for 0 points of damage.
val it : unit = ()
> doSomething();;
The dragon belches.
val it : unit = ()
> doSomething();;
You have died.
val it : unit = ()
> doSomething();;
val it : unit = ()
> 

**Edit: ** if you want to be able to add actions, maybe it's better to make an action dispenser that uses a Queue internally, since appending is O(N) with a list and O(1) with a Queue:

type actionGenerator(myActions: (unit->unit) list) =
    let Q = new System.Collections.Generic.Queue<_>(Seq.ofList myActions)

    member g.NextAction = 
        fun () -> 
            if Q.Count = 0 then ()
            else Q.Dequeue()()

    member g.AddAction(action) = Q.Enqueue(action)

这篇关于F#:存储和映射函数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆