user31708
user31708

Reputation: 730

How can I handle binary data in Frege?

I'm new to Frege, although I know both Java and Haskell.

I'm porting some Haskell code that uses ByteString, and I'm trying to figure out what to use in Frege. I assume I would want to use something whose underlying Java representation is byte[], but I'm not sure how Frege wraps that.

In particular, I looked through PreludeArrays.fr, and I noticed that there's an instance of PrimitiveArrayElement for every primitive Java type except byte.

I feel like there's something obvious I'm missing. How do I go about dealing with binary data in Frege? Are there any examples of how to do so?

Upvotes: 2

Views: 75

Answers (1)

Ingo
Ingo

Reputation: 36349

There actually is such an instance. It just can't be in PreludeArrays for technical reasons. Rather it lives in frege.java.Lang, where Byte and Short are introduced.

Even if there were none, you simply could say

instance PrimitiveArrayElement Byte

and it should work.

Regarding your question: I think it is safe to say that JArray Byte should be ok for any problem with any data. Another question is if it is the best representation. For example, if those data actually are UTF8 strings, I would think that conversion into String would be the way to go.

Things to consider

  1. mapArray, foldArray and friends are space efficient, but strict and a bit slow, because they use the ST monad.
  2. Conversely, map and fold are reasonably fast, but of course squander lots of memory.

An approach I have used in frege.data.Hashmap was to identify very basic array operations and implement them in Java (one can do this in-line, even), and write the rest of the program in terms of those.

You may want to look at the source code to get an idea of how to do this.

Upvotes: 1

Related Questions