{-# LANGUAGE CPP #-}{-# LANGUAGE Rank2Types #-}{-# LANGUAGE TypeFamilies #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE LiberalTypeSynonyms #-}{-# LANGUAGE MultiParamTypeClasses #-}#if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 704{-# LANGUAGE Trustworthy #-}#endif--------------------------------------------------------------------------------- |-- Module : Data.Vector.Generic.Lens-- Copyright : (C) 2012 Edward Kmett-- License : BSD-style (see the file LICENSE)-- Maintainer : Edward Kmett <ekmett@gmail.com>-- Stability : provisional-- Portability : non-portable---- This module provides lenses and traversals for working with generic vectors.-------------------------------------------------------------------------------moduleData.Vector.Generic.Lens(toVectorOf-- * Isomorphisms,forced,vector,asStream,asStreamR,cloned,reversed-- * Lenses,_head,_tail,_last,_init,sliced-- * Traversal of individual indices,ordinal,ordinals)whereimportControl.ApplicativeimportControl.LensimportData.List(nub)importData.MonoidimportData.Vector.GenericasVhiding(zip,filter)importData.Vector.Fusion.Stream(Stream)importData.Vector.Generic.New(New)importPreludehiding((++),length,head,tail,init,last,map,reverse)-- $setup-- >>> import Data.Vector as Vector-- | A lens reading and writing to the 'head' of a /non-empty/ 'Vector'---- Attempting to read or write to the 'head' of an /empty/ 'Vector' will result in an 'error'.---- >>> Vector.fromList [1,2,3]^._head-- 1_head::Vectorva=>SimpleLens(va)a_headfv=(\a->v//[(0,a)])<$>f(headv){-# INLINE _head #-}-- | A 'Lens' reading and writing to the 'last' element of a /non-empty/ 'Vector'---- Attempting to read or write to the 'last' element of an /empty/ 'Vector' will result in an 'error'.---- >>> Vector.fromList [1,2]^._last-- 2_last::Vectorva=>SimpleLens(va)a_lastfv=(\a->v//[(lengthv-1,a)])<$>f(lastv){-# INLINE _last #-}-- | A lens reading and writing to the 'tail' of a /non-empty/ 'Vector'---- Attempting to read or write to the 'tail' of an /empty/ 'Vector' will result in an 'error'.---- >>> _tail .~ Vector.fromList [3,4,5] $ Vector.fromList [1,2]-- fromList [1,3,4,5]_tail::Vectorva=>SimpleLens(va)(va)_tailfv=cons(headv)<$>f(tailv){-# INLINE _tail #-}-- | A 'Lens' reading and replacing all but the a 'last' element of a /non-empty/ 'Vector'---- Attempting to read or write to all but the 'last' element of an /empty/ 'Vector' will result in an 'error'.---- >>> Vector.fromList [1,2,3,4]^._init-- fromList [1,2,3]_init::Vectorva=>SimpleLens(va)(va)_initfv=(`snoc`lastv)<$>f(initv){-# INLINE _init #-}-- | @sliced i n@ provides a lens that edits the @n@ elements starting at index @i@ from a lens.---- This is only a valid lens if you do not change the length of the resulting 'Vector'.---- Attempting to return a longer or shorter vector will result in violations of the 'Lens' laws.sliced::Vectorva=>Int-- ^ @i@ starting index->Int-- ^ @n@ length->SimpleLens(va)(va)slicedinfv=(\v0->v//zip[i..i+n-1](V.toListv0))<$>f(sliceinv){-# INLINE sliced #-}-- | Similar to 'toListOf', but returning a 'Vector'.toVectorOf::Vectorva=>Getting(Endo[a])stab->s->vatoVectorOfls=fromList(toListOfls){-# INLINE toVectorOf #-}-- | Convert a list to a 'Vector' (or back)vector::Vectorva=>SimpleIso[a](va)vector=isofromListV.toList{-# INLINE vector #-}-- | Convert a 'Vector' to a finite 'Stream' (or back)asStream::Vectorva=>SimpleIso(va)(Streama)asStream=isostreamunstream{-# INLINE asStream #-}-- | Convert a 'Vector' to a finite 'Stream' from right to left (or back)asStreamR::Vectorva=>SimpleIso(va)(Streama)asStreamR=isostreamRunstreamR{-# INLINE asStreamR #-}-- | Convert a 'Vector' back and forth to an initializer that when run produces a copy of the 'Vector'.cloned::Vectorva=>SimpleIso(va)(Newva)cloned=isoclonenew{-# INLINE cloned #-}-- | Convert a 'Vector' to a version that doesn't retain any extra memory.forced::Vectorva=>SimpleIso(va)(va)forced=isoforceforce{-# INLINE forced #-}-- | Convert a 'Vector' to a version with all the elements in the reverse orderreversed::Vectorva=>SimpleIso(va)(va)reversed=isoreversereverse{-# INLINE reversed #-}-- | This is a more efficient version of 'element' that works for any 'Vector'.---- @ordinal n@ is only a valid 'Lens' into a 'Vector' with 'length' at least @n + 1@.ordinal::Vectorva=>Int->SimpleIndexedLensInt(va)aordinali=index$\fv->(\a->v//[(i,a)])<$>fi(v!i){-# INLINE ordinal #-}-- | This 'Traversal' will ignore any duplicates in the supplied list of indices.---- >>> toListOf (ordinals [1,3,2,5,9,10]) $ Vector.fromList [2,4..40]-- [4,8,6,12,20,22]ordinals::Vectorva=>[Int]->SimpleIndexedTraversalInt(va)aordinalsis=index$\fv->letl=lengthvis'=nub$filter(<l)isinfmap((v//).zipis').traverse(uncurryf).zipis$fmap(v!)is'{-# INLINE ordinals #-}