Lenses are not always better:

fresh :: FDState s c -> (Int, FDState s c)
fresh = id &&& id
  >>> first (^. nextId)
  >>> (\(i, s) -> (i, s & alive %~ Set.insert i))
  >>> second (nextId %~ (+ 1))

fresh :: FDState s c -> (Int, FDState s c)
fresh s@FDState {_nextId = i, _alive = as} =
  (i, s {_nextId = i + 1, _alive = Set.insert i as})

#haskell

  • @bitcrafter
    link
    English
    1
    edit-2
    1 year ago

    That’s not really an argument against lenses so much as an argument against extreme point-free style.

    fresh :: FDState s c -> (Int, FDState s c)
    fresh s = (i, inc_s & alive %~ Set.insert i)
      where (i, inc_s) = s & nextID <<+~ 1
    

    Edit: Fixed some of the operators being replaced with HTML entities. Edit 2: Hmm, they keep showing up fine in Preview but then getting replaced again. I’m not sure what I’m supposed to do to make them work.