|
|
| Author |
Message |
< Erlang ~ Need some help on first Erlang appliation. |
| JarrodRoberson |
Posted: Mon Sep 17, 2007 9:41 pm |
|
|
|
User
Joined: 17 Sep 2007
Posts: 42
|
I am trying to grok how to write a simple 'du' like program that walks a directory structure ( not just one but all nested directories ) and calculates a sum of all the file sizes.
I have stared at my Programming Erlang book I got a few days ago, and stared at the Erlang Cookbook as well as staring and hacking at the erlang shell and it just isn't clicking.
From what I see, this should be like a 5 line program in Erlang.
Can anyone provide a working example or point me in the correct direction? |
|
|
| Back to top |
|
| Mazen |
Posted: Tue Sep 18, 2007 9:17 am |
|
|
|
User
Joined: 20 Jul 2006
Posts: 164
Location: London
|
|
| Back to top |
|
| JarrodRoberson |
Posted: Wed Sep 19, 2007 3:34 pm |
|
|
|
User
Joined: 17 Sep 2007
Posts: 42
|
I am struggling with the "as a functional program" part. :-)
I see that there is filelib:fold_files() and filelib:file_size() but I can't figure out what to feed fold_files() the documentation is pretty sparse on what the parameters are. I have searched and searched and don't understand what is expected for "AccIn" the last parameter to fold_files().
I have a sum() function working from an example I found online that works on a list. I just need to understand how to get a list of file sizes from filelib:foldfiles()
Can anyone explain? |
|
|
| Back to top |
|
| michal |
Posted: Wed Sep 19, 2007 4:47 pm |
|
|
|
User
Joined: 20 Jul 2006
Posts: 44
Location: London
|
AccIn stands for accumulator. You want to calculate the sum of all file sizes so initiate it with 0. The fun you are passing as the fourth parameter should take two parameters - one is the file name, the other is the current value of the accumulator. The fun should return the new value of the accumulator.
Now when all is clear try this code:
filelib:fold_files(".", ".+", true, fun(F, A) -> A+filelib:file_size(F) end, 0).
Michal |
_________________ http://www.erlang-consulting.com |
|
| Back to top |
|
|
|
All times are GMT
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|
|