Coder Social home page Coder Social logo

Comments (6)

recursion-ninja avatar recursion-ninja commented on September 27, 2024

I really hope that this is possible, because it is how I would like to use most O(1) lookup functions.

from hashtables.

0xd34df00d avatar 0xd34df00d commented on September 27, 2024

@gregorycollins what are your thoughts on this? If you think this is OK, I might take a look at actually implementing this.

from hashtables.

gregorycollins avatar gregorycollins commented on September 27, 2024

My thought is, I don't really know how to do it cleanly so I haven't implemented it yet, and the use case hasn't seemed compelling enough to make the work feel worth it. All of the data types need to be reworked for pure lookup, but you want to use the same lookup algorithms everywhere, so you either have to duplicate everything or use templating/code generation.

One possibility you could consider is just to newtype around hashtables in IO, and use unsafePerformIO to expose only the non-mutating functions. I'm sure the purists reading this thread are choking on their Yerba Matés by now.

from hashtables.

0xd34df00d avatar 0xd34df00d commented on September 27, 2024

The use case is to build the hash table kust once in ST and then use it in pure code just for lookups without having to wrap the pure code in a monad.

I actually think of something similar - a newtype wrapper and runST'ing the existing lookup function.

from hashtables.

gregorycollins avatar gregorycollins commented on September 27, 2024

The use case is to build the hash table kust once in ST and then use it in pure code just for lookups without having to wrap the pure code in a monad.

My point is that you can already do this, with unsafePerformIO. A newtype wrapper and maybe a build function would help make it safer though:

build
  :: HashTable h
  => Maybe Int   -- ^ size hint
  -> (h s k v -> ST s ())
  -> ST s (Pure.HashTable k v)

Pure.HashTable can just be a set of lambdas doing either runST or unsafePerformIO:

data HashTable k v = HashTable { _lookup :: k -> Maybe v, ... }

The extra safety comes from swapping out the STRef inside the HashTable objects so that the mutable table can no longer be used once the user function is finished. It probably requires a new typeclass method.

from hashtables.

gregorycollins avatar gregorycollins commented on September 27, 2024

P.S. runST wants a forall s . ST s a, which you won't be able to give it -- your hashtable will have a concrete state type s. So here the only thing that will make it work is s=RealWorld and using unsafePerformIO anyways, the build function has to be:

build
  :: HashTable h
  => Maybe Int
  -> (IOHashTable h k v -> IO ())
  -> IO (Pure.HashTable k v)

from hashtables.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.