elm - Converting A Maybe Value Into A Normal Value -


i have no clue how convert maybe value normal value ...

i have these lines...

pickchord : model -> note -> chord pickchord model note =     let         nextchord = list.head (list.filter (testchord note) model.possiblemajorchords)     in         nextchord 

the compiler complains:

the definition of pickchord not match type annotation. - type annotation pickchord says returns:

chord

but returned value (shown above) a:

maybe chord

how handle problem?

thanks.

this why love elm. in it's own way, elm telling you've got design flaw.

let's take closer look. here's code as-is:

pickchord : model -> note -> chord pickchord model note =     let         nextchord = list.head (list.filter (testchord note) model.possiblemajorchords)     in         nextchord 

so you've got list of major chords filter specific note. produces list of chords. however, list type can represent empty list, why list.head returns maybe. in case, list.head can return either nothing or just chord.

now, can work around default values , error handling, work-arounds dodge real problem: the list type doesn't accurately fit problem domain.

if you've got list of all major chords, can't think of reason why ever end empty list after applying filter. filter should find @ least 1 chord. assuming that's case, need list-like type can represent list never empty; means head return chord , not maybe chord. better representation of you're trying achieve. luckly, there's such type (which didn't create use extensively) called list.nonempty. here's how work:

import list.nonempty ne  pickchord : model -> note -> chord pickchord model note =             ne.head (ne.filter (testchord note) model.possiblemajorchords) 

your model.possiblemajorchords have change list chord list.nonempty chord, makes whole maybe problem go away. of course assuming claim filter returning @ least 1 chord holds true.

here's link nonempty package: http://package.elm-lang.org/packages/mgold/elm-nonempty-list/latest


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -