Skip to content

Instantly share code, notes, and snippets.

@knewter
Last active July 18, 2016 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knewter/e0f33820307054db5b5074a675e04f10 to your computer and use it in GitHub Desktop.
Save knewter/e0f33820307054db5b5074a675e04f10 to your computer and use it in GitHub Desktop.
Playing with typeclassy things, don't get mad
module Main exposing (..)
import Html.App as App
import Html exposing (..)
type Ord a
= Ord { less : a -> a -> Bool }
ordInt =
Ord { less = (<) }
ordChar =
Ord { less = (<) }
max : Ord a -> a -> a -> a
max (Ord d) x y =
if (.less d) x y then
y
else
x
maximum : Ord a -> List a -> Maybe a
maximum d list =
case list of
[ x ] ->
Just x
x :: xs ->
case maximum d xs of
Just b ->
Just (max d x b)
Nothing ->
Just x
[] ->
Nothing
typeclass =
maximum ordInt [ 1, 3, 2 ]
view model =
case typeclass of
Just a ->
div [] [ text (toString a) ]
Nothing ->
div [] [ text "shouldn't happen" ]
main =
App.beginnerProgram
{ model = ()
, view = view
, update = (\_ _ -> ())
}

Wadler

This is just me playing with this construct from Wadler: https://youtu.be/8frGknO8rIg?t=1533

I know we don't have typeclasses. But it struck me that I should be able to build this. So I did.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment