Stream: beginners

Topic: Set encoding


view this post on Zulip Luke Boswell (Dec 10 2022 at 05:13):

Thought I would share this for anyone in a similar situaion. I was wanting to print my state out, and so I made a helper to encode it to Json which works really well. However Set doesn't implement Encoding.

Tip: Set does not implement Encoding.

To workaround this I converted my Set to a List and this worked really well.

stateToStr = \state ->

    # Set which doesnt implement encoding??
    encodableState = {
        head: state.head,
        tail: state.tail,
        visits : Set.toList state.visits
    }

    when Str.fromUtf8 (Encode.toBytes encodableState Json.toUtf8) is
        Ok str -> str
        Err _ -> crash "unable to encode state to Json"

Last updated: Jul 05 2025 at 12:14 UTC