Elm signals and type annotation -
Elm signals and type annotation -
from elm newbie long-time haskeller, quick query.
the aim: there map towns on @ designated positions on it, , want check if user click close town, , identify town.
so, collect signal usual:
clickpositionssignal = sampleon mouse.clicks mouse.position that gives me tuple, want turn int (denoting number of nearest town). towns designated
positions : [position] type position = {number : int, x : int, y : int} the function is:
whichtown : (int,int) -> int whichtown (x,y) = allow pz = map (\p -> getdistance p.x p.y x y) positions |> head in pz.number now, need apply function clickpositionssignal.
looking around various examples, modified code to....
whichlocationsignal : signal int whichlocationsignal = allow wbl (x,y) = whichtown(x,y) in wbl <~ clickpositionssignal .... , works. number of nearest town.
but hopelessly cumbersome , duplicative. question why can not write:
whichlocationsignal = whichtown clickpositionssignal that line throws multiple type errors not yet experienced plenty interpret
tl;dr
it should be:
whichlocationsignal = whichtown <~ clickpositionssignal or
whichlocationsignal = lift whichtown clickpositionssignal (which figured out yourself)
how read type errorsso total version of code gives these type-errors be:
import mouse type position = {number : int, x : int, y : int} clickpositionssignal : signal (int,int) clickpositionssignal = sampleon mouse.clicks mouse.position positions : [position] positions = [] getdistance x1 y1 x2 y2 = { number = 0 } whichtown : (int,int) -> int whichtown (x,y) = allow pz = map (\p -> getdistance p.x p.y x y) positions |> head in pz.number whichlocationsignal : signal int whichlocationsignal = whichtown clickpositionssignal the type errors are:
type error on line 19, column 33 53: clickpositionssignal expected type: (int, int) actual type: int type error on line 19, column 33 53: clickpositionssignal expected type: signal.signal actual type: (int) type error on line 19, column 23 32: whichtown expected type: int actual type: signal.signal int i admit, these type errors confusing. (you might incorrect. can current quality of type errors get, sorry!) 1 basic tip when type error elm doesn't create sense see if makes more sense when flip expected/actual. first type error makes no sense. sec gives information: clickpositionssignal somehow expected have type int, not signal. 3rd error message starts create sense: whichtown other way around int should given signal... @ point can find uses of these 2 together, , 1 time note whichtown works on (int,int) , clickpositionssignal : signal (int,int), you've found error , compiler messages create kind of crooked sense.
the prepare stated above utilize lift : (a -> b) -> signal -> signal b "lift" function (whichtown) signal "level". people prefer utilize infix operator <~.
signals elm
Comments
Post a Comment