Created
February 8, 2022 21:15
-
-
Save gasi/fbf3017a86d58cdcb6529e57bcbe0c7a to your computer and use it in GitHub Desktop.
PureScript: Pattern matching and @ syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Prelude | |
import Data.Maybe (Maybe(..)) | |
import Effect (Effect) | |
import Effect.Console (log) | |
data Dish = Dish Pasta (Maybe Sauce) | |
dishToString :: Dish -> String | |
dishToString _ = "I’m a dish" | |
data Sauce | |
= Marinara { spicy :: Boolean } | |
| Carbonara | |
| Pesto | |
sauceToString :: Sauce -> String | |
sauceToString (Marinara { spicy }) = "marinara (spicy: " <> show spicy <> ")" | |
sauceToString Carbonara = "carbonara" | |
sauceToString Pesto = "pesto" | |
instance Show Sauce where | |
show = sauceToString | |
data Pasta | |
= Spaghetti | |
| Linguine | |
| Macaroni | |
pastaToString :: Pasta -> String | |
pastaToString Spaghetti = "spaghetti" | |
pastaToString Linguine = "linguine" | |
pastaToString Macaroni = "macaroni" | |
main :: Effect Unit | |
main = do | |
let | |
-- dish1 = Dish Linguine (Just Carbonara) | |
-- dish2 = Dish Macaroni (Just (Marinara { spicy: true })) | |
dish3 = Dish Macaroni (Just (Marinara { spicy: false })) | |
case dish3 of | |
Dish pasta Nothing -> do | |
log ("Pasta " <> pastaToString pasta <> " without sauce") | |
Dish _ (Just (Marinara { spicy })) | spicy -> do | |
-- log ("Spicy marinara: " <> show (Just (Marinara { spicy: true }))) | |
log ("Spicy marinara: " <> show spicy) | |
Dish pasta outerSauce@(Just sauce) -> do | |
log ("Pasta " <> pastaToString pasta <> " with " <> sauceToString sauce <> " sauce") | |
log ("Sauce: " <> show outerSauce) | |
-- log ("Sauce: " <> show (Just sauceToString sauce)) | |
isSpicy :: Sauce -> Boolean | |
isSpicy (Marinara { spicy }) = spicy | |
isSpicy Pesto = false | |
isSpicy Carbonara = false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment