Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Created April 10, 2016 22:35
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 chuck0523/f633fd1c8637ba7b065d3637ecdad21f to your computer and use it in GitHub Desktop.
Save chuck0523/f633fd1c8637ba7b065d3637ecdad21f to your computer and use it in GitHub Desktop.
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