Last active
November 17, 2018 12:42
-
-
Save Kirens/e73ef7aaab1026916aa190496d03db3c to your computer and use it in GitHub Desktop.
Exempel på JavaScript-kod med statisk typning som inte påverkar körbarhet
This file contains hidden or 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
| // Do some stuff to showcase different type signatures | |
| // main :: () >> Promise Fetcher a | |
| const main = () => | |
| whoAmI() | |
| .catch(alerter('Could not determine who you are')) | |
| .then(set('userName')(window)) | |
| .then(wait(5)) | |
| .then(randomName) | |
| .then(iAmThis) | |
| .catch(alerter('Could not update your name')) | |
| // Let's create a algebraic data type to represent fetched data. | |
| class Fetcher {} | |
| // Fetching state; contains no aditional data | |
| class Fetching extends Fetcher {} | |
| // Fetching resolved state; contains resolved data | |
| class FetchResult extends Fetcher { | |
| // constructor :: a -> Fetcher a b | |
| constructor(data) { this.data = data } | |
| } | |
| // Fetching failed state; contains error information | |
| class FetchingFailed extends Fetcher { | |
| // constructor :: b -> Fetcher a b | |
| constructor(error) { this.error = error } | |
| } | |
| // Create some function wrapers for constructing new instances | |
| // The type-checker can infere types. Anotations on these trivial | |
| // functions will not aid a human reader. | |
| const fetching = () => | |
| new Fetching | |
| const fetchResult = d => | |
| new FetchResult(d) | |
| const fetchingFailed = e => | |
| new FetchingFailed(e) | |
| // Maybe support type aliases and record like syntax for type arguments | |
| // type PromiseSame a = Promise { result = a, error = a } | |
| // Wrap the fetch API in our Fetching type | |
| // doFetch :: a >> PromiseSame (Fetching String Error) | |
| const doFetch = (...args) => | |
| fetch(...args) | |
| .then(response => fetchResult(response.text())) | |
| .catch(err => Promise.reject(fetchingError(err))) | |
| // This is a global mutable variable where we store the current users name. | |
| // userName :: Fetcher String Error | |
| let userName = fetching() | |
| // `set` is a function that mutates the state of an object hence the `>>` | |
| // notation, here we indicate that a mutation happens and any function | |
| // using `set` will be tainted with this impurity. | |
| // set :: String -> Object a -> a >> void | |
| const set = property => object => value => { | |
| object[property] = value | |
| } | |
| // Simple transparent alert binder | |
| // alerter :: String -> a >> a | |
| const alerter = msg => id => | |
| alert(msg) | |
| || id | |
| // wait :: Number -> Promise void a | |
| const wait = secs => | |
| new Promise(resolve => | |
| setTimeout(resolve, secs*1e3) | |
| ) | |
| // whoAmI :: () >> Promise (Fetching String Error) a | |
| const whoAmI = () => | |
| doFetch('/api/v1/user/name') | |
| // whoAmI :: () >> Promise (Fetching String Error) a | |
| const iAmThis = name => | |
| doFetch('/api/v1/user/name', { method: 'PUT', body: name }) | |
| // alphabetLower :: String | |
| const alphabetLower = | |
| 'abcdefghijklmnopqrstuvwxyz' | |
| // Possibility of subset types Char ⊂ String | |
| // Stochastic function aplication | |
| // eandomChar :: () ~> Char | |
| const randomChar = | |
| alphabetLower[Math.floor(Math.random(), alphabetLower.length)] | |
| // Maybe there could be a notation for undeterministic halting | |
| // randomName :: () ~>? String | |
| const randomName = () => { | |
| let name = '' | |
| while(Math.random() < 5) | |
| name += randomCharacter() | |
| return name | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment