r/purescript • • Sep 19 '21

Emulating an imperative stateful foreach loop?

Every now and then I come across a situation in which I need to transform a sequence of elements, say a List, but the transformation isn't independent for each element and I would need to keep additional state during the loop to perform the transformations I need.

Let me try to give a simple example. In C# I could write something like

    private static List<object> transformSomeElementFirstTimeItOccurs(List<object> list) {
      bool done = false;
      var result = new List<object>();
      foreach (var elem in list) {
        if (!done && elem is SomeType) {
          result.Add(transformElement(elem));
          done = true;
        } else {
          result.Add(elem);
        }
      }
      return result;
    }

My naieve attempt at achieving this in PureScript currently looks as follows.

  transformSomeElementFirstTimeItOccurs :: List ElemType -> List ElemType
  transformSomeElementFirstTimeItOccurs elems = reverse (loop elems).result
    where
    loop :: List ElemType -> { done :: Boolean , result :: List ElemType }
    loop = foldl (\{ result, done } elem ->
      case elem of
        SomeValueConstructor t ->
          { result : SomeValueConstructor (if done then t else transformValue t):result
          , done : true }
        _ -> { result : elem:result, done })
      { result : Nil, done : false }

But it feels like it could use some improvement. Is there an idiomatic way to do this kind of thing?

3 Upvotes

8 comments sorted by

View all comments

Show parent comment

1

u/tdammers Sep 19 '21

You would not pay the cost for appending in Haskell, but that's mainly because the list tail can be reused, so instead of appending the remaining items one by one, we just copy one pointer, and both lists end up sharing the common tail.

Laziness makes it so that the appending doesn't happen until it is demanded, but that's not what makes this efficient. OTOH, if we were using a data structure that cannot share tails, like, say, Vector, then laziness would avoid the copy when it is not demanded.