Rich
Rich

Reputation: 5731

Elm: How does Debug.log work?

I'm logging the a and b values of a foldl.

words = ["mate", "bro", "bruv"]

sum2 = List.foldl
    (\a b ->
        Debug.log(toString <| a)
        Debug.log(toString <| b)
        a
    ) "guv" words

It works as expected, but I can't understand the output:

"mate": <function>
"guv": "mate"
"bro": <function>
"mate": "bro"
"bruv": <function>
"bro": "bruv"

Why is it outputting a as a <function>, and what is it outputting b as a:b ?

Upvotes: 12

Views: 7812

Answers (1)

dontexist
dontexist

Reputation: 5642

Debug.log takes two arguments, a tag string, which can be anything, and then the value to be logged. Updating your code like this might work:

words = ["mate", "bro", "bruv"]

sum2 = List.foldl
    (\a b 
        Debug.log "Value of a: " a
        Debug.log "Value of b: " b
        a
    ) "guv" words

Although, come to think of it, I think you need to do a bit of a trick for logging values that you don't want to return, like so:

words = ["mate", "bro", "bruv"]

sum2 = List.foldl
    (\a b ->
        let
            _ = Debug.log "Value of a: " a               
            _ = Debug.log "Value of b: " b
        in
            a
    ) "guv" words

Upvotes: 14

Related Questions