on 294fdfd help sign in
elk/libraries/List.elm
132 lines 3.7 kB history raw
2module List where
4import Native.Utils (min, max)
5import Native.List as L
7-- Extract the first element of a list. List must be non-empty.
8head : [a] -> a
10-- Extract the elements after the head of the list. List must be non-empty.
11tail : [a] -> [a]
13-- Extract the last element of a list. List must be non-empty.
14last : [a] -> a
16-- Apply a function to every element of a list.
17map : (a -> b) -> [a] -> [b]
19-- Reduce a list from the left.
20foldl : (a -> b -> b) -> b -> [a] -> b
22-- Reduce a list from the right.
23foldr : (a -> b -> b) -> b -> [a] -> b
25-- Reduce a list from the left without a base case. List must be non-empty.
26foldl1 : (a -> a -> a) -> [a] -> a
28-- Reduce a list from the right without a base case. List must be non-empty.
29foldr1 : (a -> a -> a) -> [a] -> a
31-- Reduce a list from the left, building up all of the intermediate results into a list.
32scanl : (a -> b -> b) -> b -> [a] -> [b]
34-- Same as scanl but it doesn't require a base case. List must be non-empty.
35scanl1 : (a -> a -> a) -> [a] -> [a]
37-- Filter out elements which do not satisfy the predicate.
38filter : (a -> Bool) -> [a] -> [a]
40-- Determine the length of a list.
41length : [a] -> Int
43-- Reverse a list.
44reverse : [a] -> [a]
46-- Check to see if all elements satisfy the predicate.
47all : (a -> Bool) -> [a] -> Bool
49-- Check to see if any elements satisfy the predicate.
50any : (a -> Bool) -> [a] -> Bool
52-- Check to see if all elements are True.
53and : [Bool] -> Bool
55-- Check to see if any elements are True.
56or : [Bool] -> Bool
58-- Flatten a list of lists.
59concat : [[a]] -> [a]
61-- Map a given function onto a list and flatten the resulting lists. (concatMap f xs == concat (map f xs))
62concatMap : (a -> [b]) -> [a] -> [b]
63concatMap f = L.concat . L.map f
65-- Get the sum of the list elements.
66sum = L.foldl (+) 0
68-- Get the product of the list elements.
69product = L.foldl (*) 1
71-- Find the highest number in a non-empty list.
72maximum = L.foldl1 max
74-- Find the lowest number in a non-empty list.
75minimum = L.foldl1 min
77-- Split a list based on the predicate.
78partition : (a -> Bool) -> [a] -> ([a],[a])
79partition pred lst =
80 case lst of
81 [] -> ([],[])
82 x::xs -> let (bs,cs) = partition pred xs in
83 if pred x then (x::bs,cs) else (bs,x::cs)
85-- Combine two lists, combining them into tuples pairwise. If one input list has extra elements (it is longer), those elements are dropped.
86zip : [a] -> [b] -> [(a,b)]
88-- Combine two lists, combining them with the given function. If one input list has extra elements (it is longer), those elements are dropped.
89zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
91-- Decompose a list of tuples
92unzip : [(a,b)] -> ([a],[b])
93unzip pairs =
94 case pairs of
95 [] -> ([],[])
96 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
98-- Split a list
99-- split "," "hello,there,friend" == ["hello", "there", "friend"]
100split : [a] -> [a] -> [[a]]
102-- Places the given value between all of the lists in the second argument and concatenates the result.
103-- join xs xss = concat (intersperse xs xss)
104join : [a] -> [[a]] -> [a]
106-- Places the given value between all members of the given list.
107intersperse : a -> [a] -> [a]
108intersperse sep xs =
109 case xs of
110 a::b::cs -> a :: sep :: intersperse sep (b::cs)
111 [a] -> [a]
112 [] -> []
114{-- TODO: only found in docs
116-- Add an element to the front of a list
117-- a :: [b,c] = [a,b,c]
118(::) : a -> [a] -> [a]
120-- Appends two lists.
121(++) : [a] -> [a] -> [a]
123-- Take the first n members of a list.
124-- take 2 [1,2,3,4]) ==> [1,2]
125take : Int -> [a] -> [a]
127-- Drop the first n members of a list.
128-- drop 2 [1,2,3,4]) ==> [3,4]
129drop : Int -> [a] -> [a]
131--}