-
Notifications
You must be signed in to change notification settings - Fork 271
/
Zipper.hs
95 lines (93 loc) · 2.88 KB
/
Zipper.hs
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-----------------------------------------------------------------------------
-- |
-- Module : Control.Lens.Zipper
-- Copyright : (C) 2012-13 Edward Kmett
-- License : BSD-style (see the file LICENSE)
-- Maintainer : Edward Kmett <ekmett@gmail.com>
-- Stability : experimental
-- Portability : non-portable
--
-- This module provides a 'Zipper' with fairly strong type checking guarantees.
--
-- The code here is inspired by Brandon Simmons' @zippo@ package, but uses
-- a different approach to represent the 'Zipper' that makes the whole thing
-- look like his breadcrumb trail, and can move side-to-side through
-- traversals.
--
-- Some examples types:
--
-- [@'Top' ':>>' a@] represents a trivial 'Zipper' with its focus at the root.
--
-- [@'Top' ':>>' 'Data.Tree.Tree' a ':>>' a@] represents a 'Zipper' that starts with a
-- 'Data.Tree.Tree' and descends in a single step to values of type @a@.
--
-- [@'Top' ':>>' 'Data.Tree.Tree' a ':>>' 'Data.Tree.Tree' a ':>>' 'Data.Tree.Tree' a@] represents a 'Zipper' into a
-- 'Data.Tree.Tree' with an intermediate bookmarked 'Data.Tree.Tree',
-- focusing in yet another 'Data.Tree.Tree'.
--
-- Since individual levels of a 'Zipper' are managed by an arbitrary
-- 'Control.Lens.Type.IndexedTraversal', you can move left and right through
-- the 'Control.Lens.Type.IndexedTraversal' selecting neighboring elements.
--
-- >>> zipper ("hello","world") & downward _1 & fromWithin traverse & focus .~ 'J' & rightmost & focus .~ 'y' & rezip
-- ("Jelly","world")
--
-- This is particularly powerful when compiled with 'Control.Lens.Plated.plate',
-- 'Data.Data.Lens.uniplate' or 'Data.Data.Lens.biplate' for walking down into
-- self-similar children in syntax trees and other structures.
--
-- Given keys in ascending order you can jump directly to a given key with
-- 'moveTo'. When used with traversals for balanced
-- tree-like structures such as an 'Data.IntMap.IntMap' or 'Data.Map.Map',
-- searching for a key with 'moveTo' can be done in logarithmic time.
-----------------------------------------------------------------------------
module Control.Lens.Zipper
(
-- * Zippers
Top()
, (:>)()
, (:>>)()
, (:@)()
, Zipper
, zipper
-- ** Focusing
, focus
, focusedContext
-- ** Vertical Movement
, upward
, downward, idownward
, within, iwithin
, withins, iwithins
-- ** Lateral Movement
, leftward, rightward
, leftmost, rightmost
-- ** Movement Combinators
, tug
, tugs
, jerks
, farthest
-- ** Absolute Positioning
, tooth
, teeth
, jerkTo
, tugTo
, moveTo
, moveToward
-- ** Closing the zipper
, rezip
, Zipped
, Zipping()
-- ** Recording
, Tape()
, saveTape
, restoreTape
, restoreNearTape
-- ** Unsafe Movement
, fromWithin
, ifromWithin
, unsafelyRestoreTape
) where
import Control.Lens.Internal.Zipper
-- $setup
-- >>> :set -XNoOverloadedStrings
-- >>> import Control.Lens