Re: init



Rob Thorpe wrote:
In lisp I would do:-
(map 'vector #'(lambda (cp) (+ cp (/ halfwidth 2))) centerpoint)

Which does the same as the perl Logan Shaw posted.
Can't you just do the same in OCaml?

Initialising a new array is probably the best alternative:

Array.init 3 (fun i -> centerpt.(i) +. halfwidth /. 2.)

You can also replace the elements in-place, as you probably would in C (for
extra speed):

for i=0 to 2 do
boxcenter.(i) <- centerpt.(i) +. halfwidth /. 2.
done;

I often use an implicit integer range data structure to replace loops:

iter (fun i -> boxcenter.(i) <- centerpt.(i) +. halfwidth /. 2.) 0 3

In reality, of course, I'd write a Vec3 module and just do something like:

centerpt +| Vec3.of (halfwidth /. 2.)

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/products/ocaml_for_scientists/chapter1.html
.



Relevant Pages

  • Re: any function to handle this kind of counting?
    ... the OP would find APL equally as opaque as OCaml. ... rebuilding the dynamic data structure as you go is gonna be ... > So a brute force array-based implementation computes 40 times more ... Or size the array using two passes. ...
    (comp.programming)
  • Re: OCAML versus Scheme versus Clean
    ... compiler inline array references. ... now the OCAML code is ... only three times slower than the Bigloo code. ... porting the program and the compiler to ...
    (comp.lang.functional)
  • Re: Array of Objects Initialization problem C#.
    ... i think you are saying that you are initialising an array to 1000 ... But you want to add to the array as you need without prior initialisation? ... So look up arraylists and lists ... public void InitializeRules(XmlNodeList RuleNodes) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Please Help: Script Logic/Multidimensional Array
    ... var inputs = new Array; ... Given that fileQueuehas just been initialised as an array, ... I presume this stems from your initialising the array with a length - ... Your problems stem from that. ...
    (comp.lang.javascript)
  • Re: About alternatives to Matlab
    ... I had thought that all of the array operations were allocating new arrays at ... arrays that Ocaml would not need. ... You can get the brevity of the Python approach in C. ... getting slicing into his language but the use of slicing in this Python ...
    (comp.lang.python)