Skip to content

GitLab

  • Menu
Projects Groups Snippets
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
  • Sign in / Register
  • GHC GHC
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 4,979
    • Issues 4,979
    • List
    • Boards
    • Service Desk
    • Milestones
    • Iterations
  • Merge requests 470
    • Merge requests 470
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Releases
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Glasgow Haskell Compiler
  • GHCGHC
  • Issues
  • #13573
Closed
Open
Created Apr 13, 2017 by Icelandjack@IcelandjackReporter

Add Foldable1 to base

This is a proposal to add Foldable1 (non-empty Foldable) to base

-- Data.Semigroup.Foldable
class Foldable f => Foldable1 f where
  fold1    :: Semigroup m => f m -> m
  foldMap1 :: Semigroup m => (a -> m) -> (f a -> m)

  -- Possible methods
  head1      :: f a -> a
  last1      :: f a -> a
  toNonEmpty :: f a -> NonEmpty a 

along with instances and function that are only valid for non-empty structures (details: semigroupoids issue #49, github)

import qualified Data.Semigroup as S

minimum1, maximum1 :: (Ord a, Foldable1 f) => f a -> a
minimum1 = S.getMin . foldMap1 S.Min
maximum1 = S.getMax . foldMap1 S.Max

head1, last1 :: Foldable1 f => f a -> a
head1 = S.getFirst . foldMap1 S.First
last1 = S.getLast  . foldMap1 S.Last

foldr1, foldl1 :: Foldable1 f => (a -> a -> a) -> (f a -> a)
foldr1 f = unimprove . foldMap1 (\a -> Diff (f a) a)
foldl1 f = unimprove . getDual . foldMap1 (\a -> Dual $ Diff (flip f a) a)

Adding foldM, foldM_, foldrM and foldlM for non-empty structures is also a possibility.


Currently these are partial functions in Foldable. This proposal does not propose replacing partial Foldable functions.


I wanted to test the waters before submitting it to the libraries mailing list. This may be controversial but it gives us a path to avoid partial functions in Foldable.

Edited Mar 10, 2019 by Icelandjack
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking