An erlang exercise which confused me

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

An erlang exercise which confused me

カカキ
I am reading programming erlang (2nd Ed.) and get a little confused by the exercise 16-6.

Here is the question

Twits are exactly 140 bytes long. Write a random-access twit storage module called twit_store.erl that exports the following: init(K) allocates space for K twits. store(N, Buf) stores twit number N(1,...K) with data Buf (a 140-byte binary) in the store. fetch(N) fetches the data for twit number N.

I am confused by the meaning of init(K). I try to use function file:allocate but always get a result of {error, einval}. Could anyone help me?

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: An erlang exercise which confused me

Joe Armstrong-2
twit_store:init(N) just allocates space for K twits. Note that I said
*nothing* about how or where the
twits get stored - it might be in the file system, or in a database,
or in a process - it's up to you to choose.

Seems like you've chose to store the twits in a file. {error,einval}
means that some argument to
file:allocate is incorrect, you will have to investigate why this is the case.

Cheers

/Joe

On Mon, Sep 24, 2018 at 9:26 AM, カカキ <[hidden email]> wrote:

> I am reading programming erlang (2nd Ed.) and get a little confused by the
> exercise 16-6.
>
> Here is the question
>
> Twits are exactly 140 bytes long. Write a random-access twit storage module
> called twit_store.erl that exports the following: init(K) allocates space
> for K twits. store(N, Buf) stores twit number N(1,...K) with data Buf (a
> 140-byte binary) in the store. fetch(N) fetches the data for twit number N.
>
> I am confused by the meaning of init(K). I try to use function file:allocate
> but always get a result of {error, einval}. Could anyone help me?
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions