Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

comparing dot product implementations

Name: Anonymous 2007-03-23 13:13 ID:tyeGF24s

C#:
public delegate T Function<T1, T>(T1 t1);
 public delegate T Function<T1, T2, T>(T1 t1, T2 t2);
 static IEnumerable<T> Map<T>(IEnumerable<T> v1, IEnumerable<T> v2, Function<T, T, T> proc) {
   using (IEnumerator<T> e1 = v1.GetEnumerator())
     using (IEnumerable<T> e2 = v2.GetEnumerator())
       while (e1.MoveNext() && e2.MoveNext())
         yield return proc(e1.Current, e2.Current);
 }


 static T Reduce<T1, T>(IEnumerable<T1> v, Function<T, T1, T> proc) {
   T result = default(T);
   foreach (T1 item in v)
     result = proc(result, item);
   return result;
 }


which would allow you to write the dot product function like this:

 static double DotProduct(IEnumerable<double> v1, IEnumerable<double> v2) {
   return Reduce(
             Map(v1, v2, delegate(double d1, double d2) { return d1 * d2; }),
             delegate (double sum, double item) { return sum + item; }
   );
 }


Perl:

sub dot($a,$b) {
    $sum += $a[$_] * $b[$_] for 0 .. @a-1;
}


Haskell:

dot = (sum .) . zipWith (*)

Name: Anonymous 2007-03-23 13:20 ID:Frq6I0z3

zipWith (*)

Name: Anonymous 2007-03-23 16:27 ID:V9kyEP7g

<What do these brackets do?>

Name: Anonymous 2007-03-23 16:29 ID:r42xUDku

C#, the new Pascal of bondage and discipline languages. It's fitting, really, considering it is the work of the guy who gave us Delphi. (I use "gave" in the same sense as "monkeys who gave humanity SIV".)

Name: Anonymous 2009-01-14 14:34

Its machine code

Name: Anonymous 2009-07-12 6:15

sage sage sage sage sage sage sage sage that world, the real as are to used asrep insert  splitAt f in  n '' {print echo {print -n do '' '' I I I avg System.main.out.printline("BAtting System.print.err("HURRRR I'm ERRRRR"); to use over are are problem, over problem,   fflush(f); = 0;1;i++) i for(int 0;1;i++)   |

Name: Anonymous 2010-11-02 20:18

Name: Anonymous 2010-12-17 1:40

This post brought to you by the Gay Nigger Association of America

Name: Anonymous 2010-12-22 3:35

<

Name: Sgt.Kabu淢礴kiman饵ⴿ 2012-05-28 23:34

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List