Discussion: lookups in maps

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Discussion: lookups in maps

David Feuer
Discussion has gotten going again about harmonizing between the names findWithDefault and lookupDefault in containers and unordered-containers:

    findWithDefault :: Ord k
      => a -> k -> Map k a -> a
    lookupDefault :: (Eq k, Hashable k)
      => a -> k -> HashMap k a -> a

I've been fairly unenthusiastic about adding extra names for this function in one package or the other, in large part because I don't like the function. I'd much rather have something in the spirit of `either`, `maybe`, `bool`, etc.:

    lurkup :: Ord k
       => r -> (a -> r) -> k -> Map k a -> r
    lurkup r f k = maybe r f . lookup k
    lookup k = lurkup Nothing Just k

Unfortunately, I haven't been able to come up with a decent name for this function. Can anyone help?

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Discussion: lookups in maps

Oliver Charles-3
Could you share why you are more enthusiastic about such a function?

On Mon, 8 Jun 2020, at 8:07 PM, David Feuer wrote:
Discussion has gotten going again about harmonizing between the names findWithDefault and lookupDefault in containers and unordered-containers:

    findWithDefault :: Ord k
      => a -> k -> Map k a -> a
    lookupDefault :: (Eq k, Hashable k)
      => a -> k -> HashMap k a -> a

I've been fairly unenthusiastic about adding extra names for this function in one package or the other, in large part because I don't like the function. I'd much rather have something in the spirit of `either`, `maybe`, `bool`, etc.:

    lurkup :: Ord k
       => r -> (a -> r) -> k -> Map k a -> r
    lurkup r f k = maybe r f . lookup k
    lookup k = lurkup Nothing Just k

Unfortunately, I haven't been able to come up with a decent name for this function. Can anyone help?
_______________________________________________
Libraries mailing list


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Discussion: lookups in maps

Ivan Lazar Miljenovic
I know I've had cases where I didn't have a good default `a` but did have an `r` (and I wanted to apply a function to the result if there was a value in the Map anyway).


On mobile; please excuse any tpyos.

On Tue, 9 Jun 2020, 5:49 am Oliver Charles, <[hidden email]> wrote:
Could you share why you are more enthusiastic about such a function?

On Mon, 8 Jun 2020, at 8:07 PM, David Feuer wrote:
Discussion has gotten going again about harmonizing between the names findWithDefault and lookupDefault in containers and unordered-containers:

    findWithDefault :: Ord k
      => a -> k -> Map k a -> a
    lookupDefault :: (Eq k, Hashable k)
      => a -> k -> HashMap k a -> a

I've been fairly unenthusiastic about adding extra names for this function in one package or the other, in large part because I don't like the function. I'd much rather have something in the spirit of `either`, `maybe`, `bool`, etc.:

    lurkup :: Ord k
       => r -> (a -> r) -> k -> Map k a -> r
    lurkup r f k = maybe r f . lookup k
    lookup k = lurkup Nothing Just k

Unfortunately, I haven't been able to come up with a decent name for this function. Can anyone help?
_______________________________________________
Libraries mailing list

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Discussion: lookups in maps

Henning Thielemann

On Tue, 9 Jun 2020, Ivan Lazar Miljenovic wrote:

> I know I've had cases where I didn't have a good default `a` but did
> have an `r` (and I wanted to apply a function to the result if there was
> a value in the Map anyway).

I think I had these cases, too, but "maybe . Map.lookup" would also be ok?
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Discussion: lookups in maps

Andreas Abel
In reply to this post by David Feuer
lurkup is essentially lookup returning a CPS-version (Church encoding)
of Maybe a.

   type CMaybe a = forall r. r -> (a -> r) -> r
   lurkup' :: k -> Map k a -> CMaybe a

Is it worth to add a new name for this (above the Fairbairn threshhold)?

I am quite happy with writing stuff like

    fromMaybe d $ Map.lookup k m

On 2020-06-08 21:07, David Feuer wrote:
> lurkup :: Ord k
>         => r -> (a -> r) -> k -> Map k a -> r
>      lurkup r f k = maybe r f . lookup k
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Discussion: lookups in maps

Keith
In reply to this post by David Feuer
I usually end up writing your 'lurkup' when I use maps, so I'd like see it included.

I don't have a better name than 'lookupWith'.

And, yeah, lookupDefault has not been useful in practice.

Sent from my phone with K-9 Mail.

On June 8, 2020 7:07:13 PM UTC, David Feuer <[hidden email]> wrote:
Discussion has gotten going again about harmonizing between the names findWithDefault and lookupDefault in containers and unordered-containers:

    findWithDefault :: Ord k
      => a -> k -> Map k a -> a
    lookupDefault :: (Eq k, Hashable k)
      => a -> k -> HashMap k a -> a

I've been fairly unenthusiastic about adding extra names for this function in one package or the other, in large part because I don't like the function. I'd much rather have something in the spirit of `either`, `maybe`, `bool`, etc.:

    lurkup :: Ord k
       => r -> (a -> r) -> k -> Map k a -> r
    lurkup r f k = maybe r f . lookup k
    lookup k = lurkup Nothing Just k

Unfortunately, I haven't been able to come up with a decent name for this function. Can anyone help?

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Discussion: lookups in maps

Jon Fairbairn
In reply to this post by Andreas Abel
Andreas Abel <[hidden email]> writes:

> lurkup is essentially lookup returning a CPS-version (Church
> encoding) of Maybe a.
>
>   type CMaybe a = forall r. r -> (a -> r) -> r
>   lurkup' :: k -> Map k a -> CMaybe a
>
> Is it worth to add a new name for this (above the Fairbairn threshhold)?

I don’t think so :-)

> I am quite happy with writing stuff like
>
>    fromMaybe d $ Map.lookup k m

There might be a case for a flipped infix version of this. I see
that there are several in various libraries including `orElse`
and (?:) more than once.


> On 2020-06-08 21:07, David Feuer wrote:
>> lurkup :: Ord k
>>         => r -> (a -> r) -> k -> Map k a -> r
>>      lurkup r f k = maybe r f . lookup k
> _______________________________________________
> Libraries mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

--
Jón Fairbairn                                 [hidden email]


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries