根据另一条记录更新模型中的一条记录
Updating a record in model based on another record
我有这样的东西:
type alias Record =
{ id : String
, nextId : String
, value : String
, result : Int
}
type alias Model = List Record
model = [
{
...
},
...
]
...
update id model =
List.map (updateRecord id) model
updateRecord id record =
if record.id == id then
{record | result = 1}
else
record
基本上,当我用一些 id
调用 update
时,我需要的是用 id
(我已经拥有的那部分)更新记录,但也从记录的nextId
,找到另一条带record.id == nextId
的记录,将第一条记录的value
传给第二条
基本上我要找的是用 JS 写的:
function update(id, model) {
var nextId, value;
var newModel1 = model.map(function(record) {
if (record.id === id) {
record.result = 1;
nextId = record.nextId;
value = record.value;
return record;
} else {
return record;
}
});
var newModel2 = model.map(function(record) {
if (record.id === nextId) {
record.value = value;
return record;
} else {
return record;
}
});
return newModel2;
}
如何实现?
这应该能满足您的需求:
type alias UpdateNext =
{ nextId : String
, value : String
}
update id model =
let
(matchedModel, nextToUpdate) = match id model
in
case nextToUpdate of
Nothing ->
model
Just next ->
updateNext next matchedModel
match : String -> Model -> (Model, Maybe UpdateNext)
match id model =
case model of
[] ->
([], Nothing)
(x::xs) ->
if x.id == id then
( { x | result = 1 } :: xs
, Just { nextId = x.nextId, value = x.value }
)
else
let (ys, updateNext) = match id xs
in (x :: ys, updateNext)
updateNext : UpdateNext -> Model -> Model
updateNext next model =
case model of
[] ->
[]
(x::xs) ->
if x.id == next.nextId then
{ x | value = next.value } :: xs
else
x :: updateNext next xs
这将比 List.map
更有效,因为它在匹配结果时会短路处理。
我有这样的东西:
type alias Record =
{ id : String
, nextId : String
, value : String
, result : Int
}
type alias Model = List Record
model = [
{
...
},
...
]
...
update id model =
List.map (updateRecord id) model
updateRecord id record =
if record.id == id then
{record | result = 1}
else
record
基本上,当我用一些 id
调用 update
时,我需要的是用 id
(我已经拥有的那部分)更新记录,但也从记录的nextId
,找到另一条带record.id == nextId
的记录,将第一条记录的value
传给第二条
基本上我要找的是用 JS 写的:
function update(id, model) {
var nextId, value;
var newModel1 = model.map(function(record) {
if (record.id === id) {
record.result = 1;
nextId = record.nextId;
value = record.value;
return record;
} else {
return record;
}
});
var newModel2 = model.map(function(record) {
if (record.id === nextId) {
record.value = value;
return record;
} else {
return record;
}
});
return newModel2;
}
如何实现?
这应该能满足您的需求:
type alias UpdateNext =
{ nextId : String
, value : String
}
update id model =
let
(matchedModel, nextToUpdate) = match id model
in
case nextToUpdate of
Nothing ->
model
Just next ->
updateNext next matchedModel
match : String -> Model -> (Model, Maybe UpdateNext)
match id model =
case model of
[] ->
([], Nothing)
(x::xs) ->
if x.id == id then
( { x | result = 1 } :: xs
, Just { nextId = x.nextId, value = x.value }
)
else
let (ys, updateNext) = match id xs
in (x :: ys, updateNext)
updateNext : UpdateNext -> Model -> Model
updateNext next model =
case model of
[] ->
[]
(x::xs) ->
if x.id == next.nextId then
{ x | value = next.value } :: xs
else
x :: updateNext next xs
这将比 List.map
更有效,因为它在匹配结果时会短路处理。