Home > front end >  Erlang Binary Input Example
Erlang Binary Input Example

Time:11-16

Below erlang code fails with bad argument error in ** exception error: bad argument in function re:split/3.

What should be the input for build_keyword_set ? I have tried giving binary input, string input, list of strings and list of binary objects, but it fails for all.

-module(keywords).
-export([start/0]).

start() ->
    BinList = ["8"],
    Keywords = build_keyword_set(BinList),
    io:fwrite(" Keywords = ~p", [Keywords]).



-spec build_keyword_set(list(binary())) -> list(string()).
build_keyword_set([Query|Rest]) ->
    gb_sets:union(do_build_keyword_set(Query), build_keyword_set(Rest));
build_keyword_set([]) ->
    gb_sets:new().

-spec do_build_keyword_set(binary()) -> list(string()).
do_build_keyword_set(undefined) ->
    gb_sets:new();
do_build_keyword_set(Query) ->
    gb_sets:from_list(re:split(Query, re:compile("\\s ", [unicode]), [{return, binary}])).

CodePudding user response:

The problem is that the call to re:compile() returns a tuple {ok, Result}, so you can't pass it directly into re:split().

  • Related