Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Zipping (computer science)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== In programming languages == Zip [[Subroutine|functions]] are often available in [[programming language]]s, often referred to as {{mono|zip}}. In [[Lisp (programming language)|Lisp]]-dialects one can simply {{mono|[[Map (higher-order function)|map]]}} the desired function over the desired lists, {{mono|map}} is [[Variadic function|variadic]] in Lisp so it can take an arbitrary number of lists as argument. An example from [[Clojure]]:<ref>[http://clojuredocs.org/clojure_core/1.2.0/clojure.core/map map] from ClojureDocs</ref> <syntaxhighlight lang="clojure"> ;; `nums' contains an infinite list of numbers (0 1 2 3 ...) (def nums (range)) (def tens [10 20 30]) (def firstname "Alice") ;; To zip (0 1 2 3 ...) and [10 20 30] into a vector, invoke `map vector' on them; same with list (map vector nums tens) ; β ([0 10] [1 20] [2 30]) (map list nums tens) ; β ((0 10) (1 20) (2 30)) (map str nums tens) ; β ("010" "120" "230") ;; `map' truncates to the shortest sequence; note missing \c and \e from "Alice" (map vector nums tens firstname) ; β ([0 10 \A] [1 20 \l] [2 30 \i]) (map str nums tens firstname) ; β ("010A" "120l" "230i") ;; 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)) </syntaxhighlight> In [[Common Lisp]]: <syntaxhighlight lang="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)) </syntaxhighlight> Languages such as [[Python (programming language)|Python]] provide a {{mono|zip()}} function.<ref name="pydocs">[https://docs.python.org/library/functions.html#map map(function, iterable, ...)] from section Built-in Functions from Python v2.7.2 documentation</ref> {{mono|zip()}} in conjunction with the {{mono|*}} operator unzips a list:<ref name="pydocs"/> <syntaxhighlight lang="pycon"> >>> nums = [1, 2, 3] >>> tens = [10, 20, 30] >>> firstname = 'Alice' >>> zipped = list(zip(nums, tens)) >>> zipped [(1, 10), (2, 20), (3, 30)] >>> list(zip(*zipped)) # unzip [(1, 2, 3), (10, 20, 30)] >>> zipped2 = list(zip(nums, tens, list(firstname))) >>> zipped2 # zip, truncates on shortest [(1, 10, 'A'), (2, 20, 'l'), (3, 30, 'i')] >>> list(zip(*zipped2)) # unzip [(1, 2, 3), (10, 20, 30), ('A', 'l', 'i')] </syntaxhighlight> [[Haskell (programming language)|Haskell]] has a method of zipping sequences but requires a specific function for each [[arity]] ({{mono|zip}} for two sequences, {{mono|zip3}} for three etc.),<ref>[http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:zip zip :: <nowiki>[a] -> [b] -> [(a, b)]</nowiki>] from Prelude, Basic libraries</ref> similarly the functions {{mono|unzip}} and {{mono|unzip3}} are available for unzipping: <syntaxhighlight lang="haskell"> -- nums contains an infinite list of numbers [1, 2, 3, ...] nums = [1..] tens = [10, 20, 30] firstname = "Alice" zip nums tens -- β [(1,10), (2,20), (3,30)] β zip, truncates infinite list unzip $ zip nums tens -- β ([1,2,3], [10,20,30]) β unzip zip3 nums tens firstname -- β [(1,10,'A'), (2,20,'l'), (3,30,'i')] β zip, truncates unzip3 $ zip3 nums tens firstname -- β ([1,2,3], [10,20,30], "Ali") β unzip </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)