optimization of list comprehensions

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Tue Mar 7 22:49:47 CET 2006

Richard O'Keefe wrote: 

> expand(Tops, TIds) when is_list(TIds) ->
>   #ctxtTop{frID=TId1, toTId=TId2, dir=Dir, strID=SId}
>	|| #ctxtTop{frId=F, toTId=T, dir=Dir, strId=SId} <- Tops,
>	   {TIds1,TIds2} = case {F,  T  }
>	                     of {'*','*'} -> {TIds,TIds}
>	                      ; {_,  '*'} -> {[F], TIds}
>	                      ; {'*',_  } -> {TIds,[T] }
>	                   end,
>	   TId1 <- TIds1,
>	   TId2 <- TIds2,
>	   TId2 =/= Tid1].

>If the order does matter, we might need an extra
> reverse.  But we don't appear to need any folds,
> unless I have misunderstood the example.

I'm sure you're right. The function was a suggestion
for a re-write of something that was _considerably_
longer (about 50 LOC, using neither folds, maps nor 
lcs, but with lots of inner-loop appends). I had 
presented three different pieces of code and 
simplified them in steps, to illustrate how one 
can chip away at hairy code and gradually see the 
patterns appear.

Obviously, I quit iterating before reaching fixpoint.

/Ulf W

More information about the erlang-questions mailing list