Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Prelude
- module Async =
- let map f x = async {
- let! x' = x
- return f x'
- }
- let bind f x = async {
- let! x' = x
- return! f x'
- }
- let lift a = async { return a }
- module Result =
- let orFail = function
- | Ok x -> x
- | Error e -> failwith e
- let apply a f =
- match a, f with
- | Ok v, Ok x -> Ok (x v)
- | _, Error e -> Error e
- | Error e, _ -> Error e
- let retn = Ok
- let traverse f c =
- let (<*>) f a = apply a f
- let initialState = retn []
- let cons head tail = head :: tail
- let folder head tail =
- retn cons <*> (f head) <*> tail
- List.foldBack folder c initialState
- let isOk = function | Ok _ -> true | _ -> false
- let isError = function | Error _ -> true | _ -> false
- module AsyncResult =
- let bind f a = async {
- let! a' = a
- return!
- match a' with
- | Ok v -> f v
- | Error e -> Async.lift (Error e)
- }
- let map f a = async {
- let! a' = a
- return
- match a' with
- | Ok v -> Ok (f v)
- | Error e -> Error e
- }
- let apply a f = async {
- let! a' = a
- let! f' = f
- return
- match a', f' with
- | Ok v, Ok x -> Ok (x v)
- | _, Error e -> Error e
- | Error e, _ -> Error e
- }
- let retn x = x |> Ok |> Async.lift
- let traverse f c =
- let (<*>) f a = apply a f
- let initialState = retn []
- let cons head tail = head :: tail
- let folder head tail =
- retn cons <*> (f head) <*> tail
- List.foldBack folder c initialState
- let traverseParallel f c = async {
- let! results = c |> List.map f |> Async.Parallel
- return
- if results |> Array.exists Result.isError |> not
- then results |> Array.map Result.orFail |> Array.toList |> Ok
- else results |> Array.find Result.isError |> Result.map (fun _ -> [])
- }
- module Option =
- let toResult error = function
- | Some x -> Ok x
- | None -> Error error
- module Seq =
- let noneIfEmpty s = if Seq.isEmpty s then None else Some s
- module Tuple2 =
- let uncurry f (x, y) = f x y
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement