| Author |
Message |
< Erlang ~ Is this to much fun? |
| tetrachromat |
Posted: Wed Feb 16, 2011 10:16 pm |
|
|
|
Joined: 15 Jul 2008
Posts: 3
|
We do have the following code, working as expected:
---------------------------------------------------
Code: accessibleSystem( AvailableSystems ) ->
% takes a list of available systems (e.g [1,2,3,4] and returns the currently
% accessible systems (e.g. [4,3,2,1] if all requirements are ok.
%
% Accessibility is defined by some classes of requirements (e.g. A,B,C),
% the requirements are implemented as clauses e.g. a1, a2, b1, b2, b3, c1
% We place all requirements in a tuple containing the three requirement classes
% where each class is a list of funs
A = [ fun(S) -> a1(S) end, fun(S) -> a2(S) end ],
B = [ fun(S) -> b1(S) end, fun(S) -> b2(S) end, fun(S) -> b3(S) end ],
C = [ fun(S) -> c1(S) end ],
Requirements = { A, B, C },
% There is a function (name it 'require') that verifies the requirements
% on each of the available systems
require( Requirements, AvailableSystems ).
% Below is a sample of the require function (simplified for this post)
require( { A, B, C }, Systems ) ->
Rule = fun( System , Accessible ) ->
case available( A, System ) andalso
available( B, System ) andalso
available( C, System ) of
true -> [ System | Accessible ];
false -> Accessible
end
end,
lists:foldl( Rule, [], Systems ).
% simplified recursive requirements testing
available( [ ], _System ) -> true;
available( [ R | T ], System ) ->
case R (System) of
true -> available( T, System );
false -> false
end.
% requirements clauses
a1(_S) -> true.
a2(_S) -> true.
b1(_S) -> true.
b2(_S) -> true.
b3(_S) -> true.
c1(_S) -> true.
----------------------------------------------------------------
% To ease the definition of requirements we used the following macro
-define( Req( N ), fun(S) -> N (S) end ).
% So the requirements lists are written as follows
A = [ ?Req( a1 ), ?Req( a1 ) ],
B = [ ?Req( b1 ), ?Req( b2 ), ?Req( b3 ) ],
C = [ ?Req( c1 ) ],
This also wroks charmingly.
----------------------------------------------------------------
% Our next intention was to write something like a requirements factory,
% which produces (returns) the fun's to place into the requirements list.
% Somethig like:
requirement(N) ->
fun(S) -> N (S) end.
% and using it as follows
A = [ requirement( a1 ), requirement( a2 ) ],
B = [ requirement( b1 ), requirement( b2 ), requirement( b3 ) ],
C = [ requirement( c1 ) ],
% But this did not work out well. We received the following exception
** exception error: bad function a1
in function syst:'-requirement/1-fun-0-'/2
in call from syst:available/2
in call from syst:'-require/2-fun-0-'/5
in call from lists:foldl/3
% A similar exception occured after changing the factory to
Requirement = fun(N) -> (fun(S) -> N (S) end) end,
% and using it as follows:
A = [ Requirement( a1 ), Requirement( a2 ) ],
B = [ Requirement( b1 ), Requirement( b2 ), Requirement( b3 ) ],
C = [ Requirement( c1 ) ],
?** exception error: bad function a1
in function syst:'-accessibleSystem/1-fun-0-'/2
in call from syst:available/2
in call from syst:'-require/2-fun-0-'/5
in call from lists:foldl/3
----------------------------------------------------------------
Somehow we got lost with fun's.
Is this just to much fun for Erlang?
Anybody here able to show us the direction we should go?
Thanks in advance.
Paul |
|
|
| Back to top |
|
| rvirding |
Posted: Fri Feb 18, 2011 4:08 pm |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
No, it is not too much fun for Erlang, you can have a lot of fun with Erlang. The problem is syntactic.
Macros in Erlang are handled VERY early during compilation so in your macro example the compiler sees:
Code: %% So the requirements lists are written as follows
A = [ fun (S) -> a1( S ) end, fun (S) -> a1( S ) end],
B = [ fun (S) -> b1( S ) end, fun (S) -> b2( S ) end, fun (S) -> b3( S ) end],
C = [ fun (S) -> c1( S ) end],
while in your second case the compiler actually sees exactly what you wrote:
Code: requirement(N) -> fun(S) -> N (S) end,
% and using it as follows:
A = [ requirement( a1 ), requirement( a2 ) ],
B = [ requirement( b1 ), requirement( b2 ), requirement( b3 ) ],
C = [ requirement( c1 ) ],
The problem is that in Erlang the syntax for a function call without an explicit module, <func>( ... ), the function can only be either an atom which is the name of a function in the same module, or a variable which evaluates to a fun, see Function Calls. In your funs above when they are called then N will be an atom which is the name of the function, and this is illegal. Hence the error.
The same thing happens in your last case but there it is just one level deeper inside an extra fun. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| tetrachromat |
Posted: Fri Feb 18, 2011 8:59 pm |
|
|
|
Joined: 15 Jul 2008
Posts: 3
|
Woooow, precise and concise. Althoug we had to read it more than once, that is it.
We already identified the problem as a compile time - run time thing. Also thought of using apply/3. But the Module:Function notation is more appropriate.
We define the requirements lists now as:
Code:
A = { req, [ a1, a2 ] },
B = { req, [ b1, b2, b3 ] },
C = { req, [ c1 ] },
and the requirements testing as:
Code:
available( { Module, Requirements }, System ) ->
avail( Module, Requirements, System ).
avail( _, [ ], _ ) -> true;
avail( M, [ R | T ], System ) ->
case M:R (System) of
true -> avail( M, T, System );
false -> false
end.
This definitely opens up a new road, also leading to the spec's configuration feature.
Thank you.
Paul |
|
|
| Back to top |
|
| rvirding |
Posted: Mon Feb 21, 2011 5:09 pm |
|
|
|
User
Joined: 30 Aug 2006
Posts: 452
Location: Stockholm, Sweden
|
| Doing it that way also makes it easy write requirement lists as simple files containing erlang terms. |
_________________ Robert Virding, Erlang Solutions Ltd. |
|
| Back to top |
|
| dongdongwu |
Posted: Thu Sep 20, 2012 6:44 am |
|
|
|
User
Joined: 19 Sep 2012
Posts: 236
|
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word. |
|
|
| 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
|
|
|