[erlang-questions] List comprehensions assistance
Garrett Smith
g@REDACTED
Tue Aug 18 13:12:00 CEST 2015
I'd use this one weird trick:
convert({Address, Stat}) ->
[{<<"address">>, list_to_binary(inet:ntoa(Address))},
{<<"stat">>, Stat}].
And then:
Converted = [convert(Result) || Result <- Results]
In general if the code is blinding you and it's hard to see what's
going on, a function, or even a few, can do wonders for clarity.
On Mon, Aug 17, 2015 at 9:35 PM, Technion <technion@REDACTED> wrote:
> Hi,
>
> Just a slight update, it took a bit more wrangling to get the correct format (ie, something jiffy liked)
>
> ScanConverted = [{[{<<"address">>, list_to_binary(inet:ntoa(X))},
> {<<"stat">>, Y}]} || {X, Y} <- Results],
>
>
> ________________________________________
> From: erlang-questions-bounces@REDACTED <erlang-questions-bounces@REDACTED> on behalf of Technion <technion@REDACTED>
> Sent: Tuesday, 18 August 2015 12:17 PM
> To: Hugo Mills
> Cc: Erlang Questions
> Subject: Re: [erlang-questions] List comprehensions assistance
>
> Hi Hugo,
>
> Many thanks for the response.
>
> Argh! Nothing worse than spending hours on something only to introduce a dumb mistake on the home stretch.
> ________________________________________
> From: Hugo Mills <hugo@REDACTED>
> Sent: Tuesday, 18 August 2015 8:44 AM
> To: Technion
> Cc: Erlang Questions
> Subject: Re: [erlang-questions] List comprehensions assistance
>
> On Mon, Aug 17, 2015 at 10:40:35PM +0000, Technion wrote:
>> Hi,
>>
>>
>> I have the following piece of code, which works correctly:
>>
>>
>> ConvertFun = fun({X, Y}) -> {[{<<"address">>,
>> list_to_binary(inet:ntoa(X))}, {<<"stat">>, Y}]} end,
>> ScanConverted = lists:map(ConvertFun, Results),
>>
>>
>> The reason for this very specific mapping is to convert it into exactly the format jiffy expects (in order to generate the correct JSON). I've been experimenting with rewriting this as a list comprehension (largely as a learning exercise).
>>
>>
>> Code like this fails to compile:
>>
>>
>> ScanConverted = [[{<<"address">>, list_to_binary(inet:ntoa(X))},
>> {<<"stat">>, Y}]| {X, Y} <- Results]
>>
>>
>> src/ipmangle.erl:31: syntax error before: '<-'
>>
>>
>> Any pointers on this would be appreciated.
>
> A list comprehension is [ Expr || Var <- List ]. You have
> [ Expr | Var <- List ].
>
> Hugo.
>
> PS. Yay! One I can answer! ;)
>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
> --
> Hugo Mills | I believe that it's closely correlated with the
> hugo@REDACTED carfax.org.uk | aeroswine coefficient
> http://carfax.org.uk/ |
> PGP: E2AB1DE4 | Adrian Bridgett
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
More information about the erlang-questions
mailing list