Giezy Ramírez
Giezy Ramírez

Reputation: 91

What would be a good substitute for Enum.flat_map in Elixir?

I have a program that calls a function using

Enum.flat_map(text, &lex_raw_tokens/1)

but, i changed the function "lex_raw_tokens" to have 2 arguments, which means i can't use flat_map since flat_map/3 doesn't exist as far as i know. I'm not sure what function to use then, so far just calling the function as

lex_raw_tokens(text,line)

didn't quite work as i'm given an argument error. I think it's because the arguments given are lists and the function need separate list items, which flat_map takes care of. Any ideas?

Upvotes: 0

Views: 371

Answers (1)

Brett Beatty
Brett Beatty

Reputation: 5963

Perhaps the easiest way is to just to wrap &lex_raw_tokens/2 in an anonymous 1-arity function

# depending what `text` looks like
Enum.flat_map(text, &lex_raw_tokens(&1, line))
# or
Enum.flat_map(text, fn {text, line} -> lex_raw_tokens(text, line) end)

Upvotes: 1

Related Questions