Home

Awesome

PureScript-Cookbook

An unofficial Cookbook for PureScript (Quick link to recipes table)

The recipes folder contains all the recipes in this repo in no particular order.

If you want to contribute a new recipe, see the CONTRIBUTING.md file.

Usage

Install Dependencies

Install GNU Make (version 4.0 or above), and verify installation with:

make --version

Install all dependencies locally:

make installDeps

Install for Nix users:

# Remove purescript and spago and removing trailing , from package.json
sed --in-place '/purescript\|spago/d' ./package.json && sed --in-place '$!N;s/,\n  }/\n  }/;P;D' package.json
nix-shell

Running Recipes

See a list of available recipe launch commands by running make list:

> make list
Use "make RecipeName-target" to run a recipe

=== Node Recipes ===
make ReadPrintFileContents-node
make HelloLog-node
...

=== Web Recipes ===
make WindowProperties-web
make HelloLog-web
...

Then simply copy and paste one of the listed commands. For example:

Running a node-compatible recipe:

> make HelloLog-node
=== Running HelloLog on the Node.js backend ===
[info] Installation complete.
[info] Build succeeded.
Hello world!

Running a web-compatible recipe:

> make HelloLog-web
=== Building HelloLog ===
[info] Installation complete.
[info] Build succeeded.
=== Launching HelloLog in the web browser ===
...

Current Recipe Suffixes

Recipes ending with ...... mean the following approach/library is used
HalogenClassicComponent-style Halogen
HalogenHooksHooks-style Halogen
ReactClassicComponent-style React via react-basic
ReactHooksHooks-style React via react-basic
ConcurConcur
JsRun plain PureScript only on the web (no node.js) without a web framework
LogLog content to both the browser's dev console and the terminal
NodeRun PureScript only on Node.js where no user interaction occurs
CLIRun PureScript only on Node.js with user interaction

Recipes

Note: for the try links in the "Web Browser" column, any content logged to the console will appear in the web browser's console, not in the page rendered by Try PureScript.

