Skip to content

Instantly share code, notes, and snippets.

@guibou
Created July 25, 2021 18:08
Show Gist options
  • Select an option

  • Save guibou/e05e42314f8e749e64aea8aa97bdd9fc to your computer and use it in GitHub Desktop.

Select an option

Save guibou/e05e42314f8e749e64aea8aa97bdd9fc to your computer and use it in GitHub Desktop.
`foldLR` automatically uses `foldl` or `foldr` depending on the folding function.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
class Fold f t acc | f -> t acc where
foldLR :: Foldable foldable => f -> acc -> foldable t -> acc
instance Fold (t -> acc -> acc) t acc where
foldLR = foldr
instance Fold (acc -> t -> acc) t acc where
foldLR = foldl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment