This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
elm-applicatives-and-json-decoders/demo/MaybeApplicative.elm
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (32 sloc)
890 Bytes
This file contains 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 MaybeApplicative exposing (..) | |
import Html exposing (Html, code, div, h1, h2, text) | |
import Maybe.Extra as Maybe | |
{-| Infix for Maybe.andMap which is the same as apply | |
In Haskell: | |
(<*>) :: Applicative f => f (a -> b) -> f a -> f b | |
Which takes in Applicative (because of polymorphism)--not just Maybe | |
-} | |
infixl 2 <*> | |
(<*>) : Maybe (a -> b) -> Maybe a -> Maybe b | |
(<*>) = | |
flip Maybe.andMap | |
{-| define the pure/singleton for the our applicative, Maybe | |
-} | |
singleton : a -> Maybe a | |
singleton = | |
Just | |
view : a -> Html String | |
view x = | |
div [] | |
[ h1 [] | |
[ code [] [ text "singleton (+) <*> Just 1 <*> Just 2" ] | |
, text " :" | |
] | |
, h2 [] [ text <| toString x ] | |
] | |
{-| Same as Just (+) `Maybe.andMap` Just 1 `Maybe.andMap` Just 2 | |
-} | |
main : Html String | |
main = | |
view <| singleton (+) <*> Just 1 <*> Just 2 | |
--=> Just 3 |