Module Prime_list
Amendment to the standard library List
structure.
This structure does not include the original functions. For a full replacement include Unprime_list
.
val count : ('a -> bool) -> 'a list -> int
count f xs
is the number of elementsx
ofxs
for whichf x
holds.
val search : ('a -> 'b option) -> 'a list -> 'b option
search f xs
returns the first element ofmap f xs
which is different fromNone
orNone
if all elements areNone
. This is an alternative tofind
which is easy to nest, e.g. the functionPrime_array.search (Prime_option.search (Prime_list.search f))
returns the first non-None
mapping off
in an array of optional lists.
Comparison
Iteration
val fold : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
fold f [x₁; …; xₙ]
returns the compositionf xₙ ∘ ⋯ ∘ f x₁
. This isfold_left
with parameters reordered to make it more composable.
val fmap : ('a -> 'b option) -> 'a list -> 'b list
val filter_map : ('a -> 'b option) -> 'a list -> 'b list
filter_map f xs
is the list of ally
such thatf x = Some y
for somex
inxs
, and having the same order as the corresponding elemets ofxs
. This provides an optimisation ofmap Option.get (filter ((<>) None) (map f xs))
.
Iteration over Two Lists
val iter2t : ('a -> 'b -> unit) -> 'a list -> 'b list -> unit
iter2t f [x₁; …; xₙ] [y₁; …; yₘ]
callsf x₁ y₁; …; f xₗ yₗ
in order, wherel = min n m
. This is a truncating variant ofiter2
from the standard library.
val map2t : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
map2t f [x₁; …; xₙ] [y₁; …; yₘ]
returns[f x₁ y₁; ⋯; f xₗ yₗ]
wherel = min n m
. Thet
suffix indicates truncation, otherwise this behaves likemap2
from the standard library.
val rev_map2t : ('a -> 'b -> 'c) -> 'a list -> 'b list -> 'c list
rev_map2t f
is an optimisation ofrev ∘ map2t f
.
Differential Iteration
Sublists
val drop : int -> 'a list -> 'a list
drop n xs
returns the suffix ofxs
following then
first elements. RaisesFailure
ifxs
is shorter thann
elements and Invalid_argument ifn
is negative.
val take : int -> 'a list -> 'a list
take n xs
returns then
-element prefix ofxs
. RaisesFailure
ifxs
is shorter thann
elements and Invalid_argument ifn
is negative.