#Iterating over a list

1 messages · Page 1 of 1 (latest)

young torrent
#

Hello everyone! Sorry if my question is nooby or was discussed previously. I’m new to Gleam, Erlang and FP in general, so a bit confused. What is a standard preferred way to iterate over a list? For example, if I want to iterate over all of the items, do something with each one of them and return a new list with modified values, how do I go about it? I tried list.each but this one returns nil so I’m not really sure. Case expression works but only if I know the number of elements.

nocturne cobalt
#

is this what you're looking for?

worthy edge
young torrent
#

That was blazingly fast! Thanks! I think it’s what I’m looking for yes.

let l = [“hi”, “hi2”]
// the code below should return [“hisome_string”, “hi2some_string”], am I right?
l.map(l, fn(item) {item <> “some_string”)

young torrent
worthy edge
#

Yeah it requires a bit of a mindset shift to get used to it! If you are having any issues ask question, I’d be happy to help espeon

worthy edge
#

Gleam is not object oriented so there’s no map method on the list, you instead call the list.map function passing it the list as an argument

young torrent