A Complete Data-Family-Based ShellCommand
Continue with the example code from this chapter and refactor the ShellCommand module to use data families. Try to add a few more commands that wrap some of your favorite command line tools.
Better If Expressions
Earlier in the chapter you implemented conditionals inside of type families using IfThenElse . Instead of making this a single type family, try to use a mixture of GADTs and type families to explore other ways to encode conditionals that give you more flexibility, or more closely resemble term level conditionals.
Type Level map
Write a type family named Map that works like the term level map function. It should allow you to apply a function to each element of a type level list. Next, add a new type or type family so that you can add a number to each element of a type level list of naturals, as in this example:
| | λ :kind! Map ( Add 5) [0,1,2,3,4] |
| | Map ( Add 5) [0,1,2,3,4] :: [ Natural ] |
| | = '[ 5 , 6, 7, 8, 9] |
Expanded Shell Commands
Expand the GADT-based shell command DSL to support a new operation, wc , that will call the wc program to count the number of lines in a file. After you’ve added that, write a function with the type:
| | countLinesInMatchingFiles :: String -> ShellCmd FilePath [( FilePath , Int )] |
The function should take a glob that can be passed to grep , and it should return a shell command that, given a path to a directory full of files, will output the name of each file that matched with grep , along with the number of lines in that file. Be sure to only return one output for each file, even if there are multiple matches within the file.
Classless runScript
Using what you’ve learned in this chapter, experiment with other ways of defining CommandSet and runScript so that you don’t need to use type classes. What are the trade-offs you need to make?
Capstone Project: Build a Terminal Multiplexer
A terminal multiplexer is an application that lets you run different terminal applications simultaneously. The multiplexer typically gives you tools to switch between different “screens” displaying the output of different applications. They may also allow you to configure a set of applications to open automatically at startup. screen and tmux are two popular terminal multiplexers that you can look at for inspiration.
Using everything that you’ve learned in this book, write your own application that integrates a terminal multiplexer capability. For example, you might want to reimplement the hcat project that you built in the middle of this book, this time allowing users to open multiple files and switch between them, or even show multiple files side by side. Design your library so that a user writing their own terminal application can easily use your multiplexer to add their own layout and contents.
This is a large project. You will need to use most of what you’ve learned in this book, and you’ll need to learn new libraries and dive into areas we haven’t covered in this book, like multi-threaded programming. You can make this project as large or as small as you want. Whatever you decide, have fun with it! Build something for yourself, use it, improve it, and update it with new versions of GHC as they are released. A living project that you can use every day will give you the best possible experience of how to build and maintain effective Haskell code.
Copyright © 2023, The Pragmatic Bookshelf.
Thank you!
We hope you enjoyed this book and that you’re already thinking about what you want to learn next. To help make that decision easier, we’re offering you this gift.
Head on over to https://pragprog.com right now, and use the coupon code BUYANOTHER2023 to save 30% on your next ebook. Offer is void where prohibited or restricted. This offer does not apply to any edition of the The Pragmatic Programmer ebook.
And if you’d like to share your own expertise with the world, why not propose a writing idea to us? After all, many of our best authors started off as our readers, just like you. With a 50% royalty, world-class editorial services, and a name you trust, there’s nothing to lose. Visit https://pragprog.com/become-an-author/ today to learn more and to get started.
We thank you for your continued support, and we hope to hear from you again soon!
The Pragmatic Bookshelf