[HN Gopher] Functors: Identity, Composition, and fmap
___________________________________________________________________
Functors: Identity, Composition, and fmap
Author : codehakase
Score : 12 points
Date : 2025-04-02 18:01 UTC (3 days ago)
(HTM) web link (codehakase.com)
(TXT) w3m dump (codehakase.com)
| munchler wrote:
| Here's a question to test your understanding of functors: Is the
| Haskell type `Set e` a functor? (Where `Set e` is a set of
| elements of type `e`.)
|
| Does the answer vary depending on the programming language? E.g.
| Is the `Set<'e>` type in F# a functor?
| chrisoverzero wrote:
| Is the answer (ROT13 for spoilers):
|
| V vzntvar gung vg pbhyq abg or orpnhfr Frg erdhverf gur rkgen
| fngvfsnpgvba bs Rd, evtug?
|
| After looking it up: Nu, lrf, ohg Beq. Vagrerfgvat ubj gung
| vzcyrzragngvba qrgnvy yrnxf.
|
| Even later: Unat ba, gurer'f ab jnl vg pbhyq or rira vs gung
| fngvfsnpgvba pbhyq or birepbzr! Vzntvar n frg pbagnvavat sbhe
| naq artngvir sbhe naq lbh znc `fdhner` bire vg... Gur fvmr bs
| gur pbyyrpgvba jbhyq punatr!
| nasso_dev wrote:
| > Gur fvmr bs gur pbyyrpgvba jbhyq punatr!
|
| Ubj vf guvf n ceboyrz gubhtu? Vg qbrfa'g frrz gb ivbyngr nal
| bs gur shapgbe ynjf!
| munchler wrote:
| > Even later: ...
|
| Yes, now you've hit the crux of the problem. Does that
| violate the functor laws?
| branperr wrote:
| Caesar cypher +7
|
| Zv tf buklyzahukpun, ha slhza pu QZ dolyl P't mhtpsphy, pz aoha
| pa dvbsk il. Ylhzvu ilpun pz aoha Zla jhu lzzluaphssf il hu
| Hyyhf, dopjo pz h mbujavy: jvuza zlaThw = (zla) => (mu) => uld
| Zla(zla.rlfz().thw(mu))
|
| Mvy vaoly shunbhnlz, P'k nblzz aopz klwlukz vu pm fvb jhu
| palyhal vu paz lsltluaz.
| munchler wrote:
| I think there are some differences between those two types
| that are potentially relevant.
| tmoertel wrote:
| Do you mean to ask whether `Set` (no `e`) is a functor?
| munchler wrote:
| Yes, I was just trying to be descriptive by including the
| full signature.
| tmoertel wrote:
| Ok. It's just that, as asked, the answer is obviously no
| because if F = `Set e`, F is not a type constructor, so F
| cannot be a functor.
| munchler wrote:
| Understood. I should've left the `e` out, but it's too
| late to edit now. (I love Haskell, but I'm not actually
| very good at it.)
| pxeger1 wrote:
| If `F a = Set e` for some _constant_ e, then F is a
| functor (just a trivial one)
| jackweirdy wrote:
| What a delightful little question!
| munchler wrote:
| Thanks. I ran into it in a real life situation, and was
| surprised that the answer is so nuanced. It had me
| questioning my sanity for a while. :)
| ossopite wrote:
| ROT13
|
| Fb V guvax gur nafjre eribyirf nebhaq gur pbzcbfvgvba ynj. Va
| bgure jbeqf, vf sznc (sznc frg s) t rdhny gb sznc frg (t . s)
| sbe nal s naq t?
|
| V guvax guvf ubyqf bayl vs s naq t ner cher (be znlor whfg vs t
| vf cher?). fb znlor vs lbh vtaber hafnsr bcrengvbaf va unfxryy
| vg'f gehr? vg'f pregnvayl abg gehr va s#/bpnzy. vzntvar gung s
| vf sha _ -> 0 naq t vf sha _ -> arkg_vag_sebz_zhgnoyr_pbhagre
| (). gura sznc frg (t . s) qbrfa'g punatr gur fvmr bs gur frg
| juvyr sznc (sznc frg s) t cebqhprf n fvatyrgba frg.
| munchler wrote:
| Let's assume all the functions are pure. Otherwise, even
| Maybe would not be a functor.
| ossopite wrote:
| Gung'f snve. Gura V guvax, abg pbafvqrevat nal cnegvphyne
| cebtenzzvat ynathntr, gur nafjre qrcraqf ba ubj Frg
| qrgrezvarf rdhnyvgl bs ryrzragf. Vs vg hfrf fbzr
| vzcyrzragngvba bs rdhnyvgl/pbzcnevfba sbe glcr r gung pna
| fnl gung fbzr inyhrf bs glcr r ner rdhny gung ner
| arireguryrff qvfgvathvfunoyr, gura Frg vfa'g n shapgbe
| munchler wrote:
| Irel tbbq. V guvax gung'f gur rffrapr bs gur nafjre. Gur
| shapgbe ynjf unir na vzcyvpvg qrcraqrapl ba gur
| fhofgvghgvba cebcregl bs rdhnyvgl, juvpu thnenagrrf gung
| k = l vzcyvrf s(k) = s(l).
| ossopite wrote:
| I hadn't heard of this property before, thanks for
| introducing me to it!
|
| V gubhtug vg fbhaqrq fhecevfvat sbe n glcr r gb orunir
| guvf jnl jura V jebgr zl pbzzrag, ohg abj V ernyvfr gung
| vg zvtug or pbzzba va nal vzcyrzragngvba bs rdhnyf gung
| uvqrf vagreany fgngr, n fvzcyr rknzcyr orvat n frg/znc
| qngnglcr jubfr rdhnyvgl vzcyrzragngvba vtaberf vafregvba
| beqre ohg jubfr vgrengvba beqre qrcraqf ba vg.
| TheMatten wrote:
| Another fun one in case someone's interested: the post shows an
| example of a type that may sometimes lack the inner value of a
| given type (`Maybe a`), but what about type that _never_
| contains such inner value? Would it be useful? And could you
| define some interface in style of `Functor` class that would
| _prove_ this property?
___________________________________________________________________
(page generated 2025-04-05 23:01 UTC)