Created
April 10, 2016 22:35
-
-
Save chuck0523/f633fd1c8637ba7b065d3637ecdad21f to your computer and use it in GitHub Desktop.
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
import Graphics.Element exposing (...) | |
import Text | |
-- either(http://elm-lang.org/examples/either) | |
{-| | |
複数の型を合体させるユニオンタイプ。 | |
-} | |
type Either a b | |
= Left a | |
| Right b | |
{-| | |
ユーザーIDを定義する。 | |
ユーザーIDは数値型と文字列型から為る。 | |
-} | |
userIDs : List (Either Int String) | |
userIDs = | |
[Left 42, Right "12A3BC", Left 1337, Right "ZA7T9G"] | |
{-| | |
Left型の値と、Right型の値に分別する。 | |
-} | |
partition : List (Either a b) -> (List a, List b) | |
partition eithers = | |
case eithers of | |
[] -> | |
([], []) | |
Left a :: rest -> | |
let | |
(lefts, rights) = partition rest | |
in | |
(a :: lefts, rights) | |
Right b :: rest -> | |
let | |
(lefts, rights) = partition rest | |
in | |
(lefts, b :: rights) | |
main = | |
show (partition userIDs) -- ([42,1337],["12A3BC","ZA7T9G"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment