Elm:如何在 foldl 中登录
Elm: How to log inside a foldl
我有以下代码:
findPerson name peeps = List.foldl
(\a b -> case b of
Just _ -> b
Nothing -> if a.name == name then
Just a
else Nothing
) Nothing peeps
我想在 foldl
中记录 a
和 b
的值。我试过:
findPerson : String -> List Person -> Maybe Person
findPerson name peeps = List.foldl
(\a b ->
Debug.log(a)
Debug.log(b)
case b of
Just _ -> b
Nothing -> if a.name == name then
Just a
else Nothing
) Nothing peeps
但是,这会引发错误
I am looking for one of the following things:
a closing paren ')'
whitespace`
我做错了什么,如何记录 foldl
中的值?
您可以使用 let in 块进行调试。
let
_ = Debug.log "a" a
_ = Debug.log "b" b
in
case b of
...
一个函数(或 lambda)只能 return 一次。
Debug.log return 第二个参数没有改变,所以你必须将它与某些东西进行模式匹配 - 因为你不需要两次参数,但是 Debug.log 的副作用,你可以模式将其与 _
匹配(忽略)。
您也可以将 Debug.log
直接放在 case statement
或 if statement
中,原因与@farmio 提到的相同:) - 像这样:
findPerson name peeps = List.foldl
(\a b ->
case ( Debug.log "inspect b: " b ) of
Just _ ->
b
Nothing ->
if ( Debug.log "person name is: " a.name ) == name then
Just a
else
Nothing
) Nothing peeps
不太干净,但有时更有用,因为它更紧凑。
我有以下代码:
findPerson name peeps = List.foldl
(\a b -> case b of
Just _ -> b
Nothing -> if a.name == name then
Just a
else Nothing
) Nothing peeps
我想在 foldl
中记录 a
和 b
的值。我试过:
findPerson : String -> List Person -> Maybe Person
findPerson name peeps = List.foldl
(\a b ->
Debug.log(a)
Debug.log(b)
case b of
Just _ -> b
Nothing -> if a.name == name then
Just a
else Nothing
) Nothing peeps
但是,这会引发错误
I am looking for one of the following things:
a closing paren ')'
whitespace`
我做错了什么,如何记录 foldl
中的值?
您可以使用 let in 块进行调试。
let
_ = Debug.log "a" a
_ = Debug.log "b" b
in
case b of
...
一个函数(或 lambda)只能 return 一次。
Debug.log return 第二个参数没有改变,所以你必须将它与某些东西进行模式匹配 - 因为你不需要两次参数,但是 Debug.log 的副作用,你可以模式将其与 _
匹配(忽略)。
您也可以将 Debug.log
直接放在 case statement
或 if statement
中,原因与@farmio 提到的相同:) - 像这样:
findPerson name peeps = List.foldl
(\a b ->
case ( Debug.log "inspect b: " b ) of
Just _ ->
b
Nothing ->
if ( Debug.log "person name is: " a.name ) == name then
Just a
else
Nothing
) Nothing peeps
不太干净,但有时更有用,因为它更紧凑。