text
stringlengths
0
601k
let take t ~ count = match Belt . List . take t count with | None -> if count <= 0 then [ ] else t | Some v -> v
let initial l = match reverse l with [ ] -> None | _ :: rest -> Some ( reverse rest ) rest
let filterMap t ~ f = Belt . List . keepMap t f
let filter t ~ f = Belt . List . keep t f
let filterWithIndex t ~ f = Belt . List . keepWithIndex t ( fun e i -> f i e ) e
let partition t ~ f = Belt . List . partition t f
let fold t ~ initial ~ f = Belt . List . reduce t initial f
let count t ~ f = fold t ~ initial : 0 ~ f ( : fun total element -> total + match f element with true -> 1 | false -> 0 )
let foldRight t ~ initial ~ f = Belt . List . reduceReverse t initial f
let findIndex list ~ f = let rec loop i l = match l with | [ ] -> None | x :: rest -> if f i x then Some ( i , x ) x else loop ( i + 1 ) 1 rest in loop 0 list
let splitAt t ~ index = ( take ~ count : index t , drop ~ count : index t ) t
let updateAt = ( fun t ~ index ~ f -> Belt . List . mapWithIndex t ( fun i element -> if i = index then f element else element ) : ' a t -> index : int -> f ( : ' a -> ' a ) ' a -> ' a t )
let length l = Belt . List . length l
let rec dropWhile t ~ f = match t with [ ] -> [ ] | x :: rest -> if f x then dropWhile rest ~ f else t
let isEmpty t = t = [ ]
let sliding ( ? step = 1 ) 1 t ~ size = let rec loop t = if isEmpty t then [ ] else let sample = Belt . List . take t size in let rest = Belt . List . drop t step in match ( sample , rest ) rest with | None , _ -> [ ] | Some x , None -> [ x ] | Some x , Some xs -> x :: loop xs in loop t
let chunksOf t ~ size = sliding t ~ step : size ~ size
let cons t element = element :: t
let takeWhile t ~ f = let rec takeWhileHelper acc t = match t with | [ ] -> reverse acc | x :: rest -> if f x then takeWhileHelper ( x :: acc ) acc rest else reverse acc in takeWhileHelper [ ] t
let all t ~ f = Belt . List . every t f
let tail t = match t with [ ] -> None | _ :: rest -> Some rest
let removeAt t ~ index = if index < 0 then t else let ( front , back ) back : ' a t * ' a t = splitAt t ~ index in match tail back with None -> t | Some t -> append front t
let minimumBy ( ~ f : ' a -> ' comparable ) ' comparable ( l : ' a list ) list : ' a option = let minBy ( y , fy ) fy x = let fx = f x in if fx < fy then ( x , fx ) fx else ( y , fy ) fy in match l with | [ ] -> None | [ x ] -> Some x | x :: rest -> Some ( fst ( fold ~ f : minBy ~ initial ( : x , f x ) x rest ) rest ) ...
let maximumBy ( ~ f : ' a -> ' comparable ) ' comparable ( l : ' a list ) list : ' a option = let maxBy ( y , fy ) fy x = let fx = f x in if fx > fy then ( x , fx ) fx else ( y , fy ) fy in match l with | [ ] -> None | [ x ] -> Some x | x :: rest -> Some ( fst ( fold ~ f : maxBy ~ initial ( : x , f x ) x rest ) rest ) ...
let minimum t ~ compare = fold t ~ initial : None ~ f ( : fun min element -> match min with | None -> Some element | Some value -> ( match compare element value < 0 with | true -> Some element | false -> min ) )
let maximum t ~ compare = fold t ~ initial : None ~ f ( : fun max element -> match max with | None -> Some element | Some value -> ( match compare element value > 0 with | true -> Some element | false -> max ) )
let extent t ~ compare = fold t ~ initial : None ~ f ( : fun current element -> match current with | None -> Some ( element , element ) element | Some ( min , max ) max -> Some ( ( match compare element min < 0 with | true -> element | false -> min ) , match compare element max > 0 with | true -> element | false -> max...
let sort t ~ compare = Belt . List . sort t compare
let sortBy ( ~ f : ' a -> ' b ) ' b ( l : ' a t ) t : ' a t = Belt . List . sort l ( fun a b -> let a ' = f a in let b ' = f b in if a ' = b ' then 0 else if a ' < b ' then - 1 else 1 )
let groupi l ~ break = let groups = Belt . List . reduceWithIndex l [ ] ( fun acc x i -> match acc with | [ ] -> [ [ x ] ] | current_group :: tl -> if break i ( Belt . List . headExn current_group ) current_group x then [ x ] :: current_group :: tl else ( x :: current_group ) current_group :: tl ) in match groups with ...
let groupWhile l ~ f = groupi l ~ break ( : fun _ x y -> f x y ) y
let insertAt t ~ index ~ value = let front , back = splitAt t ~ index in append front ( value :: back ) back
let splitWhen t ~ f = let rec loop front back = match back with | [ ] -> ( t , [ ] ) | element :: rest -> if f element then ( reverse front , back ) back else loop ( element :: front ) front rest in loop [ ] t
let intersperse t ~ sep = match t with | [ ] -> [ ] | [ x ] -> [ x ] | x :: rest -> x :: foldRight rest ~ initial [ ] : ~ f ( : fun acc x -> sep :: x :: acc ) acc
let initialize length ~ f = Belt . List . makeBy length f
let forEach t ~ f : unit = Belt . List . forEach t f
let forEachWithIndex t ~ f : unit = Belt . List . forEachWithIndex t f
let join strings ~ sep = Js . Array . joinWith sep ( toArray strings ) strings
let groupBy t comparator ~ f = fold t ~ initial ( : TableclothMap . empty comparator ) comparator ~ f ( : fun map element -> let key = f element in TableclothMap . update map ~ key ~ f ( : function | None -> Some [ element ] | Some elements -> Some ( element :: elements ) elements ) )
let rec equal equalElement a b = match ( a , b ) b with | [ ] , [ ] -> true | x :: xs , y :: ys -> equalElement x y && equal equalElement xs ys | _ -> false
let rec compare compareElement a b = match ( a , b ) b with | [ ] , [ ] -> 0 | [ ] , _ -> - 1 | _ , [ ] -> 1 | x :: xs , y :: ys -> ( match compareElement x y with | 0 -> compare compareElement xs ys | result -> result )
type ( ' key , ' value , ' cmp ) ' cmp t = ( ' key , ' value , ' cmp ) ' cmp Belt . Map . t
module Of ( M : TableclothComparator . S ) S = struct type nonrec ' value t = ( M . t , ' value , M . identity ) identity t end
let fromArray ( comparator : ( ' key , ' id ) ' id TableclothComparator . s ) s ( values : ( ' key * ' v ) ' v array ) array : ( ' key , ' value , ' id ) ' id t = Belt . Map . fromArray values ~ id ( : Internal . toBeltComparator comparator ) comparator
let empty comparator = fromArray comparator [ ] ||
let fromList comparator l = fromArray comparator ( Array . of_list l ) l
let singleton comparator ~ key ~ value = fromArray comparator [ | ( key , value ) value ] |
let add m ~ key ~ value = Belt . Map . set m key value
let ( { } . ?<- ) ( map : ( ' key , ' value , ' id ) ' id t ) t ( key : ' key ) ' key ( value : ' value ) ' value : ( ' key , ' value , ' id ) ' id t = add map ~ key ~ value
let ( { } . ? ) ( map : ( ' key , ' value , _ ) _ t ) t ( key : ' key ) ' key : ' value option = get map key
let update m ~ key ~ f = Belt . Map . update m key f
let merge m1 m2 ~ f = Belt . Map . merge m1 m2 f
let map m ~ f = Belt . Map . map m ( fun value -> f value ) value
let mapWithIndex t ~ f = Belt . Map . mapWithKey t f
let filter m ~ f = Belt . Map . keep m ( fun _ value -> f value ) value
let partition m ~ f = Belt . Map . partition m ( fun key value -> f ~ key ~ value ) value
let find m ~ f = Belt . Map . findFirstBy m ( fun key value -> f ~ key ~ value ) value
let any m ~ f = Belt . Map . some m ( fun _ value -> f value ) value
let all m ~ f = Belt . Map . every m ( fun _ value -> f value ) value
let forEach m ~ f = Belt . Map . forEach m ( fun _ value -> f value ) value
let forEachWithIndex m ~ f = Belt . Map . forEach m ( fun key value -> f ~ key ~ value ) value
let fold m ~ initial ~ f = Belt . Map . reduce m initial ( fun acc key data -> f acc ~ key ~ value : data ) data
let keys m = Belt . Map . keysToArray m . | Array . to_list
let values m = Belt . Map . valuesToArray m . | Array . to_list
let extent t = Option . both ( minimum t ) t ( maximum t ) t
module Poly = struct type identity type nonrec ( ' k , ' v ) ' v t = ( ' k , ' v , identity ) identity t let fromArray ( type k v ) v ( a : ( k * v ) v array ) array : ( k , v ) v t = Belt . Map . fromArray a ~ id : ( module struct type t = k type nonrec identity = identity let cmp = Pervasives . compare . | Obj . magi...
module Int = struct type nonrec ' value t = ' value Of ( OfTableclothInt ) OfTableclothInt . t let fromArray a = Poly . fromArray a . | Obj . magic let from_array = fromArray let empty = fromArray [ ] || let singleton ~ key ~ value = fromArray [ | ( key , value ) value ] | let fromList l = fromArray ( Array . of_list l...
module String = struct type nonrec ' value t = ' value Of ( OfTableclothString ) OfTableclothString . t let fromArray a = Poly . fromArray a . | Obj . magic let from_array = fromArray let empty = fromArray [ ] || let singleton ~ key ~ value = fromArray [ | ( key , value ) value ] | let fromList l = fromArray ( Array . ...
type ' a t = ' a option
let some a = Some a
let or_ ta tb = match isSome ta with true -> ta | false -> tb
let orElse ta tb = match isSome tb with true -> tb | false -> ta
let and_ ta tb = match isSome ta with true -> tb | false -> ta
let andThen t ~ f = match t with None -> None | Some x -> f x
let flatten = function Some option -> option | None -> None
let both a b = match ( a , b ) b with Some a , Some b -> Some ( a , b ) b | _ -> None
let map t ~ f = Belt . Option . map t f
let map2 a b ~ f = match ( a , b ) b with Some a , Some b -> Some ( f a b ) b | _ -> None
let unwrap t ~ default = Belt . Option . getWithDefault t default
let unwrapOrFailWith t ~ exn = match t with Some value -> value | None -> raise exn
let unwrapUnsafe = unwrapOrFailWith ~ exn ( : Invalid_argument " Option . unwrapUnsafe called with None ) "
let toArray t = match t with None -> [ ] || | Some value -> [ | value ] |
let toList t = match t with None -> [ ] | Some value -> [ value ]
let tap t ~ f = match t with None -> ( ) | Some x -> f x
let equal equal a b = match ( a , b ) b with | None , None -> true | Some a ' , Some b ' -> equal a ' b ' | _ -> false
let compare compare a b = match ( a , b ) b with | None , None -> 0 | Some a ' , Some b ' -> compare a ' b ' | None , Some _ -> - 1 | Some _ , None -> 1
let ( |? ) t default = unwrap t ~ default
let ( >>| ) t f = map t ~ f
let ( >>= ) t f = andThen t ~ f
type ( ' ok , ' error ) ' error t = ( ' ok , ' error ) ' error result
let ok a = Belt . Result . Ok a
let error e = Belt . Result . Error e
let fromOption ma ~ error = match ma with | None -> Belt . Result . Error error | Some right -> Belt . Result . Ok right
let both a b = match ( a , b ) b with | Ok a ' , Ok b ' -> Ok ( a ' , b ' ) b ' | Error a ' , _ -> Error a ' | _ , Error b ' -> Error b '
let flatten a = match a with Ok a ' -> a ' | Error error -> Error error
let or_ a b = match a with Ok _ -> a | _ -> b
let orElse a b = match b with Ok _ -> b | _ -> a
let or_else a b = match b with Ok _ -> b | _ -> a
let and_ a b = match a with Ok _ -> b | _ -> a
let unwrap t ~ default = Belt . Result . getWithDefault t default