Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iokasimov/apart
Get all your structure and rip it apart.
https://github.com/iokasimov/apart
algebraic cofree persistence structures
Last synced: 2 months ago
JSON representation
Get all your structure and rip it apart.
- Host: GitHub
- URL: https://github.com/iokasimov/apart
- Owner: iokasimov
- License: bsd-3-clause
- Created: 2018-04-10T09:46:45.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-21T13:52:15.000Z (about 6 years ago)
- Last Synced: 2024-03-25T05:02:15.492Z (10 months ago)
- Topics: algebraic, cofree, persistence, structures
- Language: Haskell
- Homepage: http://hackage.haskell.org/package/apart
- Size: 106 KB
- Stars: 29
- Watchers: 4
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```haskell
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):
```haskell
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?
```haskell
save_to_file :: FilePath -> Segment Stack Int -> IO FilePath
save_to_file fp structure = writeFile fp (show structure) *> pure fpscattered = IO (Scattered Stack Int FilePath)
scattered = limit 2 (save_to_file "part.txt") inmemory
```
And our structure transformed into:
```haskell
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:
```haskell
fluently :: IO (Stack ())
fluently = fluent print read_from_file scattered
```## Recover scattered structure
Return back to memory our stack of integers:
```haskell
read_from_file :: FilePath -> IO (Segment Stack Int)
read_from_file fp = read @(Segment Stack Int) <$> readFile fpinmemory :: IO (Stack Int)
inmemory = recover read_from_file scattered
```