Convolution (computer Science) - in Programming Languages

In Programming Languages

Convolution functions are often available in programming languages, often referred to as zip. In Lisp-dialects one can simply map the desired function over the desired lists, map is variadic in Lisp so it can take an arbitrary amount of lists as argument. An example from Clojure:

;; `nums' contains an infinite list of numbers (0 1 2 3 ...) (def nums (range)) (def tens ) (def firstname "Alice") ;; To convolve (0 1 2 3 ...) and into a vector, invoke `map vector' on them; same with list (map vector nums tens) ; ⇒ ( ) (map list nums tens) ; ⇒ ((1 10) (2 20) (3 30)) (map str nums tens) ; ⇒ ("110" "220" "330") ;; `map' truncates to the shortest sequence; note missing \c and \e from "Alice" (map vector nums tens firstname) ; ⇒ ( ) (map str nums tens firstname) ; ⇒ ("110A" "220l" "330i") ;; To unzip, apply `map vector' or `map list' (apply map list (map vector nums tens firstname)) ;; ⇒ ((0 1 2) (10 20 30) (\A \l \i))

In Common Lisp:

(defparameter nums '(1 2 3)) (defparameter tens '(10 20 30)) (defparameter firstname "Alice") (mapcar #'list nums tens) ;; ⇒ ((1 10) (2 20) (3 30)) (mapcar #'list nums tens (coerce firstname 'list)) ;; ⇒ ((1 10 #\A) (2 20 #\l) (3 30 #\i)) — truncates on shortest list ;; Unzips (apply #'mapcar #'list (mapcar #'list nums tens (coerce firstname 'list))) ;; ⇒ ((1 2 3) (10 20 30) (#\A #\l #\i))

Languages such as Python provide a zip function, older version (Python 2.*) allowed mapping None over lists to get a similar effect. zip in conjunction with the * operator unzips a list:

nums = tens = firstname = 'Alice' zipped = zip(nums, tens) zipped # ⇒ — zip zip(*zipped) # ⇒ — unzip zipped2 = zip(nums, tens, list(firstname)) zipped2 # ⇒ — zip, truncates on shortest zip(*zipped2) # ⇒ — unzip # mapping with `None' doesn't truncate; deprecated in Python 3.* map(None,nums, tens, list(firstname)) # ⇒

Haskell has a method of convolving sequences but requires a specific function for each arity (zip for two sequences, zip3 for three etc.), similarly the functions unzip and unzip3 are available for unzipping:

-- nums contains an infinite list of numbers nums = tens = firstname = "Alice" zip nums tens -- ⇒ — zip, truncates infinite list unzip $ zip nums tens -- ⇒ (,) — unzip zip3 nums tens firstname -- ⇒ — zip, truncates unzip3 $ zip3 nums tens firstname -- ⇒ (,,"Ali") — unzip

Read more about this topic:  Convolution (computer Science)

Famous quotes containing the words programming and/or languages:

    If there is a price to pay for the privilege of spending the early years of child rearing in the driver’s seat, it is our reluctance, our inability, to tolerate being demoted to the backseat. Spurred by our success in programming our children during the preschool years, we may find it difficult to forgo in later states the level of control that once afforded us so much satisfaction.
    Melinda M. Marshall (20th century)

    Science and technology multiply around us. To an increasing extent they dictate the languages in which we speak and think. Either we use those languages, or we remain mute.
    —J.G. (James Graham)