Git Product home page Git Product logo

multiwalk's Introduction

MultiWalk

This library provides functionality for traversing data types recursively, acting on multiple types during the same traversal. In spirit, it is similar to the Walk type class from Pandoc.Walk, but generalizes it by allowing multiple types to be targeted by the traversal. Also, by default it only requires an Applicative constraint on the action, making it suitable for situations where you don't have a Monad (for instance, traversing within a composition of monads).

Say, for instance, you want to query all code snippets from a Pandoc document, including both Inline and Block ones, such that the list of results is in the same order as they appear in the document. There is no way to do this with Pandoc's Walk type class, because it only supports querying or walking with one type at once. With MultiWalk you are able to do this with a single query function that targets both types, and it'll look something like this:

multi :: Block -> [Text]
multi = buildMultiQ @PTag $ \sub list ->
    list ?> blks sub
         ?> inls sub
  where
    blks _ (CodeBlock _ c) = [c]
    blks f x = f x
    inls _ (Code _ c) = [c]
    inls f x = f x

(note, however, that this library does not ship with default instances for Pandoc, so you will have to define them yourself. You can find a basic Pandoc instance for reference, and the function above, in Benchmark.hs inside the test directory.)

Another use case is when you want to modify a data type, perhaps targeting multiple subtypes, and you want to do that inside a functor that is Applicative but not a Monad. Such functors may sound unusual, but one of the interesting places where they appear are in composition of monads, which need not be a monad itself.

You can find a use of such monad compositions in my other library here. It's used for resolving cross-references inside documents inside the functor Compose M F, where M is a state monad which holds the links it has found so far, and F is a reader which receives the final, future state (that will only be ready at the end of the computation). In this way I can walk across the document only once, registering and applying cross references at the same time. Note that this is composition is semantically different from using monad transformers, because they would require the reader state to be supplied before the final state is available.

multiwalk's People

Stargazers

Daniel Kahlenberg avatar Ilmari Vacklin avatar Sridhar Ratnakumar avatar

Watchers

Sridhar Ratnakumar avatar Lucas Viana Reis avatar

multiwalk's Issues

Consider dropping Query

Queries can be implemented from Walks by using a Writer monad, so there should only make sense to keep it if there is a significant performance penalty, or if someone pops up here to correct me.

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.