August 13, 2018

4clojure 143 - Dot Product

Create a function that computes the dot product of two sequences. You may assume that the vectors will have the same length.

(ns live.test
  (:require [cljs.test :refer-macros [deftest is run-tests]]))

(defn dot-product [a b]
  (reduce +
    (map * a b)))

(deftest dot-product-test
  (is (= 0 (dot-product [0 1 0] [1 0 0])))
  (is (= 3 (dot-product [1 1 1] [1 1 1])))
  (is (= 32 (dot-product [1 2 3] [4 5 6])))
  (is (= 256 (dot-product [2 5 6] [100 10 1]))))

(run-tests)
Tags: coding exercises KLIPSE 4clojure Clojure