Dan Burton | 12 Jun 11:47 2014
Picon

ANN: io-memoize 1.1

Have you ever wanted to restrict an IO action such that it could only be invoked once? Now you can with System.IO.Memoize.once (formerly known as ioMemo).

Need slightly more complicated logic? (e.g. An IO callback that takes an argument but then only executes the first time around, essentially ignoring the argument the other times it is called.) Leverage the dead simple "write once"  Control.Concurrent.Cache api for fun and profit.

1. Create an empty cache with newCache
2. fetch repeatedly from the cache, each time providing a fallback in case it is empty.
3. There is no step 3.

Fetching from a Cache is thread safe: only one "fallback" will execute at a time, and after the first successful fallback, the cached value will be set and no other fallbacks will be called.

-- Dan Burton
_______________________________________________
Haskell mailing list
Haskell <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell
Kim-Ee Yeoh | 12 Jun 12:17 2014

Re: [Haskell] ANN: io-memoize 1.1


On Thu, Jun 12, 2014 at 4:47 PM, Dan Burton <danburton.email <at> gmail.com> wrote:
Have you ever wanted to restrict an IO action such that it could only be invoked once? Now you can with System.IO.Memoize.once (formerly known as ioMemo).

(Moving to cafe for dicussion ...)

I may want a one-time only IO action, although so far the need hasn't arisen yet. ;)

In the spirit of pilfering while the buns are still hot out of the oven, is there a backstory for this package you could possibly sketch out? Some canonical use case maybe?

-- Kim-Ee
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Simon Hengel | 12 Jun 12:28 2014
Picon

Re: [Haskell] ANN: io-memoize 1.1

Hi Kim-Ee,

> I may want a one-time only IO action, although so far the need hasn't
> arisen yet. ;)
> 
> In the spirit of pilfering while the buns are still hot out of the oven, is
> there a backstory for this package you could possibly sketch out? Some
> canonical use case maybe?

I use it to implement beforeAll/beforeAllWith[1] in Hspec[2].  I have
been reluctant to add them because used in the wrong way they lead to
bad/order dependent test cases.  But there are some valid use cases.  So
I decided to finally add them.

Cheers,
Simon

[1] https://github.com/hspec/hspec/pull/173
[2] http://hspec.github.io/
Dan Burton | 12 Jun 21:16 2014
Picon

Re: [Haskell] ANN: io-memoize 1.1

I created io-memoize a few years ago, around the same time that I also created runmemo. I was interested in "memoization" at the time, and was exploring various memoization techniques. It was just for theory/fun and had no motivating use case at the time.


I've been following various pipes/conduit discussions, and one particular discussion about WAI involved an interest in a "call once" callback. It's hard to enforce that sort of thing at the compile time without using hardcore solutions such as linear types or indexed monads, so the obvious solution is to use something like io-memoize to enforce it at runtime instead.


Something else I've been working on lately is "the observer pattern" in Haskell. When you subscribe to an observer, it will give you a "subscription", which is just an action you can run to "unsubscribe" yourself. Again, there's potential for "once" to be useful here for implementing the unsubscribe action. If I manage to create something solid and novel out of this observer pattern stuff, then I'll release it to hackage as the "observable" package, or something like that.

Coincidentally, Simon pinged me about io-memoize with his use case in HSpec. So it's something that's been on my mind, but Simon came to me with a fresh Real Code use case that motivated the recent tweaks and the 1.1 release.

On Thu Jun 12 2014 at 3:28:47 AM, Simon Hengel <sol <at> typeful.net> wrote:
Hi Kim-Ee,

> I may want a one-time only IO action, although so far the need hasn't
> arisen yet. ;)
>
> In the spirit of pilfering while the buns are still hot out of the oven, is
> there a backstory for this package you could possibly sketch out? Some
> canonical use case maybe?

I use it to implement beforeAll/beforeAllWith[1] in Hspec[2].  I have
been reluctant to add them because used in the wrong way they lead to
bad/order dependent test cases.  But there are some valid use cases.  So
I decided to finally add them.

Cheers,
Simon

[1] https://github.com/hspec/hspec/pull/173
[2] http://hspec.github.io/
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Kim-Ee Yeoh | 13 Jun 05:31 2014

Re: [Haskell] ANN: io-memoize 1.1

SimonM, SimonH, Dan: thank you all for the responses! Really helpful when deciding to use this package.

-- Kim-Ee


On Fri, Jun 13, 2014 at 2:16 AM, Dan Burton <danburton.email <at> gmail.com> wrote:
I created io-memoize a few years ago, around the same time that I also created runmemo. I was interested in "memoization" at the time, and was exploring various memoization techniques. It was just for theory/fun and had no motivating use case at the time.

I've been following various pipes/conduit discussions, and one particular discussion about WAI involved an interest in a "call once" callback. It's hard to enforce that sort of thing at the compile time without using hardcore solutions such as linear types or indexed monads, so the obvious solution is to use something like io-memoize to enforce it at runtime instead.


Something else I've been working on lately is "the observer pattern" in Haskell. When you subscribe to an observer, it will give you a "subscription", which is just an action you can run to "unsubscribe" yourself. Again, there's potential for "once" to be useful here for implementing the unsubscribe action. If I manage to create something solid and novel out of this observer pattern stuff, then I'll release it to hackage as the "observable" package, or something like that.

Coincidentally, Simon pinged me about io-memoize with his use case in HSpec. So it's something that's been on my mind, but Simon came to me with a fresh Real Code use case that motivated the recent tweaks and the 1.1 release.


On Thu Jun 12 2014 at 3:28:47 AM, Simon Hengel <sol <at> typeful.net> wrote:
Hi Kim-Ee,

> I may want a one-time only IO action, although so far the need hasn't
> arisen yet. ;)
>
> In the spirit of pilfering while the buns are still hot out of the oven, is
> there a backstory for this package you could possibly sketch out? Some
> canonical use case maybe?

I use it to implement beforeAll/beforeAllWith[1] in Hspec[2].  I have
been reluctant to add them because used in the wrong way they lead to
bad/order dependent test cases.  But there are some valid use cases.  So
I decided to finally add them.

Cheers,
Simon

[1] https://github.com/hspec/hspec/pull/173
[2] http://hspec.github.io/

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Krzysztof Skrzętnicki | 13 Jun 09:33 2014
Picon

Re: [Haskell] ANN: io-memoize 1.1

I have also used this package as a quick-and-dirty-but-working solution to loading external resources at runtime and performing caching if needed. This was something like this:

loadConfigFile :: IO SomeDataType
loadConfigFile = once (read <$> readFile "foo.config.txt")

Except that config file was actually ~50 MB long and parsed slowly.

There are obviously cases where caching such file is wrong or should be done in some structured fashion (like in Haxl). Yet still, in other cases this is perfect tool to cut down development time.

--
Krzysztof

On Fri, Jun 13, 2014 at 5:31 AM, Kim-Ee Yeoh <ky3 <at> atamo.com> wrote:
SimonM, SimonH, Dan: thank you all for the responses! Really helpful when deciding to use this package.

-- Kim-Ee


On Fri, Jun 13, 2014 at 2:16 AM, Dan Burton <danburton.email <at> gmail.com> wrote:
I created io-memoize a few years ago, around the same time that I also created runmemo. I was interested in "memoization" at the time, and was exploring various memoization techniques. It was just for theory/fun and had no motivating use case at the time.

I've been following various pipes/conduit discussions, and one particular discussion about WAI involved an interest in a "call once" callback. It's hard to enforce that sort of thing at the compile time without using hardcore solutions such as linear types or indexed monads, so the obvious solution is to use something like io-memoize to enforce it at runtime instead.


Something else I've been working on lately is "the observer pattern" in Haskell. When you subscribe to an observer, it will give you a "subscription", which is just an action you can run to "unsubscribe" yourself. Again, there's potential for "once" to be useful here for implementing the unsubscribe action. If I manage to create something solid and novel out of this observer pattern stuff, then I'll release it to hackage as the "observable" package, or something like that.

Coincidentally, Simon pinged me about io-memoize with his use case in HSpec. So it's something that's been on my mind, but Simon came to me with a fresh Real Code use case that motivated the recent tweaks and the 1.1 release.


On Thu Jun 12 2014 at 3:28:47 AM, Simon Hengel <sol <at> typeful.net> wrote:
Hi Kim-Ee,

> I may want a one-time only IO action, although so far the need hasn't
> arisen yet. ;)
>
> In the spirit of pilfering while the buns are still hot out of the oven, is
> there a backstory for this package you could possibly sketch out? Some
> canonical use case maybe?

I use it to implement beforeAll/beforeAllWith[1] in Hspec[2].  I have
been reluctant to add them because used in the wrong way they lead to
bad/order dependent test cases.  But there are some valid use cases.  So
I decided to finally add them.

Cheers,
Simon

[1] https://github.com/hspec/hspec/pull/173
[2] http://hspec.github.io/


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
Peter Simons | 13 Jun 19:16 2014
Picon

Re: ANN: io-memoize 1.1

Hi Dan,

 > 1. Create an empty cache with newCache
 > 2. fetch repeatedly from the cache, each time providing a fallback in
 >    case it is empty.
 >
 > Fetching from a Cache is thread safe: only one "fallback" will execute
 > at a time, and after the first successful fallback, the cached value
 > will be set and no other fallbacks will be called.

I'm not sure whether I get this correctly. If multiple threads fetch
values from one cache, will only one of those threads run at a time? Or
will those that fetch pairwise different values still run concurrently?

In other words, does the cache have a global lock or is there a lock per
value?

Best regards,
Peter
Dan Burton | 13 Jun 19:43 2014
Picon

Re: ANN: io-memoize 1.1

Sorry I wasn't clear about that. The lock is not global, it is per cache. newCache creates a newMVar which serves as the thread safe lock for that cache.

Concurrently fetching from different caches can indeed run their separate fallbacks concurrently.

On Fri, Jun 13, 2014, 10:16 AM, Peter Simons <simons <at> cryp.to> wrote:
Hi Dan,

 > 1. Create an empty cache with newCache
 > 2. fetch repeatedly from the cache, each time providing a fallback in
 >    case it is empty.
 >
 > Fetching from a Cache is thread safe: only one "fallback" will execute
 > at a time, and after the first successful fallback, the cached value
 > will be set and no other fallbacks will be called.

I'm not sure whether I get this correctly. If multiple threads fetch
values from one cache, will only one of those threads run at a time? Or
will those that fetch pairwise different values still run concurrently?

In other words, does the cache have a global lock or is there a lock per
value?

Best regards,
Peter

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe <at> haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Gmane