Skip to content

Instantly share code, notes, and snippets.

@ossan-pg
Last active May 5, 2019 04:17
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 ossan-pg/243b417961bff80491d4ee1af4f40970 to your computer and use it in GitHub Desktop.
Save ossan-pg/243b417961bff80491d4ee1af4f40970 to your computer and use it in GitHub Desktop.
Elm の List モジュールの関数を自作する
module MyList exposing (..)
-- original: List.repeat
repeat : Int -> a -> List a
repeat idx value =
if idx <= 0 then
[]
else
value :: (repeat (idx - 1) value)
-- original: List.range
range : Int -> Int -> List Int
range min max =
if max - min < 0 then
[]
else
min :: (range (min + 1) max)
-- original: List.reverse
reverse : List a -> List a
reverse aList =
case aList of
[] ->
[]
x :: xs ->
-- 性能的にダメそう
(reverse xs) ++ [ x ]
-- original: List.map
map : (a -> b) -> List a -> List b
map f aList =
case aList of
[] ->
[]
x :: xs ->
(f x) :: (map f xs)
-- original: List.indexedMap
indexedMap : (Int -> a -> b) -> List a -> List b
indexedMap f aList =
indexedMapHelper 0 f aList
indexedMapHelper : Int -> (Int -> a -> b) -> List a -> List b
indexedMapHelper idx f aList =
case aList of
[] ->
[]
x :: xs ->
(f idx x) :: (indexedMapHelper (idx + 1) f xs)
-- original: List.foldl
foldl : (a -> b -> b) -> b -> List a -> b
foldl f v aList =
case aList of
[] ->
v
x :: xs ->
foldl f (f x v) xs
-- original: List.foldr
foldr : (a -> b -> b) -> b -> List a -> b
foldr f v aList =
case aList of
[] ->
v
x :: xs ->
f x <| foldr f v xs
-- original: List.length
length : List a -> Int
length aList =
case aList of
[] ->
0
x :: xs ->
(length xs) + 1
-- original: List.head
head : List a -> Maybe a
head aList =
case aList of
[] ->
Nothing
x :: _ ->
Just x
-- original: List.filter
filter : (a -> Bool) -> List a -> List a
filter f aList =
case aList of
[] ->
[]
x :: xs ->
if (f x) then
x :: (filter f xs)
else
(filter f xs)
-- original: List.filterMap
filterMap : (a -> Maybe b) -> List a -> List b
filterMap f aList =
case aList of
[] ->
[]
x :: xs ->
case (f x) of
Just v ->
v :: (filterMap f xs)
Nothing ->
(filterMap f xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment