Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang List Filter Syntax

Tags:

erlang

I'm trying to write some Erlang that would filter an array in the form:

[{dakota, "cold and snowy"}, {california, "perfect weather"}] % and so on

Here is what I've got - I get a syntax error when I try to make a .beam from werl.

-module(matcher).
-export([findkeywords/2]).

findkeywords(Word, Arr) -> 
    IsMatch = fun({Key, Desc}) -> 
        lists:any(fun(X) -> X==Word end, string:tokens(Desc, " ")),
    lists:filter(IsMatch, [{K, V} || {K, V} <- Arr]).

Can anyone spot where my syntax is off?

like image 821
t3rse Avatar asked Dec 12 '22 08:12

t3rse


1 Answers

I saw your call to arms on twitter and just had to come take a look. :D

If you want this to compile, you're just missing an end on your fun on line 6. Add it in and it compiles without complaint.

-module(matcher).
-export([findkeywords/2]).

findkeywords(Word, Arr) -> 
    IsMatch = fun({Key, Desc}) -> 
        lists:any(fun(X) -> X==Word end, string:tokens(Desc, " ")) end, % ADD THE END HERE
    lists:filter(IsMatch, [{K, V} || {K, V} <- Arr]).

You can clean this up a bit too, unless this is an exercise in string matching for yourself. The string module has str(String, SubString) -> Index and rstr(String, SubString) -> Index that are described as such in the Erlang Manual:

Returns the position where the first/last occurrence of SubString begins in String. 0 is returned if SubString does not exist in String. For example:

> string:str(" Hello Hello World World ", "Hello World").
8 

Using this tidies it up a bit, and you could even shorten the whole thing into a one liner. The list comprehension is unnecessary as the data is already in the format that you're trying to feed it in.

-module(matcher).
-export([findkeywords/2]).

findkeywords(Word, Arr) -> 
    lists:filter(fun({_Key, Desc}) -> string:str(Desc, Word) > 0 end, Arr).
like image 130
Cody Avatar answered Dec 18 '22 05:12

Cody