lpaste_ 2017-02-23 02:46:41
halogenandtoast pasted "Game.hs" at http://lpaste.net/352904
trkpa 2017-02-23 02:46:48
Any insight into the usefulness of id and const? I'm new to haskell so I haven't had a reason to use them yet
halogenandtoast 2017-02-23 02:47:11
trkpa: I use them all the time...
halogenandtoast 2017-02-23 02:47:32
one sec I'll get an example
halogenandtoast 2017-02-23 02:48:10
let's say I have a variable request of type Either HttpException B.ByteString
halogenandtoast 2017-02-23 02:48:20
if the request fails I want to return Nothing
barrucadu 2017-02-23 02:48:22
halogenandtoast: Is the number of flags fixed? If so, why not just have gameFlag1, gameFlag2, etc inside GameState, rather than a list?
halogenandtoast 2017-02-23 02:48:29
`either (const Nothing) (Just . toStrict . decodeUtf8 . B64.encode) request`
halogenandtoast 2017-02-23 02:49:33
barrucadu: it is, nice observation! Having them in a list does make it convenient for priting, but they have no other dependencies on being in a list. They're still difficult to update though
trkpa 2017-02-23 02:49:35
halogenandtoast: Why wouldn't just Nothing work there?
halogenandtoast 2017-02-23 02:49:47
:t either
lambdabot 2017-02-23 02:49:49
(a -> c) -> (b -> c) -> Either a b -> c
halogenandtoast 2017-02-23 02:50:11
Just Nothing is not an (a -> c)
halogenandtoast 2017-02-23 02:50:15
:t Just Nothing
lambdabot 2017-02-23 02:50:17
Maybe (Maybe a)
trkpa 2017-02-23 02:50:27
ahhhh
trkpa 2017-02-23 02:50:29
ok
halogenandtoast 2017-02-23 02:50:31
or sorry you mean Nothing not Just Nothing lol
trkpa 2017-02-23 02:50:33
perfect example
barrucadu 2017-02-23 02:50:49
I occasionally use `id` with `maybe`
barrucadu 2017-02-23 02:50:54
:t maybe id
halogenandtoast 2017-02-23 02:50:56
same
lambdabot 2017-02-23 02:50:56
(a -> a1 -> a1) -> Maybe a -> a1 -> a1
barrucadu 2017-02-23 02:51:02
For example:
barrucadu 2017-02-23 02:51:13
> maybe id (:) (Just 5) [4,3,2,1]
lambdabot 2017-02-23 02:51:15
[5,4,3,2,1]
barrucadu 2017-02-23 02:51:22
> maybe id (:) Nothing [4,3,2,1]
lambdabot 2017-02-23 02:51:25
[4,3,2,1]
halogenandtoast 2017-02-23 02:51:38
trkpa: another example with const
halogenandtoast 2017-02-23 02:51:40
`importNhkArticle article = maybe (insert_ story) (const (return ())) =<< getBy (UniqueNewsId nid)`
barrucadu 2017-02-23 02:52:22
`id` and `const` typically aren't things you use by themselves, because they can be reduced very simply. They're useful when used with other higher-order functions.
trkpa 2017-02-23 02:53:19
Gotcha.
halogenandtoast 2017-02-23 02:54:18
barrucadu: well I sometime use const to stub functions
trkpa 2017-02-23 02:54:29
I was trying to understand its use in the class def of Functor. (<$) = fmap . const
halogenandtoast 2017-02-23 02:54:33
for example gameOver :: GameState -> Bool gameOver = const True
halogenandtoast 2017-02-23 02:54:56
there I'm using it by itself sorta
barrucadu 2017-02-23 02:55:34
Yeah, I usually use _ in that case, unless there are other cases in the function which force me to a specific number of patterns
halogenandtoast 2017-02-23 02:55:50
trkpa: Wait until you get to the Const type
halogenandtoast 2017-02-23 02:55:56
that's even more magical!
halogenandtoast 2017-02-23 02:56:08
newtype Const a b = Const { getConst :: a }
Rembane 2017-02-23 02:56:17
:t fmap . ($)
lambdabot 2017-02-23 02:56:20
Functor f => (a -> b) -> f a -> f b
Rembane 2017-02-23 02:56:28
Hm... no.
Rembane 2017-02-23 02:56:38
:t fmap . const
lambdabot 2017-02-23 02:56:41
Functor f => b -> f b1 -> f b
Rembane 2017-02-23 02:56:49
Ah. Yes.
halogenandtoast 2017-02-23 02:57:20
Yeah the type of ($) is the best, because no one I teach it to gets it by looking at the type
halogenandtoast 2017-02-23 02:57:25
:t ($)
lambdabot 2017-02-23 02:57:27
(a -> b) -> a -> b
halogenandtoast 2017-02-23 03:00:37
:t flip id
lambdabot 2017-02-23 03:00:39
b -> (b -> c) -> c
halogenandtoast 2017-02-23 03:00:41
is also a good one
halogenandtoast 2017-02-23 03:01:37
although I'd love to see where that is actually useful...
barrucadu 2017-02-23 03:04:28
All this talk of types has reminded me I need to try to reduce some code to a minimal example. I've found a case where turning on TypeFamilies breaks some code unless I add an explicit type signature, and I suspect that GHC is wrong to compile it without the type signature because of the monomorphism restriction.
trkpa 2017-02-23 03:09:10
Any opinion on this book? https://www.amazon.com/Programming-Haskell-Graham-Hutton/dp/1316626229/ref=sr_1_2?ie=UTF8&qid=1487858925&sr=8-2&keywords=haskell
halogenandtoast 2017-02-23 03:30:03
trkpa: I have "heard" good things, but I haven't read it
halogenandtoast 2017-02-23 03:30:12
I hate books though
halogenandtoast 2017-02-23 03:30:37
I'm currently trying to read https://www.amazon.com/Category-Theory-Oxford-Logic-Guides/dp/0199237182 though