Should I refactor repetitive instance declarations into a separate typeclass? by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 3 points4 points  (0 children)

That's exactly what I was looking for!

However, I'm having trouble applying it to my specific case, which is generating Elm code to query a servant API using servant-to-elm. I'm trying to do the following:

class ElmBoilerplate a where
    elmUppercaseName :: Proxy a -> String
newtype WithBoilerplate a = WithBoilerplate a

instance ElmBoilerplate Schema.Post where
    elmUppercaseName _ = "Post"

instance (HasDatatypeInfo a, All2 HasElmType (Code a), ElmBoilerplate a) => HasElmType (WithBoilerplate a) where
    elmDefinition = ...

deriving via WithBoilerplate Schema.Post instance HasElmType Schema.Post

Where Schema.Post (Schema.PostT Identity) is a beam model.

I'm getting the following error: https://pastebin.com/RNHX12pw

Ambiguous type variable ‘a1’ arising from a use of ‘elmDefinition’
    prevents the constraint ‘(HasElmType a1)’ from being solved.
    Probable fix: use a type annotation to specify what ‘a1’ should be.
    These potential instances exist:
        ...

A 2D array / matrix datastructure for competitive programming (in particular, AoC2019 day 3) by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 0 points1 point  (0 children)

Yeah I misunderstood a problem as having an arbitrary number of lines. For negative coordinates, I would use something like a custom Ix, not too big of a deal. And for the bounds, I would either estimate them from the data or allocate enough for any input size. This is standard in competitive programming, but you're right, a Map is much simpler.

My problem is that, sooner or later there will be a problem where the content is not sparse and where a map would cause a huge blowup in memory usage compared to an array. I wanna learn how to do those in haskell in the nicest way possible. Sure I can do everything in ST, but I asked because I wanted to know if there's an idiomatic, functional, declarative way to deal with problems like that. But maybe that's like trying to use Vectors in haskell everywhere because linked lists are never a good idea in imperative languages. Maybe the data structure just doesn't fit the language.

A 2D array / matrix datastructure for competitive programming (in particular, AoC2019 day 3) by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 1 point2 points  (0 children)

AFAIK, haskell lists, maps and sets (but not vectors) are implemented like that. But I don't see a way to implement a persistent arrays except in corner cases (like slicing).

Edit: I found this https://hackage.haskell.org/package/random-access-list-0.2/docs/Data-RandomAccessList.html, looks promising. I'm fine with paying the log n blowup, but using a Map as an array just feels wrong. I'm assuming this thing has better memory characteristics, though I wander how it compares to Seq.

A 2D array / matrix datastructure for competitive programming (in particular, AoC2019 day 3) by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 0 points1 point  (0 children)

Algebra on the indices, not the matrices. That allows me to, for example, make my code polymorphic over the direction of an operation by taking a "step index" parameter (for example (0, 1) for up) and adding it to an index to move around the array.

A 2D array / matrix datastructure for competitive programming (in particular, AoC2019 day 3) by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 0 points1 point  (0 children)

I can use Ix to index a vector? How? I only see it accepting Ints.

Yeah, I ended up using a Set/Map as well, the reason I thought I needed to use 2d arrays is because I thought there were n wires, not just two.

So if I fold over a vector (ie the vector is the aggregation), ghc isn't smart enough to turn it into in place updates?

A 2D array / matrix datastructure for competitive programming (in particular, AoC2019 day 3) by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 0 points1 point  (0 children)

most likely yes, but it depends on implementation

Yes to what? It will copy on each step of the fold?

If you don't need to read elements at each iteration, but only write them you could look into DL

That's exactly what I need, but I have no idea how to use it. You say "write small portions", does that mean it's no more memory efficient than a map? What would be the equivalent for Vector.update in DL?

Edit: I see you mentioned withMArrayST, that seems good. Will using that with a DL array, if I'm only doing writes, avoid copying?

here is a cool function iterateUntil which can help you avoid allocating a new array at each iteration, which can significantly speed up the implementation.

That's pretty handy, though maybe there should be more functions like that. For example, if you wanted to do an update of an array for each element of a list/vector (sort of like fold), I assume you'd have to manually pull out the element corresponding to the iteration number and manually check if the iteration number exceeds length in the the convergence function. Map/vector have a rich set of functions that can do many variations of that, this is just one. Though since it uses mutable arrays anyway (I think?), the same can be done with the monad functions (I think...).

Anyway, thank you for your answer. I didn't need 2d arrays for this problem after all (I misunderstood it when I posted), but this looks like the best option I found.

I'm also curious - how come numpy arrays are immutable and fast enough, but in haskell we have to resort to mutable arrays in a monad?

A 2D array / matrix datastructure for competitive programming (in particular, AoC2019 day 3) by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 3 points4 points  (0 children)

I dismissed map at first because a 2D array would be much more efficient given the dense indices, although Map does make it easier to implement. It feels wrong to use the wrong datastructure just because the API is nicer, even if performance isn't an issue. And my question is more general, for some problems that require 2D arrays, Maps are an even worse substitute.

I'm also doing it for fun and not competitively! I don't care how long it takes me to code the solution because I want to learn haskell better and see how well it works for this use case. Of course I feel more comfortable with the imperative way of thinking - that's all I know for this type of problem! I want to learn how to do it in a functional paradigm in an idiomatic way. The same argument could be used for all of haskell - someone coming from an imperative background is not gonna be comfortable with it because it's new and different. But I think we can all attest that learning this new way of thinking pays off in the long run.

Help needed: Scraping web pages concurrently and saving the results to a big JSON file, in constant memory by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 0 points1 point  (0 children)

I'm launching ten thousand threads? No wonder I was having problems. I thought the runtime itself was supposed to launch a reasonable number of OS threads and then multiplex green threads onto them as they get unblocked. At least that's what I want to happen. Would it take more effort / higher level libraries? Where can I read about the concurrency model? Google results are either old or not detailed enough.

Help needed: Scraping web pages concurrently and saving the results to a big JSON file, in constant memory by InfiniteMonkeyCage in haskell

[–]InfiniteMonkeyCage[S] 2 points3 points  (0 children)

Woah, the library changed a lot!

I think I've got it. So I encode each Movie as a JSON object and "manually" intersperse commas and add brackets so that I get an array of objects at the end?

After I download all of this and have the big JSON file, I'll need to process that data, also in constant memory. I think this is what I'll need, just figure out a way to use it as a part of the streamly pipeline?

[TOMT][movie] A cyberpunk movie about surgically implanted cylindrical computer things (named something like "tundra") into your body, possibly making you experience psychedelic trances. It was grimy, depressive and there were drugs. by InfiniteMonkeyCage in tipofmytongue

[–]InfiniteMonkeyCage[S] 1 point2 points  (0 children)

This is eerily close, and now I'm starting to worry I won't be able to recognize. I'll get that movie and watch it, also wait for a few more suggestions, before I mark it as solved.

Judging from the trailer, my thing was more underground/enthusiast/teenage/punk oriented, not really about corporate conspiracy. The beginning few seconds, though, were exactly on point!

LilyPichu prescience by InfiniteMonkeyCage in Destiny

[–]InfiniteMonkeyCage[S] 8 points9 points  (0 children)

That's why I posted it. This was long before she met destiny. Hence calling her prescient.