Created
February 25, 2017 10:56
-
-
Save lukaswilkeer/e91911d638363543580a23ce7d97821f to your computer and use it in GitHub Desktop.
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
const TYPES = { | |
'Undefined': ( val ) => typeof val == 'undefined', | |
'Null': ( val ) => typeof val == 'object', | |
'Boolean': ( val ) => typeof val == 'boolean', | |
'Number': ( val ) => typeof val == 'number', | |
'String': ( val ) => typeof val == 'string' | |
} | |
// String -> [in, out] | |
const signature = (anotattion) => anotattion.split(' -> ') | |
// [Signature] -> [Data] | |
const check = (test, type) => test(type) | |
const checkType = (TYPES, typeIn) => (type, i) => check(TYPES[typeIn], type) | |
const checkIn = (data, TYPES, typeIn) => data.map(checkType(TYPES, typeIn)) | |
const checkOut = (data, TYPES, typeOut) => data.map(checkType(TYPES, typeOut)) | |
// Int, Str : [6, 'Lukas'] | |
const checkSignature = (anotattion = [], data = [], fn) => { | |
const [typeIn, typeOut] = signature(anotattion) | |
const computation = fn(...data) | |
const resultIn = checkIn(data, TYPES, typeIn) | |
const resultOut = checkOut([computation], TYPES, typeOut) | |
console.log('resultIn', resultIn) | |
console.log('resultOut', resultOut) | |
return {resultIn, resultOut} | |
} | |
const isPar = (num) => !(num%2) | |
let t = checkSignature('Number -> Boolean', [6], isPar) | |
let u = checkSignature('Number -> Boolean', [[7]], isPar) | |
let i = checkSignature('Number -> Boolean', ['String bugada'], isPar) | |
let o = checkSignature('Number -> Boolean', [{}], isPar) | |
console.log('teste', t, u, i, o) | |
module.exports = {checkIn} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment