Elm 在更新模型时应用可能情况

Elm 在更新模型时应用可能情况,elm,Elm,我想通过以下方式更新我的模型: updatedModel = if model.firstChord && model.secondChord then { model | firstChord = {}, secondChord = {} } else model 第一和弦和第二和弦都属于和弦类型: type alias Chord = { root : Maybe Pitch , third : Maybe Pitch , fifth :

我想通过以下方式更新我的模型:

updatedModel =
  if model.firstChord && model.secondChord then
    { model | firstChord = {}, secondChord = {} }
  else
    model
第一和弦和第二和弦都属于和弦类型:

type alias Chord =
{   root : Maybe Pitch
,   third : Maybe Pitch
,   fifth : Maybe Pitch
}
音高类型如下所示:

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

-- pitch name
type alias PitchName = String

-- pitch level
type alias PitchLevel = Int
我的初始模型包含以下字段:

{ firstChord =
    {   root = ( "C", 3 )
    ,   third = ( "E", 3 )
    ,   fifth = ( "G", 3 )
    }
, secondChord =
    {   root = ( "F", 3 )
    ,   third = ( "A", 3 )
    ,   fifth = ( "C", 4 )
    }
我喜欢有可选的音高值

我如何更新我的模型给它一个值或什么都没有


谢谢。

不太清楚你在找什么。我想你会喜欢这样的和弦

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

不太清楚你在找什么。我想你会喜欢这样的和弦

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

当你有一个
可能是一个
类型,比如
可能是音高
时,你有两种方法来设置它的值:
只是一个
。因此,与此相反:

{ firstChord =
    {   root = ( "C", 3 )
    ,   third = ( "E", 3 )
    ,   fifth = ( "G", 3 )
    }
, secondChord =
    {   root = ( "F", 3 )
    ,   third = ( "A", 3 )
    ,   fifth = ( "C", 4 )
    }
…您需要这样做:

{ 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 )
    }
当然,然后使用
Nothing
,因此表示无值:

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

当你有一个
可能是一个
类型,比如
可能是音高
时,你有两种方法来设置它的值:
只是一个
。因此,与此相反:

{ firstChord =
    {   root = ( "C", 3 )
    ,   third = ( "E", 3 )
    ,   fifth = ( "G", 3 )
    }
, secondChord =
    {   root = ( "F", 3 )
    ,   third = ( "A", 3 )
    ,   fifth = ( "C", 4 )
    }
…您需要这样做:

{ 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 )
    }
当然,然后使用
Nothing
,因此表示无值:

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

为什么不
键入PitchName=A | B | C |……|G
。和
type Pitch=Pitch PitchName PitchLevel
。然后
somePitch=pitchg3
如果第一和弦和第二和弦是Chord类型,那么
firstChord={}
将是非法的,因为{}不是Chord类型。为什么不
typepitchname=A | B | C |……|G
。和
type Pitch=Pitch PitchName PitchLevel
。然后
somePitch=Pitch g3
如果firstChord和secondChord是Chord类型,那么
firstChord={}
将是非法的,因为{}不是Chord类型。