liberty-eiffel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Javascript-like for_each


From: Paolo Redælli
Subject: Javascript-like for_each
Date: Wed, 9 Mar 2022 14:48:34 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.1.0

I may have been contaminated by _javascript_... :)

See https://developer.mozilla.org/en-US/docs/Web/_javascript_/Reference/Global_Objects/Array/forEach

// Arrow function
forEach((element) => { /* ... */ })
forEach((element, index) => { /* ... */ })
forEach((element, index, array) => { /* ... */ })

// Callback function
forEach(callbackFn)
forEach(callbackFn, thisArg)

// Inline callback function
forEach(function(element) { /* ... */ })
forEach(function(element, index) { /* ... */ })
forEach(function(element, index, array){ /* ... */ })
forEach(function(element, index, array) { /* ... */ }, thisArg)


one can call in [2,3,5,7,11].forEach( (e,i) => {console.log( i+"-nth prime is "+e);})

to get

0-nth prime is 2
1-nth prime is 3
2-nth prime is 5
3-nth prime is 7
4-nth prime is 11

Liberty Eiffel allows for an almost brief syntax:

class FOO
create make
feature
      make is
   do
      (<<2,3,5,7,11>>).do_all(agent (x: INTEGER_8) do
      print(x.out+" is prime%N");
      end)
   end
end

Where the actual code equivalent is  (<<2,3,5,7,11>>).do_all(agent (x: INTEGER_8) do print(x.out+" is prime%N"); end)

Which is a little more verbose. Since the index and the container is often useful in the called agent I think that we may evaluate to change the signature of for_each to
    for_each (action: PROCEDURE[TUPLE[AN_ITEM,INTEGER,ANY]])

becuase TUPLE[AN_ITEM] and TUPLE[AN_ITEM,INTEGER] conform to TUPLE[AN_ITEM,INTEGER,ANY]

reply via email to

[Prev in Thread] Current Thread [Next in Thread]