zipper

Zipper vs. iterator for walking over a list or tree

﹥>﹥吖頭↗ 提交于 2019-12-10 02:35:01
问题 Suppose I need to walk over a list or tree to read (but not modify) the data. I can use either an iterator or a Zipper. Does Zipper have any advantages aside from immutability in this case? 回答1: Do you need to backtrack or otherwise move around in the structure in a non-sequential order? Do you want to be able to traverse the structure multiple times without worrying about where you left the iteration? Do you want to avoid thinking about concurrent access or thread safety? Go with the zipper.

returning multiple values using clojure xml zipper

僤鯓⒐⒋嵵緔 提交于 2019-12-07 11:36:58
问题 Lets suppose we have some XML like so: <a> <b> <c>text</c> <d> <e>text</e> <f> ... lots of cruft here .. </f> </d> </b> <b> ... </b> <!-- more b sub-trees --> </a> Now, looking through the samples in zip_filter/xml.clj, I've figured out how to get to single values that I'm interested in. I'm wondering how I would do something like return a list of pairs of text values of (c e). EDIT: Here is some working code, but it's pretty ugly. Not asking for trivial refactoring, but is there a nicer way

Zipping zippers in Anti-XML

你说的曾经没有我的故事 提交于 2019-12-06 01:00:52
In this question , the asker wants to transform documents like this: <text> The capitals of Bolivia are <blank/> and <blank/>. </text> Into this: <text> The capitals of Bolivia are <input name="blank.1"> and <input name="blank.2">. </text> As I noted in my answer there , Anti-XML 's zippers provide a clean solution to this problem. The following, for example, would work for renaming the blank elements: import com.codecommit.antixml._ val q = <text>The capitals of Bolivia are <blank/> and <blank/>.</text>.convert (q \\ "blank").map(_.copy(name = "input")).unselect Unfortunately the following

returning multiple values using clojure xml zipper

北城余情 提交于 2019-12-05 15:52:29
Lets suppose we have some XML like so: <a> <b> <c>text</c> <d> <e>text</e> <f> ... lots of cruft here .. </f> </d> </b> <b> ... </b> <!-- more b sub-trees --> </a> Now, looking through the samples in zip_filter/xml.clj, I've figured out how to get to single values that I'm interested in. I'm wondering how I would do something like return a list of pairs of text values of (c e). EDIT: Here is some working code, but it's pretty ugly. Not asking for trivial refactoring, but is there a nicer way that zippers give us to do this? (defn extract-data [xml] (let [items (x/xml-> xml zf/descendants :Item

Zipper vs. iterator for walking over a list or tree

我与影子孤独终老i 提交于 2019-12-05 02:36:17
Suppose I need to walk over a list or tree to read (but not modify) the data. I can use either an iterator or a Zipper . Does Zipper have any advantages aside from immutability in this case? Do you need to backtrack or otherwise move around in the structure in a non-sequential order? Do you want to be able to traverse the structure multiple times without worrying about where you left the iteration? Do you want to avoid thinking about concurrent access or thread safety? Go with the zipper. Do you know for a fact that you need the extra performance an iterator can provide in some situations? Are

How well do zippers perform in practice, and when should they be used?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 16:00:08
问题 I think that the zipper is a beautiful idea; it elegantly provides a way to walk a list or tree and make what appear to be local updates in a functional way. Asymptotically, the costs appear to be reasonable. But traversing the data structure requires memory allocation at each iteration, where a normal list or tree traversal is just pointer chasing. This seems expensive (please correct me if I am wrong). Are the costs prohibitive? And what under what circumstances would it be reasonable to

Are all differentiable types Monads

£可爱£侵袭症+ 提交于 2019-12-04 08:59:22
问题 Given a differentiable type, we know that its Zipper is a Comonad. In response to this, Dan Burton asked, "If derivation makes a comonad, does that mean that integration makes a monad? Or is that nonsense?". I'd like to give this question a specific meaning. If a type is differentiable, is it necessarily a monad? One formulation of the question would be to ask, given the following definitions data Zipper t a = Zipper { diff :: D t a, here :: a } deriving instance Diff t => Functor (Zipper t)

Apply function to one element only in list or array in Scala

故事扮演 提交于 2019-12-03 17:14:12
For any given list or array, for instance val list = (1 to 3).toList val array = (1 to 3).toArray and a given function that maps from and onto the collection type, for instance def f(v: Int): Int = v + 10 how to apply f to the ith element of list or array so that list.myApply(f, ith = 2) res: List(1,12,3) and also array.myApply(f, ith = 2) res: Array(1,12,3) tl;dr import scala.collection.SeqLike import scala.collection.generic.CanBuildFrom implicit class Seq_[A, Repr, S : ({type L[X] = X => SeqLike[A, Repr]})#L](seq: S) { def myApply[B >: A, That](f: A => B, ith: Int) (implicit bf:

How well do zippers perform in practice, and when should they be used?

♀尐吖头ヾ 提交于 2019-12-03 09:59:57
I think that the zipper is a beautiful idea; it elegantly provides a way to walk a list or tree and make what appear to be local updates in a functional way. Asymptotically, the costs appear to be reasonable. But traversing the data structure requires memory allocation at each iteration, where a normal list or tree traversal is just pointer chasing. This seems expensive (please correct me if I am wrong). Are the costs prohibitive? And what under what circumstances would it be reasonable to use a zipper? I can provide one solid data point: John Dias and I had a paper in the 2005 ML Workshop

Understanding why Zipper is a Comonad

南楼画角 提交于 2019-12-03 06:07:04
问题 This is a follow-up to the answer to my previous question. Suppose I need to map each item a:A of List[A] to b:B with function def f(a:A, leftNeighbors:List[A]): B and generate List[B] . Obviously I cannot just call map on the list but I can use the list zipper . The zipper is a cursor to move around a list. It provides access to the current element ( focus ) and its neighbors. Now I can replace my f with def f'(z:Zipper[A]):B = f(z.focus, z.left) and pass this new function f' to cobind