Last active
December 23, 2019 12:11
-
-
Save jihchi/d484af67c420b10827b8824d4c292863 to your computer and use it in GitHub Desktop.
bs-let / let%Anything utilities and example from https://github.com/notablemind/renm/blob/master/src/utils/Lets.re
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
// source: https://github.com/notablemind/renm/blob/master/src/utils/Lets.re | |
module Async = { | |
type t('a) = Js.Promise.t('a); | |
let try_ = (promise, continuation) => Js.Promise.catch(continuation, promise); | |
let let_ = (promise, continuation) => | |
Js.Promise.then_(continuation, promise); | |
let resolve = Js.Promise.resolve; | |
let reject = Js.Promise.reject; | |
let map = (promise, fn) => Js.Promise.then_(v => Js.Promise.resolve(fn(v)), promise); | |
module Wrap = { | |
let let_ = (promise, cont) => | |
Js.Promise.then_(v => Js.Promise.resolve(cont(v)), promise); | |
}; | |
/* | |
let getRoot = token => { | |
let%Lets.Async response = | |
fetch( | |
"https://www.googleapis.com/drive/v2/files?q=" | |
++ encodeURIComponent("title = '" ++ rootTitle ++ "'") | |
++ "&orderBy=createdDate", | |
{ | |
"headers": { | |
"Authorization": "Bearer " ++ token, | |
}, | |
}, | |
); | |
let%Lets.Async.Wrap data = response->json; | |
switch (data##items[0]) { | |
| None => None | |
| Some(item) => Some(item##id: string) | |
} | |
}; | |
*/ | |
module Consume = { | |
let let_ = (promise, cont) => | |
Js.Promise.then_( | |
value => { | |
let () = cont(value); | |
Js.Promise.resolve(); | |
}, | |
promise, | |
) | |
|> ignore; | |
}; | |
/* | |
let createFile = (state, google: Session.google, ~rootFolder, file) => { | |
Js.log("Creating file"); | |
let serverFile = currentToServerFile(file); | |
let serialized = WorkerProtocolSerde.serializeServerFile(serverFile); | |
let%Lets.Async.Consume (nmId, fileId, fileTitle) = GoogleDrive.createFile( | |
google.accessToken, | |
~fileId=file.meta.id, | |
~rootFolder, | |
~title=file.meta.title, | |
~contents=Js.Json.stringify(serialized) | |
); | |
Js.log2("Created file, getting etag", fileId); | |
let%Lets.Async.Consume etag = GoogleDrive.getEtag(google.accessToken, fileId); | |
Js.log2("etag", etag); | |
let%Lets.OptForce etag = etag; | |
file.meta = { | |
...file.meta, | |
sync: Some({ | |
remote: Google(google.googleId, fileId), | |
lastSyncTime: Js.Date.now(), | |
etag, | |
}) | |
} | |
sendMetaDataChange(state.ports, file.meta); | |
let%Lets.Async.Consume () = MetaDataPersist.save(file.meta); | |
}; | |
*/ | |
}; | |
module Guard = { | |
let let_ = ((condition, default), continuation) => | |
if (condition) { | |
continuation(); | |
} else { | |
default; | |
}; | |
}; | |
/* | |
let%Lets.Guard () = ( | |
!blacklistedIds->Set.String.has(itemId), | |
candidate, | |
); | |
let%Lets.OptDefault (distance, dropPos, position) = ( | |
{ | |
let rect = getBoundingClientRect(itemNode); | |
testNode(itemId, x, y, rect); | |
}, | |
candidate, | |
); | |
*/ | |
module OptDefault = { | |
let let_ = ((a, default), b) => | |
switch (a) { | |
| None => default | |
| Some(x) => b(x) | |
}; | |
let or_ = (v, default) => | |
switch (v) { | |
| None => default | |
| Some(c) => c | |
}; | |
}; | |
module OptForce = { | |
let let_ = (a, b) => | |
switch (a) { | |
| None => failwith("Unwrapping an empty optional") | |
| Some(x) => b(x) | |
}; | |
}; | |
module Try = { | |
let let_ = (a, b) => | |
switch (a) { | |
| Result.Error(e) => Result.Error(e) | |
| Ok(x) => b(x) | |
}; | |
let map = (a, b) => | |
switch (a) { | |
| Result.Error(e) => Result.Error(e) | |
| Ok(x) => Ok(b(x)) | |
}; | |
let flatMap = let_; | |
let try_ = (a, b) => | |
switch (a) { | |
| Result.Error(e) => b(e) | |
| Ok(v) => Result.Ok(v) | |
}; | |
let force = t => | |
switch (t) { | |
| Result.Error(e) => | |
Js.log(e); | |
failwith("Force unwrapped an Error()"); | |
| Ok(v) => v | |
}; | |
}; | |
module TryWrap = { | |
let let_ = Try.map; | |
}; | |
module TryForce = { | |
let let_ = (a, b) => b(Try.force(a)); | |
}; | |
module TryLog = { | |
let let_ = (a, b) => | |
switch (a) { | |
| Result.Error(e) => Js.log2(e, [%bs.raw "new Error('for the stack')"]) | |
| Ok(v) => b(v) | |
}; | |
}; | |
module Opt = { | |
let let_ = (a, b) => | |
switch (a) { | |
| None => None | |
| Some(x) => b(x) | |
}; | |
let map = (a, b) => | |
switch (a) { | |
| None => None | |
| Some(x) => Some(b(x)) | |
}; | |
let force = value => | |
switch (value) { | |
| None => failwith("Force unwrapped a none") | |
| Some(x) => x | |
}; | |
let orError = (value, error) => | |
switch (value) { | |
| Some(v) => Result.Ok(v) | |
| None => Result.Error(error) | |
}; | |
let flatMap = let_; | |
}; | |
module OptIf = { | |
let let_ = (a, b) => | |
if (a) { | |
b(); | |
} else { | |
None; | |
}; | |
}; | |
module UnitIf = { | |
let let_ = (a, b) => | |
if (a) { | |
b(); | |
} else { | |
(); | |
}; | |
}; | |
module OptWrap = { | |
let let_ = (a, b) => | |
switch (a) { | |
| None => None | |
| Some(x) => Some(b(x)) | |
}; | |
}; | |
module OptOr = { | |
let let_ = (a, b) => | |
switch (a) { | |
| None => b() | |
| Some(x) => a | |
}; | |
}; | |
module OptConsume = { | |
let let_ = (a, b) => | |
switch (a) { | |
| None => () | |
| Some(x) => b(x) | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment