I have two arrays of the same size and I want to run through them concurrently using a sequence iterator, so I am trying to consolidate them into one record but am having trouble initializing the fields The original arrays - let i = [| for i in 1 .. 17 -> i * 10|] let mutable tupArray = Array.create i.Length (0.f,0.f) The new record type - type rec1 = {i : int ; mutable tup : float32 * float32 } Below expresses what I am trying to do, but the syntax is off. let t1 = Array.init 17 (fun x -> {i <- float32(x)*10.f; tup <- (0.f,0.f)}) Any ideas on the correct systax for this, or on another way of doing it? Thanks

I think you want:

1
let t1 = [|for i in 1..17 -> {i=i; tup=0.f, 0.f}|]
By on 4/11/2009 6:51 AM ()

If I'm understanding correctly, you want Array.map2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 

> let xs = [| for i in 1..17 -> i * 10 |]
- let ys = [| for i in 1..17 -> i / 2 |]
- let zs = Array.map2 (fun x y -> x + y) xs ys ;;

val xs : int array
val ys : int array
val zs : int array

> zs;;
val it : int array
= [|10; 21; 31; 42; 52; 63; 73; 84; 94; 105; 115; 126; 136; 147; 157; 168; 178|]

Edit: And on record syntax:

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
 

type Combo = { i : int; f : float }

> let zs = Array.map2 (fun x y -> { i = x + y; f = float x / float y }) xs ys;;

val zs : Combo array

> zs;;
val it : Combo array
= [|{i = 10;
     f = infinity;}; {i = 21;
                      f = 20.0;}; {i = 31;
                                   f = 30.0;}; {i = 42;
                                                f = 20.0;}; {i = 52;
                                                             f = 25.0;};
    {i = 63;
     f = 20.0;}; {i = 73;
                  f = 23.33333333;}; {i = 84;
                                      f = 20.0;}; {i = 94;
                                                   f = 22.5;}; {i = 105;
                                                                f = 20.0;};
    {i = 115;
     f = 22.0;}; {i = 126;
                  f = 20.0;}; {i = 136;
                               f = 21.66666667;}; {i = 147;
                                                   f = 20.0;};
    {i = 157;
     f = 21.42857143;}; {i = 168;
                         f = 20.0;}; {i = 178;
                                      f = 21.25;}|]
By on 4/8/2009 6:32 PM ()

Thanks. Array.map works well.

My record only needs to map a single Array and looks like this:

1
2
3
4
5
6
7
8
let xs = [| for i in 1..17 -> i |]
type r = 
        { drv : float ; 
        mutable pt : float * float }

let pts = Array.map (fun x -> 
        { drv = float x * 10.0 ; 
        pt = (0.0, 0.0) }) xs 

Now, is there a way to get rid of the dummy array "xs"?

Also, since only the "drv" field needs to be initialized, is there a way to initialize just this field without affecting other fields?

By on 4/9/2009 5:32 PM ()

Is this maybe what you are after?

1
2
3
4
5
6
7
8
9
type r = 
    { drv : float ; 
      mutable pt : float * float } 

let pts =
    [| 1..17 |]
    |> Array.map (fun x -> 
                    { drv = float x * 10.0 ;
                      pt = (0.0, 0.0) })

And actually, a sequence could be more efficient in space for huge arrays, but is definatly slower in time by about 10x; use #time in FSI to do the timing test):

1
2
3
4
5
6
7
8
9
10
11
12
#light

type r = 
    { drv : float ; 
      mutable pt : float * float } 

let pts =
    { 1..17 }
    |> Seq.map (fun x -> 
                    { drv = float x * 10.0 ;
                      pt = (0.0, 0.0) })
    |> Array.of_seq
By on 4/10/2009 9:26 AM ()

Array.init can be used well here.

[link:research.microsoft.com]

EDIT: Oh, wait, were you originally using that? I'm confused what you need.

By on 4/9/2009 6:20 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper