2017-08-13 1 views
0

Je veux mettre à jour mon modèle ainsi:Application Maybes Lorsque la mise à jour un modèle

updatedModel = 
    if model.firstChord && model.secondChord then 
    { model | firstChord = {}, secondChord = {} } 
    else 
    model 

le firstChord et secondChord les deux sont du type Chord:

type alias Chord = 
{ root : Maybe Pitch 
, third : Maybe Pitch 
, fifth : Maybe Pitch 
} 

Le type de terrain ressemble à:

-- pitch 
type alias Pitch = (PitchName, PitchLevel) 

-- pitch name 
type alias PitchName = String 

-- pitch level 
type alias PitchLevel = Int 

Mon modèle initial a ces domaines:

{ firstChord = 
    { root = ("C", 3) 
    , third = ("E", 3) 
    , fifth = ("G", 3) 
    } 
, secondChord = 
    { root = ("F", 3) 
    , third = ("A", 3) 
    , fifth = ("C", 4) 
    } 

J'aime avoir des valeurs de hauteur optionnelles.

Comment puis-je mettre à jour mon modèle en lui donnant une valeur OU rien?

Merci.

+0

Pourquoi ne pas 'type PitchName = A | B | C | ... | G'. Et 'type Pitch = Pitch PitchName PitchLevel'. Alors 'somePitch = Pitch G 3' – z5h

+0

Si firstChord et secondChord sont de type Chord alors' firstChord = {} 'serait illégal car {} n'est pas de type Chord. – jpierson

Répondre

2

Vous ne savez pas exactement ce que vous cherchez. Je vous invite à avoir un Chord Peut-être comme ça.

type Pitch = 
    Pitch String Int 

type alias Chord = 
    { root: Maybe Pitch 
    , third: Maybe Pitch 
    , fifth: Maybe Pitch 
    } 

type alias Model = 
    { firstChord: Maybe Chord 
    , secondChord: Maybe Chord 
    } 

init: Model 
init = 
    { firstChord = 
     { root = Pitch "C" 3 
     , third = Pitch "E" 3 
     , fifth = Pitch "G" 3 
     } 
    , secondChord = 
     { root = Pitch "F" 3 
     , third = Pitch "A" 3 
     , fifth = Pitch "C" 4 
     } 
    } 

update: Model -> Model 
update model = 
    case (model.firstChord, model.secondChord) of 
     (Just first, Just second) -> 
      { model | firstChord = Nothing, secondChord = Nothing} 
     _ -> 
      model 
0

Lorsque vous avez un type Maybe a, tels que Maybe Pitch vous avez deux façons de définir sa valeur: Nothing ou Just a. Ainsi, au lieu de cela:

{ firstChord = 
    { root = ("C", 3) 
    , third = ("E", 3) 
    , fifth = ("G", 3) 
    } 
, secondChord = 
    { root = ("F", 3) 
    , third = ("A", 3) 
    , fifth = ("C", 4) 
    } 

... vous devez faire ceci:

{ firstChord = 
    { root = Just ("C", 3) 
    , third = Just ("E", 3) 
    , fifth = Just ("G", 3) 
    } 
, secondChord = 
    { root = Just ("F", 3) 
    , third = Just ("A", 3) 
    , fifth = Just ("C", 4) 
    } 

Bien sûr, vous utilisez alors Nothing donc indiquer aucune valeur:

firstChord = 
    { root = Just ("C", 3) 
    , third = Just ("E", 3) 
    , fifth = Nothing 
    } 
+0

je sais déjà à ce sujet :) mais merci. – Timo