How to iterate over a list in sophia?

Hello,

I’m translating a dapp from solidity to sophia and I’m currently stuck at trying to translate this function:

image

The inside getStatus (the one being called in the for loop) is another function that works with a singular id as parameter, which I’ve already implemented.

I’ve gotten this far:

image

But since we don’t have for loops in sophia, how do I iterate through the ids?

@bruteforce.chain :pray: last one I promise I wont bug you anymore :sweat_smile:

You need to do a recursion over the list and then apply whatever action you want to do on it.

Code:

@compiler >= 4

contract MyContract =
  
  entrypoint loop_list() : list(string) =
    let dummy_bytes_list : list(bytes(32)) = [ #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
                                             , #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
                                             , #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210
                                             , #fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210]
    
    // interesting stuff here:
    let bytes_list_to_string_list = map(convert_to_string, dummy_bytes_list)
    bytes_list_to_string_list
    
    
  function map(f : 'a => 'b, l : list('a)) : list('b) =
	switch(l)
		[] => []
		e :: l' => f(e) :: map(f, l')

  function convert_to_string ( bytes': bytes(32) ) : string =
    Bytes.to_str(bytes')

4 Likes

List.map is part of the sophia standard library now, no need to define it yourself. protocol/sophia_stdlib.md at master · aeternity/protocol · GitHub

Use it as:

include "List.aes"

...
  List.map(your_function, [your_list])
3 Likes

Thanks @philipp.chain! Totally forgot about that… :smiley: I haven’t used to having this library still… :slight_smile:

1 Like

@philipp.chain It works in https://testing.playground.aepps.com/editor, thank you so much!

But where can I find the List.aes file, in order to put it in my aeproject files in vscode to compile it from there as well?

I know it sounds stupid but using
image

and it gives me:

I think we should make a little video explaining this step by step. I will try to do that later. =)

1 Like

Yes, that would be very helpful :smile:

aeproject is not yet compatible with the standardlibrary use, as you won’t need the “List.aes” file in your project, the neccessarry functions will be included by the compiler. (cc @martingrigorov.chain)

Aeproject did implement logic here that did not behave to the api set by the node…

1 Like