NodeWeb BrowserRecipeDescription
:heavy_check_mark: (try - fixme)AceEditorHalogenHooksA Halogen Hooks port of the "Ace Editor" Halogen Example.
:heavy_check_mark: (try - fixme)AddRemoveEventListenerJsThis recipe shows how to add and remove an event listener to an HTML element.
:heavy_check_mark:AffjaxPostNodePerforms a simple HTTP Post request using the Affjax library.
:heavy_check_mark: (try)BasicHalogenHooksDisplays a button that toggles the label to "On" and "Off".
:heavy_check_mark::heavy_check_mark: (try)BigIntJsThis recipe shows how to print, create, and use values of the BigIntJs type in either the node.js or web browser console.
:heavy_check_mark: (try)BookHalogenHooksA Halogen port of the "HTTP - Book" Elm Example.
:heavy_check_mark: (try)BookReactHooksA React port of the "HTTP - Book" Elm Example.
:heavy_check_mark: (try)ButtonsHalogenHooksA Halogen port of the "User Input - Buttons" Elm Example.
:heavy_check_mark: (try)ButtonsReactHooksA React port of the "User Input - Buttons" Elm Example.
:heavy_check_mark:CapabilityPatternNodeA skeletal version of an application structuring pattern
:heavy_check_mark: (try)CardsHalogenHooksA Halogen port of the "Random - Cards" Elm Example.
:heavy_check_mark: (try)CardsReactHooksA React port of the "Random - Cards" Elm Example.
:heavy_check_mark: (try)ClockReactHooksA React port of the "User Interface - Clock" Elm Example.
:heavy_check_mark: (try)ComponentsHalogenHooksDemonstrates how to nest one Halogen-Hooks-based component inside another and send/receive queries between the two.
:heavy_check_mark: (try)ComponentsInputHalogenHooksEach time a parent re-renders, it will pass a new input value into the child, and the child will update accordingly.
:heavy_check_mark: (try)ComponentsInputReactHooksEach time the parent's state updates, it will pass a new prop value into the child, and the child will update accordingly.
:heavy_check_mark: (try)ComponentsMultiTypeHalogenHooksDemonstrates a component that can communicate with its children that have differing types.
:heavy_check_mark: (try)ComponentsMultiTypeReactHooksDemonstrates a parent component with several children components, each with different prop types.
:heavy_check_mark: (try)ComponentsReactHooksDemonstrates how to nest one React Hooks-based component inside another and send props from the parent to the child component.
:heavy_check_mark::heavy_check_mark: (try)DateTimeBasicsLogThis recipe shows how to use purescript-datetime library to create Time, Date, and DateTime values and adjust/diff them.
:heavy_check_mark::heavy_check_mark: (try)DebuggingLogThis recipe shows how to do print-debugging using the Debug module's spy and traceM functions. The compiler will emit warnings to remind you to remove these debug functions before you ship production code.
:heavy_check_mark:DiceCLIThis recipe shows how to create an interactive command line prompt that repeatedly generates a random number between 1 and 6.
:heavy_check_mark::heavy_check_mark: (try)DiceLogThis recipe shows how to log a random integer between 1 and 6 (representing a roll of a die) in either the node.js or web browser console.
:heavy_check_mark: (try)DogImagesHalogenHooksFetches random dog images from the Dog API.
:heavy_check_mark: (try)DogImagesReactHooksFetches random dog images from the Dog API.
:heavy_check_mark: (try)DragAndDropHalogenHooksA Halogen port of the "Files - Drag-and-Drop" Elm Example.
:heavy_check_mark: (try)DragAndDropReactHooksA React port of the "Files - Drag-and-Drop" Elm Example.
:heavy_check_mark: (try)DriverIoHalogenHooksDemonstrates how to communicate with a Halogen application by sending messages to and receiving messages from the root-level component via the driver.
:heavy_check_mark:DriverRoutingHalogenHooksDemonstrates using hashchange events to drive the root component in a Halogen application via the driver.
:heavy_check_mark: (try)DriverWebSocketsHalogenHooksDemonstrates using a WebSocket to drive the main component in a Halogen application.
:heavy_check_mark: (try)FileUploadHalogenHooksA Halogen port of the "Files - Upload" Elm Example.
:heavy_check_mark: (try)FileUploadReactHooksA React port of the "Files - Upload" Elm Example.
:heavy_check_mark:FindDomElementJsThis recipe shows how to find elements in the DOM by using query selectors.
:heavy_check_mark: (try)FormsReactHooksA React port of the "User Interface - Forms" Elm Example.
:heavy_check_mark: (try)GroceriesHalogenHooksA Halogen port of the "HTML - Groceries" Elm Example.
:heavy_check_mark: (try)GroceriesJsA framework-free port of the "HTML - Groceries" Elm Example.
:heavy_check_mark: (try)GroceriesReactHooksA React port of the "HTML - Groceries" Elm Example.
:heavy_check_mark: (try)HelloHalogenHooksA Halogen port of the "HTML - Hello" Elm Example.
:heavy_check_mark: (try)HelloJsA framework-free port of the "HTML - Hello" Elm Example.
:heavy_check_mark::heavy_check_mark: (try)HelloLogThis recipe shows how to run a simple "Hello world!" program in either the node.js or web browser console.
:heavy_check_mark: (try)HelloReactHooksA React port of the "HTML - Hello" Elm Example.
:heavy_check_mark::heavy_check_mark: (try)HeterogeneousLogThis recipe demonstrates how to use purescript-heterogeneous to manipulate records generically.
:heavy_check_mark::heavy_check_mark: (try)HeterogenousArrayLogThis recipe demonstrates how to create a heterogenous array and process its elements.
:heavy_check_mark: (try - fixme)ImagePreviewsHalogenHooksA Halogen port of the "Files - Drag-and-Drop" Elm Example with an additional feature to display image thumbnails.
:heavy_check_mark: (try)ImagePreviewsReactHooksA React port of the "Files - Image-Previews" Elm Example.
:heavy_check_mark: (try)InterpretHalogenHooksDemonstrates how to use a custom monad (in this case, using ReaderT with Aff as the effect type) for a component, and then interpreting that custom monad back down to Aff, so it can be run as a normal component.
:heavy_check_mark: (try)KeyboardInputHalogenHooksThis example demonstrates how to selectively capture keyboard events and, more generally, how to use EventSources in Halogen.
:heavy_check_mark: (try)LifecycleHalogenHooksDemonstrates component lifecycle.
:heavy_check_mark: (try)NumbersHalogenHooksA Halogen port of the "Random - Numbers" Elm Example.
:heavy_check_mark: (try)NumbersReactHooksA React port of the "Random - Numbers" Elm Example.
:heavy_check_mark::heavy_check_mark: (try)ParallelAppMExampleLogDemonstrates how to use parSequence/parTraverse and how to define a Parallel instance for a ReaderT r Aff-based AppM monad.
:heavy_check_mark: (try)PositionsHalogenHooksA Halogen port of the "Random - Positions" Elm Example.
:heavy_check_mark: (try)PositionsReactHooksA React port of the "Random - Positions" Elm Example.
:heavy_check_mark:RandomNumberGameNodeThis recipe shows how to build a "guess the random number" game using a custom AppM monad via the ReaderT design pattern and Aff, storing the game state in a mutable variable via a Ref.
:heavy_check_mark:ReadPrintFileContentsNodeReads a file's contents and prints it to the console.
:heavy_check_mark:RoutingHashHalogenClassicThis recipe shows how to use purescript-routing to do client-side hash-based routing in a Halogen-based single-page application (SPA).
:heavy_check_mark:RoutingHashLogThis recipe demonstrates hash-based routing with purescript-routing. No web framework is used.
:heavy_check_mark:RoutingHashReactHooksThis recipe shows how to use purescript-routing to do client-side hash-based routing in a React-based single-page application (SPA).
:heavy_check_mark:RoutingPushHalogenClassicThis recipe shows how to use purescript-routing to do client-side push-state routing in a Halogen-based single-page application (SPA).
:heavy_check_mark:RoutingPushReactHooksThis recipe shows how to use purescript-routing to do client-side push-state routing in a React-based single-page application (SPA).
:heavy_check_mark:RunCapabilityPatternNodeA skeletal version of an application structuring pattern using purescript-run and free dsls.
:heavy_check_mark: (try)ShapesHalogenHooksDemonstrates rendering of SVG shapes.
:heavy_check_mark: (try)ShapesReactHooksDemonstrates rendering of SVG shapes.
:heavy_check_mark: (try)SignalRenderJsSignal demo that responds to user input and elapsed time.
:heavy_check_mark: (try)SignalSnakeJsA snake game built using Signal.
:heavy_check_mark: (try)SignalTrisJsA tetromino game built using Signal.
:heavy_check_mark::heavy_check_mark: (try)SimpleASTParserLogThis recipe shows how to parse and evaluate a math expression using parsers and a "precedence climbing" approach.
:heavy_check_mark: (try)TextFieldsHalogenHooksA Halogen port of the "User Interface - Text Fields" Elm Example.
:heavy_check_mark: (try)TextFieldsReactHooksA React port of the "User Interface - Text Fields" Elm Example.
:heavy_check_mark: (try - fixme)TicTacToeReactHooksA PureScript port of the official reactjs.org documentation's Tutorial: Intro to React example.
:heavy_check_mark: (try)TimeHalogenHooksA Halogen port of the "Time - Time" Elm Example.
:heavy_check_mark: (try)TimeReactHooksA React port of the "User Interface - Time" Elm Example.
:heavy_check_mark::heavy_check_mark: (try)ValueBasedJsonCodecLogThis recipe shows how to use codec and codec-argonaut to write value-based bidirectional JSON codecs to encode and decode examples written in "meta-language."
:heavy_check_mark: (try)WindowPropertiesJsThis recipe shows how to get and print various properties in the browser's window object.
:heavy_check_mark:WriteFileNodeWrites a String to a text file using UTF-8 encoding.