import type { Maybe } from "Maybe" import {} from "Scan" #iftarget js import { Just, Nothing } from "Maybe" _scanFloat :: (Float -> Maybe Float) -> Maybe Float -> String -> Maybe Float _scanFloat = (just, nothing, str) => ( #- { const floatPattern = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/ if (str.trim() !== str || !floatPattern.test(str)) { return nothing } const n = Number(str) return Number.isFinite(n) ? just(n) : nothing } -# ) scanFloat = _scanFloat(Just, Nothing) #elseif llvm scanFloat :: String -> Maybe Float scanFloat = extern "madlib__number__scanFloat" #endif instance Scan Float { scan = scanFloat } #iftarget js fromInteger :: Integer -> Float export fromInteger = (a) => #- { return a } -# fromShort :: Short -> Float export fromShort = (a) => #- { return a } -# fromByte :: Byte -> Float export fromByte = (a) => #- { return a } -# toInteger :: Float -> Integer export toInteger = (a) => #- { return Math.trunc(a) } -# toShort :: Float -> Short export toShort = (a) => #- { return Math.trunc(a) } -# toByte :: Float -> Byte export toByte = (a) => #- { return Uint8Array.from([Math.trunc(a)])[0] } -# #elseif llvm fromInteger :: Integer -> Float export fromInteger = extern "madlib__number__intToFloat" fromShort :: Short -> Float export fromShort = extern "madlib__number__shortToFloat" fromByte :: Byte -> Float export fromByte = extern "madlib__number__byteToFloat" toInteger :: Float -> Integer export toInteger = extern "madlib__number__floatToInt" toShort :: Float -> Short export toShort = extern "madlib__number__floatToShort" toByte :: Float -> Byte export toByte = extern "madlib__number__floatToByte" #endif