From 07e012a3b668c31a216e698e56f8d8cfcc542497 Mon Sep 17 00:00:00 2001 From: Rehno Lindeque Date: Tue, 8 Mar 2016 00:23:45 +0000 Subject: [PATCH] Add some demo snippets from the IHaskell documentation --- licenses/LICENSE-IHaskell-demo-mit | 20 ++++ notebooks/demo.ipynb | 179 +++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 licenses/LICENSE-IHaskell-demo-mit create mode 100644 notebooks/demo.ipynb diff --git a/licenses/LICENSE-IHaskell-demo-mit b/licenses/LICENSE-IHaskell-demo-mit new file mode 100644 index 0000000..869533f --- /dev/null +++ b/licenses/LICENSE-IHaskell-demo-mit @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 Andrew Gibiansky + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/notebooks/demo.ipynb b/notebooks/demo.ipynb new file mode 100644 index 0000000..0390493 --- /dev/null +++ b/notebooks/demo.ipynb @@ -0,0 +1,179 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "hidden": false + }, + "source": [ + "Some demo snippets adapted from IHaskell documentation\n", + "===" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "-- Enable some extensions\n", + ":ext OverloadedStrings\n", + ":ext NoMonomorphismRestriction\n", + ":ext TypeFamilies\n", + ":ext FlexibleContexts\n", + "\n", + "-- Basics\n", + "import Prelude hiding (div, id)\n", + "import Data.Default.Class\n", + "import Control.Lens hiding ((#))\n", + "import Data.Traversable (forM)\n", + "\n", + "-- Display\n", + "import IHaskell.Display as Display\n", + "import Text.Blaze.Html4.Strict as Blaze hiding (style, map)\n", + "import Text.Blaze.Html4.Strict.Attributes as Blaze\n", + "import Diagrams.Prelude as Diagrams\n", + "import Graphics.Rendering.Chart as Chart" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "data Color = Red | Green | Blue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "instance IHaskellDisplay Color where\n", + " display color = return $ Display [Display.html code]\n", + " where\n", + " code = concat [\"
Look!
\"]\n", + " css Red = \"red\"\n", + " css Blue = \"blue\"\n", + " css Green = \"green\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "Red\n", + "Green\n", + "Blue" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "-- Small bits of HTML generated via Blaze are displayed.\n", + "div ! Blaze.style \"color: red\" $ do\n", + " p \"This is an example of BlazeMarkup syntax.\"\n", + " b \"Hello\"\n", + " \n", + "forM [1..3] $ \\size -> do\n", + " let s = Blaze.toValue $ size * 70\n", + " img ! src \"https://www.google.com/images/srpr/logo11w.png\" ! Blaze.width s" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "-- By Brent Yorgey\n", + "-- Draw a Sierpinski triangle!\n", + "sierpinski 1 = eqTriangle 1\n", + "sierpinski n = s\n", + " ===\n", + " (s ||| s) # centerX\n", + " where s = sierpinski (n-1)\n", + "\n", + "-- The `diagram` function is used to display them in the notebook.\n", + "diagram $ sierpinski 4\n", + " # centerXY\n", + " # fc black\n", + " `atop` square 10\n", + " # fc white" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "-- We can draw small charts in the notebook.\n", + "-- This example is taken from the haskell-chart documentation.\n", + "\n", + "let values = [\n", + " (\"Mexico City\" , 19.2, 0),\n", + " (\"Mumbai\" , 12.9, 10), \n", + " (\"Sydney\" , 4.3, 0),\n", + " (\"London\" , 8.3, 0), \n", + " (\"New York\" , 8.2, 25)]\n", + " \n", + "pitem (s, v, o) = pitem_value .~ v\n", + " $ pitem_label .~ s\n", + " $ pitem_offset .~ o\n", + " $ def \n", + "\n", + "-- Convert to a renderable in order to display it.\n", + "toRenderable \n", + " $ pie_title .~ \"Relative Population\"\n", + " $ pie_plot . pie_data .~ map pitem values\n", + " $ def" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Haskell", + "language": "haskell", + "name": "haskell" + }, + "language_info": { + "codemirror_mode": "ihaskell", + "file_extension": ".hs", + "name": "haskell", + "version": "7.10.3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}