«List expander and contractor function» by moncrey

on 22 Mar'13 02:43 in utilityfunction

useful for expanding or reducing arrays by 2^x. I would like to know if there is a more concise way to write this! Any suggestions?

(duplicate post, sorry! forgot to log in!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
(
 b = {|x,y|
    var arr = List[];
    case
     {y % x.size == 0} // condition 1
     {
      var per = y / x.size;
      x.size.do{|oo|
       var pp = 1,i = 1;
       arr.add( x[oo] );
       while({i < per},{
        arr.add(0);
        i = i+1;
       });
      }
     }
     {x.size % y == 0} // condition 2
     {
      var pp = x.size / y, i = 0, sz = x.size;
      while({i < sz},{
       if(i % pp == 0)
        { arr.add(x[i]) };
       i=i+1;
      });
     };
    arr
   };
 a = [1,1,1,1];
 c = b.value(a,16) 
          // [ 1, 1, 1, 1 ]
          // becomes
          // [ 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ]

 b.value(c,4); 
          // [ 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 ]
          // becomes
          // [ 1, 1, 1, 1 ]
)
ancestors
«List expander and contractor function» by anonymous (private)
full graph
raw 805 chars (focus & ctrl+a+c to copy)
reception
comments
moncrey user 22 Mar'13 05:29

I suppose one consideration would be using the lists as streams so other objects can be used