Last active
September 7, 2019 14:13
-
-
Save busypeoples/a7c4179027a28c1daa69ff58b957bef2 to your computer and use it in GitHub Desktop.
ReasonML port remote-data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
remote-data ported to ReasonML | |
See also https://github.com/krisajenkins/remotedata | |
Tools for fetching data from remote sources (incl. HTTP). | |
*/ | |
type remoteData 'e 'a | |
= NotAsked | |
| Loading | |
| Failure 'e | |
| Success 'a; | |
let map f data => | |
switch data { | |
| Success d => Success (f d) | |
| Failure error => Failure error | |
| NotAsked => NotAsked | |
| Loading => Loading | |
}; | |
let andMap wrappedValue wrappedFunction => | |
switch wrappedFunction { | |
| Success f => map f wrappedValue | |
| Failure error => Failure error | |
| Loading => Loading | |
| NotAsked => NotAsked | |
}; | |
let map2 f a b => | |
map f a | |
|> andMap b; | |
let map3 f a b c => | |
map f a | |
|> andMap b | |
|> andMap c; | |
let mapError f data => | |
switch data { | |
| Success x => Success x | |
| Failure e => Failure (f e) | |
| Loading => Loading | |
| NotAsked => NotAsked | |
}; | |
let mapBoth successFn failureFn data => | |
switch data { | |
| Success x => Success (successFn x) | |
| Failure e => Failure (failureFn e) | |
| Loading => Loading | |
| NotAsked => NotAsked | |
}; | |
let andThen f data => | |
switch data { | |
| Success a => f a | |
| Failure e => Failure e | |
| Loading => Loading | |
| NotAsked => NotAsked | |
}; | |
let withDefault default data => | |
switch data { | |
| Success a => a | |
| _ => default | |
}; | |
let toOption data => | |
data | |
|> map (fun a => Some a) | |
|> withDefault None; | |
let fromOption option errorMsg => | |
switch option { | |
| Some a => Success a | |
| None => Failure errorMsg | |
}; | |
let append a b => map (fun x y => (x, y)) a |> andMap b; | |
let succeed a => Success a; | |
let isSuccess data => | |
switch data { | |
| Success _a => true | |
| _ => false | |
}; | |
let isFailure data => | |
switch data { | |
| Failure _e => true | |
| _ => false | |
}; | |
let isLoading data => | |
switch data { | |
| Loading => true | |
| _ => false | |
}; | |
let isNotAsked data => | |
switch data { | |
| NotAsked => true | |
| _ => false | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type remoteData 'e 'a | |
= NotAsked | |
| Loading | |
| Failure 'e | |
| Success 'a; | |
let map : ('a => 'b) => remoteData 'e 'a => remoteData 'e 'b; | |
let map2 : ('a => 'b => 'c) => remoteData 'e 'a => remoteData 'e 'b => remoteData 'e 'c; | |
let map3 : ('a => 'b => 'c => 'd) | |
=> remoteData 'e 'a => remoteData 'e 'b => remoteData 'e 'c => remoteData 'e 'd; | |
let mapError : ('e => 'f) => remoteData 'e 'a => remoteData 'f 'a; | |
let mapBoth : ('a => 'b) => ('e => 'f) => remoteData 'e 'a => remoteData 'f 'b; | |
let andThen : ('a => remoteData 'e 'b) => remoteData 'e 'a => remoteData 'e 'b; | |
let withDefault : 'a => remoteData 'e 'a => 'a; | |
let toOption : remoteData 'e 'a => option 'a; | |
let fromOption : option 'a => 'e => remoteData 'e 'a; | |
let append : remoteData 'e 'a => remoteData 'e 'b => remoteData 'e ('a, 'b); | |
let andMap : remoteData 'e 'a => remoteData 'e ('a => 'b) => remoteData 'e 'b; | |
let succeed : 'a => remoteData 'e 'a; | |
let isSuccess : remoteData 'e 'a => bool; | |
let isFailure : remoteData 'e 'a => bool; | |
let isLoading : remoteData 'e 'a => bool; | |
let isNotAsked : remoteData 'e 'a => bool; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment