[erlang-questions] List comprehensions assistance

Hugo Mills hugo@REDACTED
Tue Aug 18 00:44:59 CEST 2015


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150817/cd7c10a0/attachment.bin>


More information about the erlang-questions mailing list