List Manipulation

make

in Haskell:

\(a,b)->take b $ repeat a :: (a, Int) -> [a]

Example:

 def **(x:Long,n:Int):Long = 
	if (n == 0)  1 
	else List.make(n,x).reduceLeft[Long]((x,y)=>x*y)

map2

Combine to Lists with a function;

in Haskell: zipWith :: [a]→[b]→(a→b→c)→[c]

Example:

List.map2(List.range(0,10),"12345678".toList.reverse.map(_.toLong - '0'.toLong ))
	{(p,n)=> **(10,p) * n}.reduceLeft[Long]((x,y)=>x+y)

Convert between strict and lazy

To convert a strict collection xs to lazy, use

   xs.projection

To go from a lazy collection xs (for example, from a comprehension) to strict:

   xs.toList
   xs.toArray

See How do I write a loop? for how lazyness and eagerness affect for comprehensions of lists

 
common/list-manipulation.txt · Last modified: 2008/03/26 16:26 by ctw
 
Recent changes RSS feed Valid XHTML 1.0 Driven by DokuWiki