Git Product home page Git Product logo

apart's Introduction

Get all your structure and rip it apart πŸ”ͺ

The main idea: if you can describe your data structure via Cofree, you got:

  • Comonadic methods, you always can extract a focus value or extend with some function
  • With apart you can serialize, persistent or hash a segment of your structure!

Example usage

Let's define a simple non-empty list type:

type Stack a = Cofree Maybe a

Then, build a value of this type, stack of integers, the whole structure in memory (ignore lazy evaluation aspects now):

inmemory :: Stack Int
inmemory = 1 :< Just (2 :< Just (3 :< Just (4 :< Just (5 :< Nothing))))

Set a limit for structure

Sometimes, we don’t need to hold a whole structure in memory, can it be good just cut a part of it and save to file?

save_to_file :: FilePath -> Segment Stack Int -> IO FilePath
save_to_file fp structure = writeFile fp (show structure) *> pure fp

scattered = IO (Scattered Stack Int FilePath)
scattered = limit 2 (save_to_file "part.txt") inmemory

And our structure transformed into:

scattered :: Scattered Stack Int FilePath
scattered = Apart $ 1 :< Ready (Just $ 2 :< Ready (Just $ 3 :< Converted "part.txt"))

Traverse over scattered structure

We also can fluently traverse over scattered structure with action and function for recover a segment:

fluently :: IO (Stack ())
fluently = fluent print read_from_file scattered

Recover scattered structure

Return back to memory our stack of integers:

read_from_file :: FilePath -> IO (Segment Stack Int)
read_from_file fp = read @(Segment Stack Int) <$> readFile fp

inmemory :: IO (Stack Int)
inmemory = recover read_from_file scattered

apart's People

Contributors

iokasimov avatar

Watchers

 avatar  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.