Code Monkey home page Code Monkey logo

elm-holey-zipper's Introduction

Holey Zipper

Like a List zipper, but with more holes in it.

The basic idea is a zipper that can represent an empty list, can focus before and after every item, and doesn't make life hard.

import List.Holey.Zipper as Zipper

Zipper.empty           -- Zipper Hole a
    |> Zipper.plug 5   -- Zipper Full Int
    |> Zipper.append
        [ 1, 2, 3 ]    -- Zipper Full Int
    |> Zipper.nextHole -- Zipper Hole Int
    |> Zipper.toList   -- List Int
--> [ 5, 1, 2, 3 ]

So, there's that.


Made with love and released under BSD-3.

elm-holey-zipper's People

Contributors

zwilias avatar

Stargazers

Marek Fajkus avatar Roman Frołow avatar Matthieu Pizenberg avatar

Watchers

James Cloos avatar  avatar  avatar lue avatar

Forkers

lue-bird

elm-holey-zipper's Issues

Different data structure?

@zwilias Thanks for the package, and for the friendly suggestions to plug it our holes. Sounds fun! ;)

The idea of being able to have a non-empty list fits my case. However in the current implementation doing something like this causes an error:

revisionIdPerAuthorityZipper =
    case flags.revisionIdPerAuthority of
        [] ->
            Zipper.empty

        x :: xs ->
            Zipper.zipper x xs

Selection_999(405)

The thing is that it doesn't like it having Zipper Hole.. in one branch and Zipper Full ... in another.

What if we've changed the type to be:

type Zipper a
    = Zipper (List a) (ZipperType a) (List a)


type ZipperType a
    = Hole
    | Full a

Then we could also have Zipper.fromList that would take care of the logic.

type-safe `current`

List.Holey.Zipper is implemented using unsafe recursive code, but it really doesn't have to be:

type ZipperOnHole possiblyOrNever a
    = Zipper (List a) (Emptiable possiblyOrNever a) (List a)

type Emptiable possiblyOrNever a
    = Empty possiblyOrNever
    | Filled a

type Possibly
    = Possible

current : ZipperOnHole Never a -> a
current (Zipper _ selected _) =
    case selected of
        Filled value ->
            value
            
        Empty hole ->
            hole |> never -- Never <3

empty : ZipperOnHole Possibly a
empty =
    Zipper [] (Empty Possibly) []

zipper : a -> ZipperOnHole never_ a
zipper head tail =
    Zipper [] (Filled head) tail

Notice the never_ type variable, which solves the problem that branches that have Hole and Full don't combine into one type #2:

case list of
    [] -> Zipper.empty
    x :: xs -> Zipper.zipper x xs

--> : ZipperOnHole Possibly ...

I published Possibly and Empty, Stack, FocusList in new libs allowable-state, elm-emptiness-typed and scroll as it seemed more adequate to put them in a separate package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.