Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a name for this function or pattern?

Instead of fmap, which applies a function to a value-in-a-functor:

fmap :: Functor f => (a -> b) -> f a -> f b

I needed a function where the functor has a function and the value is plain:

thing :: Functor f => f (a -> b) -> a -> f b

but I can't find one.

What is this pattern called, where I apply a function-in-a-functor (or in an applicative, or in a monad) to a plain value?

I've implemented it already, I just don't quite understand what I did and why there wasn't already such a function in the standard libraries.

like image 381
Matt Fenwick Avatar asked Apr 25 '12 16:04

Matt Fenwick


People also ask

How do you name a function name?

Short: a function name must be as short as possible so that it's simple to type as well as easy to remember. A function getNumberOfPagesInTheBook() is not good, something like getBookPageCount() is better. Use of prefixes: I always use prefixes in the functions such as getName(), setName(), hasHair(), isBlond(), etc.

How do you name a function in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).

How do you name a function in Python?

Method name in PythonUse only lowercase in method names. An underscore should separate words in a method name. Non-public method name should begin with a single underscore. Use two consecutive underscores at the beginning of a method name, if it needs to be mangled.


1 Answers

You don't need Applicative for this; Functor will do just fine:

apply f x = fmap ($ x) f
-- or, expanded:
apply f x = fmap (\f' -> f' x) f

Interestingly, apply is actually a generalisation of flip; lambdabot replaces flip with this definition as one of its generalisations of standard Haskell, so that's a possible name, although a confusing one.

By the way, it's often worth trying Hayoo (which searches the entirety of Hackage, unlike Hoogle) to see what names a function is often given, and whether it's in any generic package. Searching for f (a -> b) -> a -> f b, it finds flip (in Data.Functor.Syntax, from the functors package) and ($#) (from the synthesizer package) as possible names. Still, I'd probably just use fmap ($ arg) f at the use site.

like image 147
ehird Avatar answered Sep 28 '22 04:09

ehird