Skip to content

foreach like pattern #1383

Answered by vkleen
mipmip asked this question in Q&A
Discussion options

You must be logged in to vote

This is a textbook application for std.array.map:

["jane", "pete", "richie"]
|> std.array.map
  (
    fun name =>
      {
        username = name,
        email = "%{name}@nickel.org"
      }
  )

In general, a foreach like pattern where the elements are handled independently from each other calls for std.array.map. If you need to manipulate some state depending on each element to arrive at a final value, you'd typically want to look into std.array.fold_left.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by mipmip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants