-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.elm
45 lines (30 loc) · 1.64 KB
/
Util.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Util where
import String
import Graphics.Input
--Helper Function to read a Float
getFloat = fromJust 0 . String.toFloat
getInt = fromJust 0 . String.toInt
liftFloat : Signal String -> Signal Float
liftFloat = lift getFloat
labelit title elem = flow right [plainText title, elem]
labelledField : String -> String-> (Signal Element, Signal Float)
labelledField title dflt = let (elem,str) = Graphics.Input.field dflt
in (lift (labelit title) elem,liftFloat str)
labelledChoice : String -> [(String,a)] -> (Signal Element,Signal a)
labelledChoice title vals =
let (elem,val) = Graphics.Input.dropDown vals
in (lift (labelit title) elem, val)
fromJust dflt val = maybe dflt id val
range a b count = map (\x -> a + x/(count-1) *(b-a)) [0..(count)]
labove = lift2 above
--floatPrecision : Int -> Float -> String
floatPrecision digits x = let splitup = (String.split "e" <| show x)
mantissa = getFloat <| head splitup
adjust = if mantissa < 1 then 1 else 0
exponent = if length splitup == 1 then 0 else getInt . head <| tail splitup
secondExponent = truncate . logBase 10 <| mantissa
sigDigs = round (mantissa / (10 ^ (toFloat (exponent + secondExponent - digits))))
final = (toFloat sigDigs) / (10 ^ (toFloat digits)) * (10^adjust)
in String.join "e" [show final , show (secondExponent-adjust)]
getAssoc : a -> [(a,b)] -> b
getAssoc k = snd . head . filter ((\ x -> x == k) . fst)