抑制 TRIE 的嵌套 Maps 的 Clojure 拉链 [英] Clojure Zipper of nested Maps repressing a TRIE

查看:19
本文介绍了抑制 TRIE 的嵌套 Maps 的 Clojure 拉链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为由嵌套映射表示的 TRIE 创建 Clojure 拉链,键是字母吗?

How can I create a Clojure zipper for a TRIE, represented by nested maps, were the keys are the letters.?

像这样:

{ {a {
 {a {
 {a {'$ '$}}}}}} a {
 {a {'$ '$}}}}

代表一个带有 2 个单词 'banana' 和 'ana' 的树.(如有必要,可以在地图中进行一些更改..)

Represents a trie with 2 words 'banana' and 'ana'. (If necessary , its possible to make some changes here in maps..)

我试图通过 map?vals assoc 分别作为拉链的 3 个功能.但它似乎不起作用..

I've tried to pass map? vals assoc as the 3 functions to the zipper,respectively. But it doesnt seem to work..

我应该使用哪 3 个函数?

What 3 functions should I use?

基于拉链的 insert-into-trie 是什么样子的?

And how the insert-into-trie would look like based on the zipper ?

推荐答案

map? vals #(zipmap (keys %1) %2) 可以,但不支持插入/删除子项(因为子项只是值,您不知道要删除/添加哪个键).

map? vals #(zipmap (keys %1) %2) would do but doesn't support insertion/removal of children (since children are only values, you don't know which key to remove/add).

下面的 map-zipper 确实支持插入/删除,因为节点是 [k v] 对(除了作为映射的根).

The map-zipper below does support insertion/removal because nodes are [k v] pairs (except the root which is a map).

(defn map-zipper [m]
  (z/zipper 
    (fn [x] (or (map? x) (map? (nth x 1))))
    (fn [x] (seq (if (map? x) x (nth x 1))))
    (fn [x children] 
      (if (map? x) 
        (into {} children) 
        (assoc x 1 (into {} children))))
    m))

这篇关于抑制 TRIE 的嵌套 Maps 的 Clojure 拉链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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