Opened 6 years ago

Closed 5 years ago

#3399 closed proposal (wontfix)

Generalize the type of Data.List.{deleteBy, deleteFirstsBy}

Reported by: iago Owned by:
Priority: normal Milestone: Not GHC
Component: libraries/base Version: 6.10.4
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Revisions:

Description

Better (more general) type signatures would be

deleteBy :: (b -> a -> Bool) -> b -> [a] -> [a]
deleteFirstsBy :: (b -> a -> Bool) -> [b] -> [a] -> [a]

Example of why it is useful:

deleteBy ((==) . fst) 1 [(1,'a'), (2, 'b')]

Change History (6)

comment:1 Changed 6 years ago by iago

Sorry, the example was wrong, it should be

deleteBy (\x -> (x ==) . fst) 1 [(1,'a'), (2, 'b')]

or

deleteBy ((==) . fst) (1,'a') [1,2]

comment:2 Changed 6 years ago by maeder

Your proposal shows to me that there is another generalization of delete (I think a better one than deleteBy), let's call it deleteFirst which is a variant of filter with a negated predicate, that stops immediately after one element has been removed.

deleteFirst :: (a -> Bool) -> [a] -> [a]
deleteFirst p l =
  let (ft, rt) = break p l
  in ft ++ drop 1 rt

delete :: Eq a => a -> [a] -> [a]
delete a = deleteFirst (== a)

(In the type signature of your above function deleteFirstsBy the argument lists '[a]' and '[b]' should be swapped, because the second argument is taken away from the first one.)

Maybe a better alternative for deleteFirstsBy would be deleteFirsts:

deleteFirsts :: [a -> Bool] -> [a] -> [a]
deleteFirsts fs l = foldr deleteFirst l fs

deleteFirstsBy :: (b -> a -> Bool) -> [a] -> [b] -> [a]
deleteFirstsBy eq l r = deleteFirsts (map eq r) l

But, since hardly anybody wants to touch Data.List, I would vote for closing this ticket

comment:3 Changed 6 years ago by iago

The proposal doesn't require change code itself, it is just use the more general type signature instead of restrict it unnecessary. The change wouldn't break any code.

comment:4 Changed 6 years ago by maeder

Another argument against this proposal is, that an "equality" function taking different argument types is rather unintuitive. Who actually needs this generalization and is able to understand this from the documentation? Orthogonality would also require to check if the other "By"-functions can be generalized to swallow an "(b -> a -> Bool)" argument. (Indeed intersectBy could be generalized, too.)

comment:5 Changed 6 years ago by igloo

  • difficulty set to Unknown
  • Milestone set to Not GHC

comment:6 Changed 5 years ago by igloo

  • Resolution set to wontfix
  • Status changed from new to closed
  • Type of failure set to None/Unknown

Looks like an abandoned proposal

Note: See TracTickets for help on using tickets.