Re: [Haskell-cafe] Data.Map: Values to keys and keys to values

2011-06-16 Thread Brent Yorgey
On Thu, Jun 16, 2011 at 04:17:55PM +0200, Francesco Mazzoli wrote:
> On 16/06/11 15:01, Dmitri O.Kondratiev wrote:
> >Hi,
> >Data.Map has many great functions, yet I could not find the one that
> >allows from one map create another map where keys are values and values
> >are keys of the first one.
> >Something like:
> >transMap:: (Ord k, Ord a) => Map k a -> Map a k
> >
> >Does such function exist?
> >Thanks!
> >
> >
> >
> >___
> >Haskell-Cafe mailing list
> >Haskell-Cafe@haskell.org
> >http://www.haskell.org/mailman/listinfo/haskell-cafe
> 
> What about something like
> 
> transMap :: (Ord k, Ord a) => Map k a -> Map a k
> transMap = M.fromList . map swap . M.toList
> 
> ?

Or, if you want to keep duplicates,

import qualified Data.Set as S
import Control.Arrow (second)

transMap = M.fromListWith S.union . map (second S.singleton . swap) . M.toList

-Brent

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map: Values to keys and keys to values

2011-06-16 Thread Dmitri O.Kondratiev
On Thu, Jun 16, 2011 at 5:38 PM, Johan Tibell wrote:

> On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev 
> wrote:
> > Hi,
> > Data.Map has many great functions, yet I could not find the one that
> allows
> > from one map create another map where keys are values and values are keys
> of
> > the first one.
> > Something like:
> > transMap:: (Ord k, Ord a) => Map k a -> Map a k
>
> I don't think implementing this function in the library would add much
> as it cannot be implemented more efficiently with access to the
> internal representation than it can using the public API. Just write
>
>transMap = M.fromList . map swap . M.toList
>
> and stick it in some utility file.
>
> Johan
>


Yes, this is a good one. Thanks!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map: Values to keys and keys to values

2011-06-16 Thread Johan Tibell
On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev  wrote:
> Hi,
> Data.Map has many great functions, yet I could not find the one that allows
> from one map create another map where keys are values and values are keys of
> the first one.
> Something like:
> transMap:: (Ord k, Ord a) => Map k a -> Map a k

I don't think implementing this function in the library would add much
as it cannot be implemented more efficiently with access to the
internal representation than it can using the public API. Just write

transMap = M.fromList . map swap . M.toList

and stick it in some utility file.

Johan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map: Values to keys and keys to values

2011-06-16 Thread Francesco Mazzoli

On 16/06/11 15:01, Dmitri O.Kondratiev wrote:

Hi,
Data.Map has many great functions, yet I could not find the one that
allows from one map create another map where keys are values and values
are keys of the first one.
Something like:
transMap:: (Ord k, Ord a) => Map k a -> Map a k

Does such function exist?
Thanks!



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


What about something like

transMap :: (Ord k, Ord a) => Map k a -> Map a k
transMap = M.fromList . map swap . M.toList

?

Francesco.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map: Values to keys and keys to values

2011-06-16 Thread Daniel Peebles
Why not make it unlossy and have:

trans :: (Ord k, Ord a) => Map k a -> Map a (Set k)



On Thu, Jun 16, 2011 at 9:10 AM, Johan Tibell wrote:

> On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev 
> wrote:
> > Hi,
> > Data.Map has many great functions, yet I could not find the one that
> allows
> > from one map create another map where keys are values and values are keys
> of
> > the first one.
> > Something like:
> > transMap:: (Ord k, Ord a) => Map k a -> Map a k
> >
> > Does such function exist?
>
> Note that such a function would be lossy as there might be duplicate
> values in the map.
>
> Cheers,
> Johan
>
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map: Values to keys and keys to values

2011-06-16 Thread Johan Tibell
On Thu, Jun 16, 2011 at 3:01 PM, Dmitri O.Kondratiev  wrote:
> Hi,
> Data.Map has many great functions, yet I could not find the one that allows
> from one map create another map where keys are values and values are keys of
> the first one.
> Something like:
> transMap:: (Ord k, Ord a) => Map k a -> Map a k
>
> Does such function exist?

Note that such a function would be lossy as there might be duplicate
values in the map.

Cheers,
Johan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Map: Values to keys and keys to values

2011-06-16 Thread Dmitri O.Kondratiev
Hi,
Data.Map has many great functions, yet I could not find the one that allows
from one map create another map where keys are values and values are keys of
the first one.
Something like:
transMap:: (Ord k, Ord a) => Map k a -> Map a k

Does such function exist?
Thanks!
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map lookup signature

2009-05-12 Thread Ryan Ingram
Maybe is an instance of Monad; the second signature is just more
general than the first.

class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
fail :: String -> m a

Map lookup only uses "return" and "fail"; for Maybe these are defined
as follows:
   return x = Just x
   fail s = Nothing

So it's really the same thing.

  -- ryan

On Mon, May 11, 2009 at 8:37 AM, Nico Rolle  wrote:
> Hi everyone.
>
> The docs in the web on http://www.haskell.org/ghc/docs
> define Data.Map.lookup as follows:
> http://www.haskell.org/ghc/docs
> lookup :: Ord k => k -> Map k a -> Maybe a
> but my version of ghci does i like that:
> Data.Map.lookup :: (Ord k, Monad m) => k -> Data.Map.Map k a -> m a
> but i need the 1. one.
> my version of ghci is 6.8.2
> regards
> ___
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Map lookup signature

2009-05-12 Thread Nico Rolle
Hi everyone.

The docs in the web on http://www.haskell.org/ghc/docs
define Data.Map.lookup as follows:
http://www.haskell.org/ghc/docs
lookup :: Ord k => k -> Map k a -> Maybe a
but my version of ghci does i like that:
Data.Map.lookup :: (Ord k, Monad m) => k -> Data.Map.Map k a -> m a
but i need the 1. one.
my version of ghci is 6.8.2
regards
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map and strictness (was: Is Haskell aGoodChoice for WebApplications?(ANN: Vocabulink))

2009-05-07 Thread Daniel McAllansmith
On Fri, 08 May 2009 00:30:34 Claus Reinke wrote:
> > seq something like size map that will force a traversal of the entire
> > tree, and ensure that the result is actually demanded, ..
> > (Not tested)
>
> and not recommended, either, I'm afraid!-)
>
> |> Actually, I'm unsure how to fix this. For an expression like this:
> |>
> |>Data.Map.delete key map
> |>
> |> how can I use seq (or something else) to sufficiently evaluate the above
> |> to ensure that the value associated with key can be garbage collected?

[snip]

> Anyway;-) You can see that size is actually pre-computed, so there's
> no guarantee that asking for it will traverse the internal representation,

Presumably seq'ing the Maybe you get from looking up the key will be 
sufficient to dereference the key, and will avoid unnecessary traversal of 
unrelated parts of the map.

Dan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map and strictness (was: Is Haskell aGoodChoice for WebApplications?(ANN: Vocabulink))

2009-05-07 Thread Claus Reinke

seq something like size map that will force a traversal of the entire
tree, and ensure that the result is actually demanded, ..
(Not tested)


and not recommended, either, I'm afraid!-)

|> Actually, I'm unsure how to fix this. For an expression like this:
|> 
|>Data.Map.delete key map
|> 
|> how can I use seq (or something else) to sufficiently evaluate the above

|> to ensure that the value associated with key can be garbage collected?

You can ensure that the Map update no longer holds on to the old key
by keeping a copy of the old Map in an unevaluated thunk, but for
garbage collection, you'd also have to make sure that there are no other
unused references to the old Map, and no other references to the value
in that old key. I assume you knew that, but the discussion suggested 
that we should keep making such things explicit.


|> My knowledge of Data.Map is limited to it's haddock documentation.

That won't be sufficient in practice, eg., most of the documentation is 
silent about strictness properties. If you are willing to look a little bit

under the hood, GHCi's :info is your friend:

   Prelude> :m +Data.Map
   Prelude Data.Map> :info Map
   data Map k a
 = Data.Map.Tip
 | Data.Map.Bin !Data.Map.Size !k a !(Map k a) !(Map k a)
   -- Defined in Data.Map
   ...

You can see that the constructors are not exported (GHCi reports
them qualified, even though we've brought Data.Map into scope),
so any use you make of their strictness properties is version dependent.
They are not likely to change soon, and should probably be documented
in the haddock API specification (at least for the abstract constructors
that are actually exported), so that we can rely on them with clear
conscience, but that isn't quite the case at the moment.

Anyway;-) You can see that size is actually pre-computed, so there's
no guarantee that asking for it will traverse the internal representation,
the element type is not stored strictly (which isn't all that unexpected, 
but sometimes surprises because it isn't documented), and everything

else is strict. So, with the current representation, if you force the Map,
its whole structure will be forced, though none of its elements will be.

Hth,
Claus

PS. GHood is useful for observing dynamic strictness properties
   (what is inspected when, with what dependencies and results),
   though it won't help you directly with garbage collection issues
   (it can't observe when a value gets collected).


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


RE: [Haskell-cafe] Data.Map and strictness (was: Is Haskell a GoodChoice for WebApplications?(ANN: Vocabulink))

2009-05-07 Thread Sittampalam, Ganesh
seq something like size map that will force a traversal of the entire
tree, and ensure that the result is actually demanded, e.g. when writing
to a TVar:

do ...
   let map' = Data.Map.delete key map
   size map' `seq` writeTVar tvar map'
   ...

(Not tested)

Note that this also won't force any of the values; it sounds like in
this case you don't want them forced.


-Original Message-
From: haskell-cafe-boun...@haskell.org
[mailto:haskell-cafe-boun...@haskell.org] On Behalf Of Tim Docker
Sent: 07 May 2009 09:46
To: haskell-cafe@haskell.org
Subject: [Haskell-cafe] Data.Map and strictness (was: Is Haskell a
GoodChoice for WebApplications?(ANN: Vocabulink))


Actually, I'm unsure how to fix this. For an expression like this:

Data.Map.delete key map

how can I use seq (or something else) to sufficiently evaluate the above
to ensure that the value associated with key can be garbage collected?
My
knowledge of Data.Map is limited to it's haddock documentation.


-Original Message-
From: haskell-cafe-boun...@haskell.org
[mailto:haskell-cafe-boun...@haskell.org] On Behalf Of Tim Docker
Sent: Thursday, 7 May 2009 6:04 PM
To: haskell-cafe@haskell.org
Subject: RE: [Haskell-cafe] Is Haskell a Good Choice for
WebApplications?(ANN: Vocabulink)

I think that multi-threading in combination with laziness makes space
usage harder to manage. In fact, just today I have discovered a problem
with a long running server process with a subtle space leak.

With a regular process that communicates with the outside world via IO,
I know that the act of communicating a value causes it to be fully
evaluated. However, with a multi threaded process, communication occurs
via writes to TVars/IOVars and nothing gets evaluated. This gives lots
of opportunities for space leaks. In this particularly case cleanup code
was removing a large entry from a map stored in a Tvar. Because that map
is only read infrequently, however, the memory release is delayed.

This is the second such problem I've found. The profiling tools do help
in discovering them, but it still needs a bit of thought and analysis. I
wonder if, for my application, I should work out some means of
deepseqing every value written to a Tvar.

Tim

-Original Message-
From: haskell-cafe-boun...@haskell.org
[mailto:haskell-cafe-boun...@haskell.org] On Behalf Of wren ng thornton
Sent: Thursday, 7 May 2009 2:06 PM
To: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] Is Haskell a Good Choice for Web
Applications?(ANN: Vocabulink)

FFT wrote:
> Anton van Straaten wrote:
> 
> > The app is written for a client under NDA, so a blog about it would 
> > have to be annoyingly vague.
> 
> > No doubt the potential for encountering space leaks goes up as one 
> > writes less pure code, persist more things in memory, and depend on
more libraries.
> 
> Exactly. I'm worried about, e.g. needing to use something as simple as

> a stream of prime numbers (see the recent thread about leaks there)

The issues here are going to be the same in Haskell as in every other
language. There's always a tradeoff between the memory of caching old
results vs the time of recalculating them. At present no language's
RTS/GC is smart enough to make that tradeoff for you, and so memoization
must be done manually.

There are some tricks to help make this easier (e.g. weak pointers), but
the problem will always remain. The only thing that makes this perhaps
trickier than in other languages is that, AFAIK/R, the reflection API to
ask the RTS how it's feeling about memory at any given point isn't
terribly portable (between Haskell compilers) or polished/pretty. Then
again, the other GC languages I've dealt with aren't much better and are
often worse.

--
Live well,
~wren
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

=== 
 Please access the attached hyperlink for an important electronic 
communications disclaimer: 
 http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html 
 
=== 
 
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Map and strictness (was: Is Haskell a Good Choice for WebApplications?(ANN: Vocabulink))

2009-05-07 Thread Tim Docker

Actually, I'm unsure how to fix this. For an expression like this:

Data.Map.delete key map

how can I use seq (or something else) to sufficiently evaluate the above
to ensure that the value associated with key can be garbage collected?
My
knowledge of Data.Map is limited to it's haddock documentation.


-Original Message-
From: haskell-cafe-boun...@haskell.org
[mailto:haskell-cafe-boun...@haskell.org] On Behalf Of Tim Docker
Sent: Thursday, 7 May 2009 6:04 PM
To: haskell-cafe@haskell.org
Subject: RE: [Haskell-cafe] Is Haskell a Good Choice for
WebApplications?(ANN: Vocabulink)

I think that multi-threading in combination with laziness makes space
usage harder to manage. In fact, just today I have discovered a problem
with a long running server process with a subtle space leak.

With a regular process that communicates with the outside world via IO,
I know that the act of communicating a value causes it to be fully
evaluated. However, with a multi threaded process, communication occurs
via writes to TVars/IOVars and nothing gets evaluated. This gives lots
of opportunities for space leaks. In this particularly case cleanup code
was removing a large entry from a map stored in a Tvar. Because that map
is only read infrequently, however, the memory release is delayed.

This is the second such problem I've found. The profiling tools do help
in discovering them, but it still needs a bit of thought and analysis. I
wonder if, for my application, I should work out some means of
deepseqing every value written to a Tvar.

Tim

-Original Message-
From: haskell-cafe-boun...@haskell.org
[mailto:haskell-cafe-boun...@haskell.org] On Behalf Of wren ng thornton
Sent: Thursday, 7 May 2009 2:06 PM
To: haskell-cafe@haskell.org
Subject: Re: [Haskell-cafe] Is Haskell a Good Choice for Web
Applications?(ANN: Vocabulink)

FFT wrote:
> Anton van Straaten wrote:
> 
> > The app is written for a client under NDA, so a blog about it would 
> > have to be annoyingly vague.
> 
> > No doubt the potential for encountering space leaks goes up as one 
> > writes less pure code, persist more things in memory, and depend on
more libraries.
> 
> Exactly. I'm worried about, e.g. needing to use something as simple as

> a stream of prime numbers (see the recent thread about leaks there)

The issues here are going to be the same in Haskell as in every other
language. There's always a tradeoff between the memory of caching old
results vs the time of recalculating them. At present no language's
RTS/GC is smart enough to make that tradeoff for you, and so memoization
must be done manually.

There are some tricks to help make this easier (e.g. weak pointers), but
the problem will always remain. The only thing that makes this perhaps
trickier than in other languages is that, AFAIK/R, the reflection API to
ask the RTS how it's feeling about memory at any given point isn't
terribly portable (between Haskell compilers) or polished/pretty. Then
again, the other GC languages I've dealt with aren't much better and are
often worse.

--
Live well,
~wren
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map: Enumerating ordered subset of keys

2009-02-09 Thread Jared Updike
On 2/8/09, Evan Laforge  wrote:
> I have a little util library with various map functions.  'within' is
>  almost what you want, except it's half-open.  You can make an
>  inclusive one by pulling the lowest element off the above map.
>
>  I'm also curious if there's a better way to do this...
>
>  -- | Like Map.split, except include a matched key in the above map.
>  split_map :: (Ord k) => k -> Map.Map k a -> (Map.Map k a, Map.Map k a)
>  split_map k fm = (pre, post')
> where
> (pre, at, post) = Map.splitLookup k fm
> post' = maybe post (\v -> Map.insert k v post) at
>
>  -- | Split the map into the maps below, within, and above the given range.
>  -- @low@ to @high@ is half-open, as usual.
>  split3_map :: (Ord k) => k -> k -> Map.Map k a
> -> (Map.Map k a, Map.Map k a, Map.Map k a)
>  split3_map low high fm = (below, within, way_above)
> where
> (below, above) = split_map low fm
> (within, way_above) = split_map high above
>
>  within low high fm = let (_, m, _) = split3_map low high fm in m

This looks right to me (correct time complexity). It should do what I
need. I will test it and see what I discover.

Thanks,
  Jared.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map: Enumerating ordered subset of keys

2009-02-08 Thread Evan Laforge
On Mon, Feb 9, 2009 at 2:57 PM, Jared Updike  wrote:
> I would like to enumerate a subset of keys in a Map satisfying \ key
>>= key1 && key <= key2 but in the expected, reasonable amount of time
> (e.g. < O(log(n)) + O(m) for n total keys and m keys in the subset).
> (Or key > key1 and/or key < key2 or some such combination).
>
> Is there an appropriate idiom or combination of library functions to
> accomplish this, short of digging into the code for Data.Map and
> writing such a function for a forked version of Data.Map?

I have a little util library with various map functions.  'within' is
almost what you want, except it's half-open.  You can make an
inclusive one by pulling the lowest element off the above map.

I'm also curious if there's a better way to do this...

-- | Like Map.split, except include a matched key in the above map.
split_map :: (Ord k) => k -> Map.Map k a -> (Map.Map k a, Map.Map k a)
split_map k fm = (pre, post')
where
(pre, at, post) = Map.splitLookup k fm
post' = maybe post (\v -> Map.insert k v post) at

-- | Split the map into the maps below, within, and above the given range.
-- @low@ to @high@ is half-open, as usual.
split3_map :: (Ord k) => k -> k -> Map.Map k a
-> (Map.Map k a, Map.Map k a, Map.Map k a)
split3_map low high fm = (below, within, way_above)
where
(below, above) = split_map low fm
(within, way_above) = split_map high above

within low high fm = let (_, m, _) = split3_map low high fm in m
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Map: Enumerating ordered subset of keys

2009-02-08 Thread Jared Updike
I would like to enumerate a subset of keys in a Map satisfying \ key
>= key1 && key <= key2 but in the expected, reasonable amount of time
(e.g. < O(log(n)) + O(m) for n total keys and m keys in the subset).
(Or key > key1 and/or key < key2 or some such combination).

Is there an appropriate idiom or combination of library functions to
accomplish this, short of digging into the code for Data.Map and
writing such a function for a forked version of Data.Map?

For example I could try something like a Set.split of a Set.split of
Map.keysSet of my original map, but will laziness magically do what I
really want? which is to walk down the tree to key1 (or the nearest
key > key1) and enumerate keys in order until key2 is reached?

Data.Map almost looks like what I need if I can do this.

  Jared.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map add only if not exist and return true if added

2008-12-09 Thread Arthur van Leeuwen


On 9 dec 2008, at 09:11, John Ky wrote:


Hi,

I'm looking for a function for Data.Map that will insert a new key  
and value into the map if the key doesn't already exist in the map.   
When the key already exists, I don't want the value updated in the  
map.  Additionally, I want to know whether the key/value was  
inserted or not so that I can return that fact as a boolean from my  
function.


I can't work out which function in Data.Map is suitable for this  
task and I wanted to avoid two lookups.


Data.Map.insertLookupWithKey (\k n o -> o)
should do the trick. The first element of the tuple returned
is the Maybe a that is Nothing if no value existed at the given key,
the second element is the updated map.

With kind regards, Arthur.

--

  /\/ |   [EMAIL PROTECTED]   | Work like you don't need  
the money
/__\  /  | A friend is someone with whom | Love like you have never  
been hurt
/\/__ | you can dare to be yourself   | Dance like there's nobody  
watching




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Map add only if not exist and return true if added

2008-12-09 Thread John Ky
Hi,

I'm looking for a function for Data.Map that will insert a new key and value
into the map if the key doesn't already exist in the map.  When the key
already exists, I don't want the value updated in the map.  Additionally, I
want to know whether the key/value was inserted or not so that I can return
that fact as a boolean from my function.

I can't work out which function in Data.Map is suitable for this task and I
wanted to avoid two lookups.

Thanks,

-John
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map traversal.

2008-06-08 Thread Ian Lynagh

Hi Serguey,

On Sat, Jun 07, 2008 at 05:32:46PM +0400, Serguey Zefirov wrote:
> 
> So I propose to include those operations into next version of Data.Map.
> 
> If anyone could point me in the right direction I could do any necessary 
> modifications myself (just because I need it).

Please see http://www.haskell.org/haskellwiki/Library_submissions for
how to propose changes to the libraries.


Thanks
Ian

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Map traversal.

2008-06-07 Thread Serguey Zefirov
I found that I often need predecessor and successor of some key in 
Data.Map.Map. Just like that:


predKey, succKey :: Ord k => Data.Map.Map k a -> k -> Maybe k
predKeyElem, succKeyElem :: Ord k => Data.Map.Map k a -> k -> Maybe (k,a)

Data.Map has operations like that on key indexes, but it is slightly 
unnatural to work with three entities (index, key and element) where two 
suffices.


So I propose to include those operations into next version of Data.Map.

If anyone could point me in the right direction I could do any necessary 
modifications myself (just because I need it).


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map

2005-04-03 Thread Gracjan Polak
Sebastian Sylvan wrote:
On Apr 3, 2005 9:38 AM, Gracjan Polak <[EMAIL PROTECTED]> wrote:

insertList asclist map = union map (Data.Map.fromList asclist)

How about:
insertList :: (Ord a) => Map a b -> [(a, b)] -> Map a b
insertList = foldr (uncurry insert) 

Is there any reason why foldr is better than foldl here?
/S
--
Gracjan
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Data.Map

2005-04-03 Thread Sebastian Sylvan
On Apr 3, 2005 9:38 AM, Gracjan Polak <[EMAIL PROTECTED]> wrote:

> insertList asclist map = union map (Data.Map.fromList asclist)

How about:

insertList :: (Ord a) => Map a b -> [(a, b)] -> Map a b
insertList = foldr (uncurry insert) 


/S
-- 
Sebastian Sylvan
+46(0)736-818655
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Map

2005-04-02 Thread Gracjan Polak
Hi all,
As I tried to convert some of my code to newer libraries comming with 
GHC 6.4, I would like to share two things with you:

I noticed that there are two functions missing: deleteList and
insertList. The first one is easy with foldl:
deleteList list map = foldl (flip Data.Map.delete) map list
Second one is even shorter:
insertList asclist map = union map (Data.Map.fromList asclist)
Still I find both of them useful also execution speed seems to be of 
some concern here. Why generate temporary map just to join it with 
second in a moment? Isn't it slower?

Also why Data.Map.lookup and Data.Map.findWithDefault? Why not
lookupWithDefault?
Besides, I really like new library :)
--
Gracjan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe