yotamdvir / elm-pivot Goto Github PK
View Code? Open in Web Editor NEWPivot: a list with a cursor pointing at the center, like a zipper.
License: BSD 3-Clause "New" or "Revised" License
Pivot: a list with a cursor pointing at the center, like a zipper.
License: BSD 3-Clause "New" or "Revised" License
See example below. For readability, list notation with center denoted by *
s is used for a pivot.
mapR_ (List.take 1) [1,2,*3*,4,5] == [1,2,*3*,5]
mapCLR_ identity identity (List.take 1) [1,2,*3*,4,5] == [1,2,*3*,4]
Awkward implementation and lack of testing are to blame.
Lines 70 to 73 in cee1b98
Documentation makes matters worse, as it hints that these two applications above should have the same result.
Lines 530 to 532 in cee1b98
As for fixing, the following works:
mapR_ = mapCLR_ identity identity
Still, it could be optimized, as mapCLR_
internally reverses the left side twice.
indexedMapCLR
would be really nice right about now in my project. ...Or at least an easy way to zip a tuple of ( Int, a )
.
It appears that the uses of abstractions such as function calls, the function mirror
, etc impact performance even though they are functionally equivalent to other more direct implementations (see discussion in #13).
setR
reverses its argument. For example, setR [4, 5, 6] [ 1 * 2 * 3 ] == [ 1 * 2 * 6 5 4 ]
.
Would you like me to submit a fix?
Attached: a property test suite that helped me diagnose the issue
module Tests.Pivot exposing (pivotTests)
import Expect
import Fuzz exposing (Fuzzer)
import Pivot exposing (Pivot)
import Test exposing (Test, describe)
genPivotInt : Fuzzer (Pivot Int)
genPivotInt =
Fuzz.map3
(\a b c -> Pivot.singleton b |> Pivot.setL a |> Pivot.setR c)
(Fuzz.list Fuzz.int)
Fuzz.int
(Fuzz.list Fuzz.int)
genListInt : Fuzzer (List Int)
genListInt =
Fuzz.list Fuzz.int
pivotTests : Test
pivotTests =
describe "pivot"
[ Test.fuzz genPivotInt "setL (getL a) == a" <|
\pivot ->
Pivot.setL (Pivot.getL pivot) pivot
|> Expect.equal pivot
, Test.fuzz2 genListInt genPivotInt "getL (setL l a) == l" <|
\list pivot ->
Pivot.getL (Pivot.setL list pivot)
|> Expect.equal list
, Test.fuzz genPivotInt "setR (getR a) == a" <|
\pivot ->
Pivot.setR (Pivot.getR pivot) pivot
|> Expect.equal pivot
, Test.fuzz2 genListInt genPivotInt "getR (setR l a) == l" <|
\list pivot ->
Pivot.getR (Pivot.setR list pivot)
|> Expect.equal list
, Test.fuzz3 Fuzz.int genListInt genPivotInt "goR (setR (x :: xs) a) == Just (setR xs (appendGoR x a))" <|
\x xs pivot ->
Pivot.goR (Pivot.setR (x :: xs) pivot)
|> Expect.equal (Just (Pivot.setR xs (Pivot.appendGoR x pivot)))
]
It's coming soon. Mostly I want to make sure @yotamDvir is still actively maintaining this library. :)
This is zip
as it is now:
Lines 81 to 96 in cee1b98
It indexes the pivot like a list, without any regard to where the center is.
It may be beneficial to have a relative version.
zipRelative : Pivot a -> Pivot ( Int, a )
zipRelative pvt =
let
onC =
\x -> (0,x)
onL =
List.indexedMap (\i x -> (-1 - i,x))
onR =
List.indexedMap (\i x -> (i + 1, x))
in
mapCLR_ onC onL onR pvt
For example, a situation where one knows their pivot is going to get jumbled, and would like to retain their current positions afterwards.
In such a case we could also change the name of zip
to zipAbsolute
, leaving less room for confusion. To increase uniformity, we could further replace goTo
by goAbsolute
; and goBy
by goRelative
.
In fact, why is it called zip
in the first place? index
is better...
Tests should cover the entire library.
The only changes I see to be had are removing '
primes from variables. I'd also suggest renaming pure
to singleton
to match other Elm libraries.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.