1
submitted 1 year ago* (last edited 1 year ago) by jaror@kbin.social to c/haskell@kbin.social

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

you are viewing a single comment's thread
view the rest of the comments
[-] bitcrafter 1 points 1 year ago* (last edited 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.

this post was submitted on 10 Aug 2023
1 points (100.0% liked)

Haskell

4 readers
1 users here now

**The Haskell programming language community.** Daily news and info about all things Haskell related: practical stuff, theory, types, libraries, jobs, patches, releases, events and conferences and more... ### Links - Get Started with Haskell

founded 1 year ago