加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Clojure中使用C风格的封装技术?

发布时间:2020-12-16 10:17:12 所属栏目:百科 来源:网络整理
导读:我正在研究我的第一个(非平凡的) Clojure program我对我如何在全球宣布我所有可变状态感到不自在.例如: (def next-blocks (atom []))(def num-next-blocks 1)(def is-game-over (atom false))(def user-name (atom (str)))(def hs-xml (atom nil)) 由于我在
我正在研究我的第一个(非平凡的) Clojure program我对我如何在全球宣布我所有可变状态感到不自在.例如:

(def next-blocks (atom []))
(def num-next-blocks 1)
(def is-game-over (atom false))
(def user-name (atom (str)))
(def hs-xml (atom nil))

由于我在工作中使用了很多C,因此我提出了使用常见的C风格封装技术的想法.它通常涉及struct对象,它作为第一个参数传递给对其进行操作的任何“成员函数”.例如,见udev.

将此应用于Clojure将导致函数看起来像这样(未经测试):

(defstruct gamestate)

(defn game-new []
  (struct-map gamestate
    :level            (atom 0)
    :score            (atom 0)
    ;etc...
    ))

(def game-get-score [game]
    @(game :score))

(defn game-set-score [game new-score]
  (reset! (game :score) new-score))

(defn game-get-level [game]
  @(game :level))

(defn game-inc-level [game]
  (swap! (game :level) inc))

; etc...

我认为这肯定是我目前正在使用的全球定义的一个步骤.

这是推荐的方式吗?或者是否有更标准的Clojure方式?

更新

我目前正在使用Clojure 1.1.0.

解决方法

函数式编程的基本思想是你只有很少的全局变量,但主要是局部变量(见 Rich Hickey’s article on state and identity).在这个范例中编写游戏可能具有挑战性,我建议使用 this post on functional programming for games(虽然这些例子在Erlang中).

我不知道你想要实现什么样的游戏.这是我对局部变量做的代码片段改进.

(defn play-game [name score blocks]
  (let [level (start-mission (:level score) blocks)]
    (if level
      (assoc score :level level)
      score)))

(defn the-whole-game []
  (let [name (ask-username)
        score (or (load-score name) {:level 0,:score 0}]
    (when-let [new-score (play-game name score [])]
       (save-score name new-score))))

您可能想要查看another Tetris clone in clojure,但它使用的是opengl.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读