zipper

Printing a tree lazily in Newick format

我怕爱的太早我们不能终老 提交于 2019-12-01 00:24:40
I wish to print a binary tree in Newick format , showing each node's distance to its parent. At the moment I haven't had an issue with the following code, which uses regular recursion, but a tree too deep may produce a stack overflow. (defn tree->newick [tree] (let [{:keys [id children to-parent]} tree dist (double to-parent)] ; to-parent may be a rational (if children (str "(" (tree->newick (first children)) "," (tree->newick (second children)) "):" dist) (str (name id) ":" dist)))) (def example {:id nil :to-parent 0.0 :children [{:id nil :to-parent 0.5 :children [{:id "A" :to-parent 0.3

Zipper to iterate over list in Scala

早过忘川 提交于 2019-11-30 18:22:45
This is a follow-up to my previous question . I can use an iterator, fold , zip , foreach and others to iterate over a list in Scala. Now I wonder if there are use cases where Zipper is most appropriate. Suppose I need read-only access without concurrency. Could you give such an example and explain why the Zipper is the best choice? One of the many neat things about zippers is that they have a comonad instance, which allows us to solve a certain class of problems very elegantly. Here's a quick example off the top of my head. Suppose that we've got a sequence of numbers and we want to do a

Clojure Zipper of nested Maps repressing a TRIE

旧城冷巷雨未停 提交于 2019-11-30 08:34:38
问题 How can I create a Clojure zipper for a TRIE, represented by nested maps, were the keys are the letters.? Something like this: {\b {\a {\n {\a {\n {\a {'$ '$}}}}}} \a {\n {\a {'$ '$}}}} Represents a trie with 2 words 'banana' and 'ana'. (If necessary , its possible to make some changes here in maps..) I've tried to pass map? vals assoc as the 3 functions to the zipper,respectively. But it doesnt seem to work.. What 3 functions should I use? And how the insert-into-trie would look like based

Zipper to iterate over list in Scala

爷,独闯天下 提交于 2019-11-30 01:26:46
问题 This is a follow-up to my previous question. I can use an iterator, fold , zip , foreach and others to iterate over a list in Scala. Now I wonder if there are use cases where Zipper is most appropriate. Suppose I need read-only access without concurrency. Could you give such an example and explain why the Zipper is the best choice? 回答1: One of the many neat things about zippers is that they have a comonad instance, which allows us to solve a certain class of problems very elegantly. Here's a

Zipper like data structure with more than one cursor

巧了我就是萌 提交于 2019-11-29 00:39:43
问题 The Zipper data structure is great when one wants to traverse a tree and keep the current position, but what data structure one should use if they want to track more then one position? Let me explain with examples: Someone on the #haskell channel has told me that zippers are used in yi editor to represent the cursor position. This is great, but what if you want to have two cursors. Like if you want to represent a selection, you need to know the beginning and the end of the selection. In the

Two-dimensional zipper

杀马特。学长 韩版系。学妹 提交于 2019-11-28 09:00:58
Inspired by the recent question about 2d grids in Haskell, I'm wondering if it would be possible to create a two-dimensional zipper to keep track of a position in a list of lists. A one-dimensional zipper on a list allows us to really efficiently move locally in a large list (the common example being a text editor). But lets say we have a second dimension like this: grid = [[ 1, 2, 3, 4, 5] ,[ 6, 7, 8, 9,10] ,[11,12,13,14,15] ,[16,17,18,19,20] ,[21,22,23,24,25]] Can we create some kind of zipper data structure to efficiently move not only left and right but up and down in the grid here? If so,

What is the Zipper data structure and should I be using it?

≯℡__Kan透↙ 提交于 2019-11-28 03:54:55
The question is simple: I cannot understand the Zipper data structure. My question is related to its uses with a Tree. I want to understand how can I change the tree node using zipper. And how not to copy the whole tree (or the most part of it). Please, clarify if I'm wrong with zipper. Maybe it cannot help with the tree update? Or, maybe, it is possible to update the tree and I just cannot see the way? Let's start with the Zipper-analog for lists. If you'd like to modify the nth element of a list, it takes O(n) because you have to copy the n-1 first elements. Instead, you can keep the list as

Two-dimensional zipper

这一生的挚爱 提交于 2019-11-27 19:18:50
问题 Inspired by the recent question about 2d grids in Haskell, I'm wondering if it would be possible to create a two-dimensional zipper to keep track of a position in a list of lists. A one-dimensional zipper on a list allows us to really efficiently move locally in a large list (the common example being a text editor). But lets say we have a second dimension like this: grid = [[ 1, 2, 3, 4, 5] ,[ 6, 7, 8, 9,10] ,[11,12,13,14,15] ,[16,17,18,19,20] ,[21,22,23,24,25]] Can we create some kind of

What is the Zipper data structure and should I be using it?

耗尽温柔 提交于 2019-11-27 00:13:39
问题 The question is simple: I cannot understand the Zipper data structure. My question is related to its uses with a Tree. I want to understand how can I change the tree node using zipper. And how not to copy the whole tree (or the most part of it). Please, clarify if I'm wrong with zipper. Maybe it cannot help with the tree update? Or, maybe, it is possible to update the tree and I just cannot see the way? 回答1: Let's start with the Zipper-analog for lists. If you'd like to modify the nth element

Zipper Comonads, Generically

独自空忆成欢 提交于 2019-11-26 21:21:37
Given any container type we can form the (element-focused) Zipper and know that this structure is a Comonad. This was recently explored in wonderful detail in another Stack Overflow question for the following type: data Bin a = Branch (Bin a) a (Bin a) | Leaf a deriving Functor with the following zipper data Dir = L | R data Step a = Step a Dir (Bin a) deriving Functor data Zip a = Zip [Step a] (Bin a) deriving Functor instance Comonad Zip where ... It is the case that Zip is a Comonad though the construction of its instance is a little hairy. That said, Zip can be completely mechanically