[Haskell-cafe] [announcement] filtrable: class of filtrable containers
Oleg Grenrus
oleg.grenrus at iki.fi
Wed Feb 17 08:49:37 UTC 2016
Hi M,
- I’d also like to see instances for types in containers, unordered-containers, vector and semigroup.
- The OtherLicense seems a bit scary (even the contents aren’t),
is there a good reason why you don’t use more familiar MIT or BSD3?
If you don’t mind I can make a PR for the instances.
And one more comment:
The law:
filter f = mapMaybe (liftA2 (<$) id (guard ∘ f))
is very hard to understand.
filter f = mapMaybe (\x -> if f x then Just x else Nothing)
is longer, but IMHO much simpler. Or if you really want to code golf, then maybe:
filter f = mapMaybe (mfilter f . Just)
- Oleg
> On 17 Feb 2016, at 10:32, Joachim Breitner <mail at joachim-breitner.de> wrote:
>
> Hi M,
>
> Am Dienstag, den 16.02.2016, 23:02 -0800 schrieb M Farkas-Dyck:
>> I quietly posted this library to Hackage nearly a year ago, but lately
>> learned that some seeking such a package had difficulty finding it, so
>> i announce it now ☺
>>
>> https://hackage.haskell.org/package/filtrable
>>
>> class Functor f => Filtrable f where
>> mapMaybe :: (a -> Maybe b) -> f a -> f b
>> catMaybes :: f (Maybe a) -> f a
>> filter :: (a -> Bool) -> f a -> f a
>>
>> For laws, see docs on Hackage.
>
> You might want to add laws in the style of
>
> If this is also a Foldable, then
> toList . mapMaybe f = mapMapybe f . toList
> toList . catMaybes = catMaybes . toList
> toList . filter f = filter f . toList
>
> which would fix the behavior quite tightly.
>
> I wonder if these laws (together with a “well behaved” Foldable) still
> allow any unexpected behavior.
>
> And also whether they follow from your laws (but I don’t think so;
> mapMaybe could do something mean such as duplicating elements if there
> is at least one Nothing in the result).
>
> Do you plan to add instances for all the other data structures in base
> that are filtrable?
>
> Greetings,
> Joachim
>
> --
> Joachim “nomeata” Breitner
> mail at joachim-breitner.de • https://www.joachim-breitner.de/
> XMPP: nomeata at joachim-breitner.de • OpenPGP-Key: 0xF0FBF51F
> Debian Developer: nomeata at debian.org
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 842 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20160217/2ac06f6a/attachment.sig>
More information about the Haskell-Cafe
mailing list