Easy Hoogle usage from bash

What is hoogle?

Hoogle is Google for searching of Haskell functions. You could ask it for function name or its signature. There is available command hoogle, which could be installed using stack:
$ stack install hoogle

Using hoogle from command line {#using-hoogle-from-command-line} To

hoogle a function you could just pass it as parameter:
$ hoogle fmap
Prelude fmap :: Functor f => (a -> b) -> f a -> f b
Data.Functor fmap :: Functor f => (a -> b) -> f a -> f b
Control.Monad fmap :: Functor f => (a -> b) -> f a -> f b
Control.Monad.Instances fmap :: Functor f => (a -> b) -> f a -> f b
Data.Traversable fmapDefault :: Traversable t => (a -> b) -> t a -> t b
Network.Stream fmapE :: (a -> Result b) -> IO (Result a) -> IO (Result b) or pass its signature:

$ hoogle "(a -> b -> c) -> [a] -> [b] -> [c]"
Prelude zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Data.List zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Control.Applicative liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
Control.Monad liftM2 :: Monad m => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r
Prelude scanl :: (a -> b -> a) -> a -> [b] -> [a]
Data.List scanl :: (a -> b -> a) -> a -> [b] -> [a]
Prelude scanr :: (a -> b -> b) -> b -> [a] -> [b]
Data.List scanr :: (a -> b -> b) -> b -> [a] -> [b]
Data.List deleteFirstsBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
Data.List intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
... But it shows only list of the signatures of the functions. Sometimes we want to see more information about function. If you use option 

-i, then additional information will be shown:
$ hoogle -i “(a -> b -> c) -> [a] -> [b] -> [c]”
Prelude zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

zipWith generalises zip by zipping with the function given as the first argument, instead of a tupling function. For example, zipWith (+) is applied to two lists to produce the list of corresponding sums.

From package base
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] And again it is not enough, because it shows only documentation of first function from the list. We have to move a counter to see documentation of further functions, e. g. to show information about third item from the list: 

$ hoogle -i -s 3 "(a -> b -> c) -> [a] -> [b] -> [c]"
Control.Applicative liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c

Lift a binary function to actions.

From package base
liftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c

Easier hoogle usage in bash {#easier-hoogle-usage-in-bash} It is cumbersome to count each time you want to read info about further functions, so I have prepared bash function which makes it easier. To search for function type:

$ hoog "(a->b) -> f a -> f b"
1) Data.Traversable fmapDefault :: Traversable t => (a -> b) -> t a -> t b
2) Prelude fmap :: Functor f => (a -> b) -> f a -> f b
3) Data.Functor fmap :: Functor f => (a -> b) -> f a -> f b
4) Control.Monad fmap :: Functor f => (a -> b) -> f a -> f b
5) Control.Monad.Instances fmap :: Functor f => (a -> b) -> f a -> f b
6) Data.Functor (<$>) :: Functor f => (a -> b) -> f a -> f b
7) Control.Applicative (<$>) :: Functor f => (a -> b) -> f a -> f b Each function will have its counter at the beginning and just add its number at the end of command to show more information about specific function: 

$ hoog "(a->b) -> f a -> f b" 6
Searching for: (a -> b) -> f a -> f b
Data.Functor (<$>) :: Functor f => (a -> b) -> f a -> f b

An infix synonym for fmap.

From package base
(<$>) :: Functor f => (a -> b) -> f a -> f b

How to install hoog command? {#how-to-install-hoog-command} Command is available

here. To use this command just add it to your ~/bashrc file.

You May Also Like

Grails with Spock unit test + IntelliJ IDEA = No thread-bound request found

During my work with Grails project using Spock test in IntelliJ IDEA I've encountered this error:

java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.codehaus.groovy.grails.plugins.web.api.CommonWebApi.currentRequestAttributes(CommonWebApi.java:205)
at org.codehaus.groovy.grails.plugins.web.api.CommonWebApi.getParams(CommonWebApi.java:65)
... // and few more lines of stacktrace ;)

It occurred when I tried to debug one of test from IDEA level. What is interesting, this error does not happen when I'm running all test using grails test-app for instance.

So what was the issue? With little of reading and tip from Tomek Kalkosiński (http://refaktor.blogspot.com/) it turned out that our test was missing @TestFor annotation and adding it solved all problems.

This annotation, according to Grails docs (link), indicates Spock what class is being tested and implicitly creates field with given type in test class. It is somehow strange as problematic test had explicitly and "manually" created field with proper controller type. Maybe there is a problem with mocking servlet requests?