GithubHelp home page GithubHelp logo

odr / patch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from reflex-frp/patch

0.0 0.0 0.0 1.59 MB

Data structures for describing changes to other data structures.

Home Page: https://hackage.haskell.org/package/patch

License: BSD 3-Clause "New" or "Revised" License

Shell 0.09% C 7.57% Haskell 88.53% Nix 3.81%

patch's Introduction

patch

Haskell Hackage BSD3 License

Data structures for describing changes to other data structures.

A Patch type represents a kind of change made to a data structure.

class Patch p where
  type PatchTarget p :: *
  -- | Apply the patch p a to the value a.  If no change is needed, return
  -- 'Nothing'.
  apply :: p -> PatchTarget p -> Maybe (PatchTarget p)

Patching Maps

For example, Data.Patch.Map defines the PatchMap type which can be used to patch Maps. A PatchMap represents updates to a Map that can insert, remove, or replace items in the Map. In this example, the Map is the PatchTarget and the PatchMap is the Patch. Keep in mind that there are many other possible Patches that can be applied to a Map (i.e., Map can be the PatchTarget for many different Patch instances).

PatchMap is defined as:

newtype PatchMap k v = PatchMap { unPatchMap :: Map k (Maybe v) }

The Maybe around the value is used to represent insertion/updates or deletion of the element at a given key.

Its Patch instance begins with:

instance Ord k => Patch (PatchMap k v) where
  type PatchTarget (PatchMap k v) = Map k v
  ...

When a PatchMap is applied to its PatchTarget, the following changes can occur:

  • If the key is present in the Patch and the PatchTarget...

    • And the Patch value at that key is Nothing: delete that key from the PatchTarget.

    • And the Patch value at that key is Just x: update the value at that key in the PatchTarget to x.

  • If the key is present in the Patch and not present in the PatchTarget...

    • And the Patch value at that key is Nothing: do nothing because we're trying to delete a key that doesn't exist in the target in the first place.

    • And the Patch value at that key is Just x: insert the key and the value x into the PatchTarget

  • If the key is not present in the Patch but present in the PatchTarget: do nothing.

There are, of course, more complicated ways of patching maps involving, for example, moving values from one key to another. You can find the code for that in Data.Patch.PatchMapWithMove. Note that the PatchTarget type associated with the PatchMapWithMove patch instance is still Map k v!

patch's People

Contributors

ericson2314 avatar ali-abrar avatar luigy avatar oliver-batchelor avatar alexfmpe avatar dalaing avatar bennofs avatar ryantrinkle avatar 3noch avatar dbbnrl avatar cgibbard avatar dfordivam avatar phadej avatar mightybyte avatar endgame avatar danbornside avatar hamishmack avatar davean avatar elvishjerricco avatar maralorn avatar jbetz avatar treeowl avatar esoeylemez avatar aljce avatar tomsmalley avatar jappeace avatar cocreature avatar eskimor avatar matthewbauer avatar schell avatar

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.