Deserializing Heterogenous Data

Deserializing Heterogenous Data

Parsing and deserialize is another ubiquitous problem in modern computing, and it’s a class of problem Haskell is particularly well suited to solving.

In this chapter you’ll learn how to apply what you’ve learned so far about Functors, Applicatives, and Monads to build tools that will let you parse a variety of different types of data.

Once you’ve finished this chapter, you’ll be able to build rich and expressive libraries to work with many different kinds of data. You’ll also be able to understand how existing some of the most popular parsing libraries in Haskell are built.

In the next chapter you’ll learn about mutable data. The work that you do in this chapter will be useful both on it’s own, as well as serve as a useful stepping stone into understanding how the State monad handles mutable data.

A Configurable Status Line for HCat

Using what you’ve learned in this chapter, revisit the hcat application that you built earlier. Update hcat to support a configurable status bar that will show up at the bottom of the output. The status bar configuration should be stored on disk in a configuration file (for example ~/.config/hcat.conf). Allow users to specify which fields they want to see, what order they should appear in, and the maximum width of each field.

Hint 1

Some high level hint text

Hint 2

Some more detailed hint text

Hint 3

Even more detailed hint text

Solution

A complete solution for the exercise

Command Line Argument Parsing

Several of the examples that you’ve worked on in this book have required you to deal with command line arguments. So far, we’ve dealt with this in an ad-hoc manner. Using what you’ve learned in this chapter, try to build a library to make it easier to work with command line arguments. Use this library to improve the command line argument handling for your filepack parsing program and the hcat application you built earlier.

Hint 1

Some high level hint text

Hint 2

Some more detailed hint text

Hint 3

Even more detailed hint text

Solution

A complete solution for the exercise

Pretty Printing and Parsing

Reversible parsing is a technique that lets you write a parser that can also be used to encode data. This is particularly useful when you want to impelement parsing for files like json, XML, or our own FilePack format. A common use for reversible parsing is to implement a “pretty printing” tool that will let users re-format a document so that it’s more readable.

Using what you’ve learned in this chapter, try to implement a parser for a simple plain text format, and then make the parser reversible. Your reversible parser should reformat the original input so that it’s easier to read.

Hint 1

Some high level hint text

Hint 2

Some more detailed hint text

Hint 3

Even more detailed hint text

Solution

A complete solution for the exercise