tags:

views:

132

answers:

2

I have this Erlang code:

not lists:any(fun(Condition) ->Condition(Message) end, Conditions).

Can anyone please explain the entire statement in layman's terms? For your information Condition is a function, Conditions is an array. What does fun(Condition) ->Condition(Message) end mean? As well as meaning of not lists:any.

+6  A: 
fun(Condition) ->Condition(Message) end

is a lambda function that applies the function Condition to the value of Message (taken as a closure on the surrounding code).

lists:any

is a function that takes a predicate and a list of values, and calls the predicate on each value in turn, and returns the atom true if any of the predicate calls do.

Overall, the result is the atom true if none of the Condition functions in the list Conditions return true for the Message value.

EDIT -- add documentation for lists:any

any(Pred, List) -> bool()

Types:

Pred = fun(Elem) -> bool()
 Elem = term()
List = [term()]

Returns true if Pred(Elem) returns true for at least one element Elem in List.

Steve Gilham
eh the result is inverted ("not lists::any(.."
Will
For the hard of reading : "Overall, the result is true if _none_ of the Condition functions return true"
Steve Gilham
you edited your post Steve? I don't see the 'edit' diff thing.
Will
Yeah -- I added the section starting with "EDIT -- "
Steve Gilham
yet the wiki diff thing doesn't show (for me). Anyone can see it?Only I'd swear that I saw you had it inverted before the edit, but I'll believe you if you now tell me that I'm hard of reading ;)
Will
+1  A: 

Condition is something that takes a message and returns a boolean if it meets some criteria.

The code goes through the list of conditions and if any of them say true then it returns false, and if all of them say false it says true.

Roughly translated to verbose pseudo-Python:

def not_lists_any(Message,Conditions):
  for Condition in Conditions:
    if Condition(Message):
      return False
  return True
Will