flip1 through flip9, useful?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

flip1 through flip9, useful?

Christian Marie
I have defined a bunch of functions like this:

        -- | Move the fourth argument to the first place
        rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)

        -- | Reverse four arguments
        flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)

I decided to upload this as a library to hackage, as I personally find it
useful, especially for writing FFI bindings.

It seems like I can't be the first to write a library like this though, am I
missing something obvious? Is this useful or stupid? Does it exist already?

Full definition here:

https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

attachment0 (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Ivan Lazar Miljenovic
On 14 January 2014 16:48, Christian Marie <[hidden email]> wrote:

> I have defined a bunch of functions like this:
>
>         -- | Move the fourth argument to the first place
>         rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)
>
>         -- | Reverse four arguments
>         flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)
>
> I decided to upload this as a library to hackage, as I personally find it
> useful, especially for writing FFI bindings.
>
> It seems like I can't be the first to write a library like this though, am I
> missing something obvious? Is this useful or stupid? Does it exist already?
>
> Full definition here:
>
> https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs

Except for completeness, I don't see the point of rotate1 and flip1.

I'd also be tempted to possibly shift the module to
Data.Function.Flippers (as flip is re-exported by Data.Function in
base, and it seems like a more valid location to me).

That said, whilst I do use flip upon occasion, I think in the general
case it'd be too easy to abuse these kinds of functions and could be
more difficult to work any errors if you used the wrong one or had
some arguments in the wrong order.

--
Ivan Lazar Miljenovic
[hidden email]
http://IvanMiljenovic.wordpress.com
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Tony Morris-4
In reply to this post by Christian Marie
On 14/01/14 15:48, Christian Marie wrote:
I have defined a bunch of functions like this:

	-- | Move the fourth argument to the first place
	rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)

	-- | Reverse four arguments
	flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)

I decided to upload this as a library to hackage, as I personally find it
useful, especially for writing FFI bindings.

It seems like I can't be the first to write a library like this though, am I
missing something obvious? Is this useful or stupid? Does it exist already?

Full definition here:

https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Why not generalise to any functor?

let flip f a = fmap ($a) f

-- 
Tony Morris
http://tmorris.net/


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Michael Orlitzky
In reply to this post by Ivan Lazar Miljenovic
On 01/14/2014 01:06 AM, Ivan Lazar Miljenovic wrote:

> On 14 January 2014 16:48, Christian Marie <[hidden email]> wrote:
>> I have defined a bunch of functions like this:
>>
>>         -- | Move the fourth argument to the first place
>>         rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)
>>
>>         -- | Reverse four arguments
>>         flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)
>>
>> I decided to upload this as a library to hackage, as I personally find it
>> useful, especially for writing FFI bindings.
>>
>> It seems like I can't be the first to write a library like this though, am I
>> missing something obvious? Is this useful or stupid? Does it exist already?
>>
>> Full definition here:
>>
>> https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs
>
> Except for completeness, I don't see the point of rotate1 and flip1.
>

It could be nice if you're attempting something insane with e.g.
template haskell and don't want n=1 to be a special case.


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Christian Marie
In reply to this post by Tony Morris-4
On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>    Why not generalise to any functor?
>    let flip f a = fmap ($a) f

I didn't think of that, I suppose.

Now that I do, it seems to be a tradeoff between (arguably) less immediately
obvious type signatures and something.

I can't actually think of what that something is yet. Can you think of a real
world use for a functor flip where normal flip wouldn't do?

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe

attachment0 (853 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Alexander Solla
In reply to this post by Tony Morris-4



On Mon, Jan 13, 2014 at 10:57 PM, Tony Morris <[hidden email]> wrote:
On 14/01/14 15:48, Christian Marie wrote:
I have defined a bunch of functions like this:

	-- | Move the fourth argument to the first place
	rotate4 :: (a -> b -> c -> d -> e) -> (d -> a -> b -> c -> e)

	-- | Reverse four arguments
	flip4 :: (a -> b -> c -> d -> e) -> (d -> c -> b -> a -> e)

I decided to upload this as a library to hackage, as I personally find it
useful, especially for writing FFI bindings.

It seems like I can't be the first to write a library like this though, am I
missing something obvious? Is this useful or stupid? Does it exist already?

Full definition here:

https://github.com/christian-marie/flippers/blob/master/src/Control/Flippers.hs



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Why not generalise to any functor?

let flip f a = fmap ($a) f

I don't see that operator as "flip-like", personally.  It might behave as flip in the case of the function functor , but that intuition does not carry over to things like

fmap ($a) [f, g, h] = [f a, g a, h a]

I'd call it "funder", personally.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

John Lato-2
In reply to this post by Christian Marie
On Tue, Jan 14, 2014 at 3:30 PM, Christian Marie <[hidden email]> wrote:
On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>    Why not generalise to any functor?
>    let flip f a = fmap ($a) f

I didn't think of that, I suppose.

Now that I do, it seems to be a tradeoff between (arguably) less immediately
obvious type signatures and something.

I can't actually think of what that something is yet. Can you think of a real
world use for a functor flip where normal flip wouldn't do?

I know I've written "fmap ($a) f" in the past, so I guess any of those would be a use case for a functor flip.  On the other hand, it's only a few characters longer than flip, and arguably more clear, so I'm not convinced that generalizing flip is useful.  YMMV.

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Stijn van Drongelen
In reply to this post by Christian Marie
On Wed, Jan 15, 2014 at 12:30 AM, Christian Marie <[hidden email]> wrote:
On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>    Why not generalise to any functor?
>    let flip f a = fmap ($a) f

I didn't think of that, I suppose.

Now that I do, it seems to be a tradeoff between (arguably) less immediately
obvious type signatures and something.

I can't actually think of what that something is yet. Can you think of a real
world use for a functor flip where normal flip wouldn't do?


I think it's mostly a skeleton to use for generalizing flip. Right now, you have

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

so if you take (f ~ (->) c), you have

    \f a -> fmap ($a) f :: (c -> a -> b) -> (a -> c -> b)

For flips of higher arities, you would choose a different `f`, like (f ~ (->) d . (->) c):

    \f a -> fmap ($a) f :: (c -> d -> a -> b) -> (a -> c -> d -> b)

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Jason Dagit-3
While I don't think the overall idea is good*, I do wonder if the explicit numbers in the names could be removed.

Here are some approaches to writing N-ary zipWith that might give you inspiration:

Both have links to the original sources. The first one depends on closed type families and the other one still uses numerals but they are just names for special functions that makes it all hold together.

* I don't think we should encourage use of things like flip8 in 'real code' because it's very likely to make the code completely incomprehensible. I DO think it's a fun and interesting exercise to generalizing flip to N-ary.

Jason

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Tom Ellis
In reply to this post by Tony Morris-4
On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
> Why not generalise to any functor?
>
> let flip f a = fmap ($a) f

FYI, this is Control.Lens.??

    http://hackage.haskell.org/package/lens-3.9.2/docs/src/Control-Lens-Combinators.html#%3F%3F

Tom

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

Tony Morris-4
Indeed! I offered the question as a coaching exercise :)

On 15/01/14 19:56, Tom Ellis wrote:

> On Tue, Jan 14, 2014 at 04:57:14PM +1000, Tony Morris wrote:
>> Why not generalise to any functor?
>>
>> let flip f a = fmap ($a) f
> FYI, this is Control.Lens.??
>
>     http://hackage.haskell.org/package/lens-3.9.2/docs/src/Control-Lens-Combinators.html#%3F%3F
>
> Tom
>
> _______________________________________________
> Haskell-Cafe mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/haskell-cafe


--
Tony Morris
http://tmorris.net/

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: flip1 through flip9, useful?

João Cristóvão
In reply to this post by Stijn van Drongelen
For flips of higher arities, you would choose a different `f`, like (f ~ (->) d . (->) c):

    \f a -> fmap ($a) f :: (c -> d -> a -> b) -> (a -> c -> d -> b)

Hi,

Could you give a concrete example of this (for higher arities)?

Thanks,
Joao

_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe