Functional Jobs | 27 Jan 08:00 2014

New Functional Programming Job Opportunities

Here are some functional programming job opportunities that were posted
recently:

Haskell Developer at Zalora
http://functionaljobs.com/jobs/8678-haskell-developer-at-zalora

Cheers,
Sean Murphy
FunctionalJobs.com
Vlatko Basic | 28 Jan 19:37 2014
Picon

How to pass a polymorphic function in a record?

Hello Cafe,

I'm playing with Persistent and have modules that I'd like to use on several 
backends. This is simplified situation.

In shared module:

   sqliteRun, postgresRun :: Text -> Int -> (ConnectionPool -> IO a) -> IO a
   sqliteRun          = withSqlitePool
   postgresRun conStr = withPostgresqlPool (encodeUtf8 conStr)

   sqlRun :: Text -> Int -> SqlPersistM a -> IO a
   sqlRun conStr poolSize = postgresRun conStr poolSize . runSqlPersistMPool
   --sqlRun conStr poolSize = sqliteRun conStr poolSize . runSqlPersistMPool
	
All works well if either 'sqlRun' above is commented/uncommented:

In one of modules:

   data ThingCfg = ThingCfg { thingDb :: Text }

   listThings :: ThingCfg -> IO [Thing]
   listThings db = sqlRun (thingDb db) $ selectList ...
	
   findThing :: ThingId -> ThingCfg -> IO (Maybe Thing)
   findThing uid db = sqlRun (thingDb db) $ getBy ...

On call site simply:
   let tdb = ThingCfg "test"
   ts <- listThings tdb
(Continue reading)

adam vogt | 28 Jan 21:47 2014
Picon

Re: How to pass a polymorphic function in a record?

Hi Vlatko,

Did you consider:

{-# LANGUAGE RankNTypes #-}
data ThingCfg m = ThingCfg {
    thingDb  :: Text,
    thingRun_ :: forall a. Text -> m a -> IO a }

thingRun (ThingCfg db f) = f db

Maybe the `m' above should be SqlPersistM, if all your other backends use that type.

--
Adam


On Tue, Jan 28, 2014 at 1:37 PM, Vlatko Basic <vlatko.basic <at> gmail.com> wrote:
Hello Cafe,

I'm playing with Persistent and have modules that I'd like to use on several backends. This is simplified situation.

In shared module:

  sqliteRun, postgresRun :: Text -> Int -> (ConnectionPool -> IO a) -> IO a
  sqliteRun          = withSqlitePool
  postgresRun conStr = withPostgresqlPool (encodeUtf8 conStr)

  sqlRun :: Text -> Int -> SqlPersistM a -> IO a
  sqlRun conStr poolSize = postgresRun conStr poolSize . runSqlPersistMPool
  --sqlRun conStr poolSize = sqliteRun conStr poolSize . runSqlPersistMPool
       
All works well if either 'sqlRun' above is commented/uncommented:


In one of modules:

  data ThingCfg = ThingCfg { thingDb :: Text }

  listThings :: ThingCfg -> IO [Thing]
  listThings db = sqlRun (thingDb db) $ selectList ...
       
  findThing :: ThingId -> ThingCfg -> IO (Maybe Thing)
  findThing uid db = sqlRun (thingDb db) $ getBy ...



On call site simply:
  let tdb = ThingCfg "test"
  ts <- listThings tdb


I would like to specify 'sqliteRun' or 'postgresRun' function as (some) parameter on the call site, but do not know how.
Something of imaginary solution:

  data ThingCfg = ThingCfg {
      thingDb  :: Text,
      thingRun :: SqlPersistM a -> IO a
    }

On call site:
  let tdb = ThingCfg "test" sqliteRun
  ts <- listThings tdb

I want to keep it as an init param because there are other backends (class instances) that are not Persistent, so the use of 'sqlRun' on call site is not an option.


What would be the best/correct way(s) to achieve that?


Best regards,
  Vlatko
_______________________________________________
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
Vlatko Basic | 29 Jan 16:26 2014
Picon

Re: How to pass a polymorphic function in a record?

Hi Adam,

Thanks a lot. Spent almost a day on the problem. Tried every possible combination, except forall.

I read again about forall and now it is much more clear when I see it as a solution to my own problem.

vlatko


-------- Original Message --------
Subject: Re: [Haskell-cafe] How to pass a polymorphic function in a record?
From: adam vogt <vogt.adam <at> gmail.com>
To: vlatko.basic <at> gmail.com
Cc: haskell-cafe <haskell-cafe <at> haskell.org>
Date: 28.01.2014 21:47


Hi Vlatko,

Did you consider:

{-# LANGUAGE RankNTypes #-}
data ThingCfg m = ThingCfg {
    thingDb  :: Text,
    thingRun_ :: forall a. Text -> m a -> IO a }

thingRun (ThingCfg db f) = f db

Maybe the `m' above should be SqlPersistM, if all your other backends use that type.

--
Adam


On Tue, Jan 28, 2014 at 1:37 PM, Vlatko Basic <vlatko.basic <at> gmail.com> wrote:
Hello Cafe,

I'm playing with Persistent and have modules that I'd like to use on several backends. This is simplified situation.

In shared module:

  sqliteRun, postgresRun :: Text -> Int -> (ConnectionPool -> IO a) -> IO a
  sqliteRun          = withSqlitePool
  postgresRun conStr = withPostgresqlPool (encodeUtf8 conStr)

  sqlRun :: Text -> Int -> SqlPersistM a -> IO a
  sqlRun conStr poolSize = postgresRun conStr poolSize . runSqlPersistMPool
  --sqlRun conStr poolSize = sqliteRun conStr poolSize . runSqlPersistMPool
       
All works well if either 'sqlRun' above is commented/uncommented:


In one of modules:

  data ThingCfg = ThingCfg { thingDb :: Text }

  listThings :: ThingCfg -> IO [Thing]
  listThings db = sqlRun (thingDb db) $ selectList ...
       
  findThing :: ThingId -> ThingCfg -> IO (Maybe Thing)
  findThing uid db = sqlRun (thingDb db) $ getBy ...



On call site simply:
  let tdb = ThingCfg "test"
  ts <- listThings tdb


I would like to specify 'sqliteRun' or 'postgresRun' function as (some) parameter on the call site, but do not know how.
Something of imaginary solution:

  data ThingCfg = ThingCfg {
      thingDb  :: Text,
      thingRun :: SqlPersistM a -> IO a
    }

On call site:
  let tdb = ThingCfg "test" sqliteRun
  ts <- listThings tdb

I want to keep it as an init param because there are other backends (class instances) that are not Persistent, so the use of 'sqlRun' on call site is not an option.


What would be the best/correct way(s) to achieve that?


Best regards,
  Vlatko
_______________________________________________
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