DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

The Latest Languages Topics

article thumbnail
Clojure: Destructuring
In The Joy of Clojure (TJoC) destructuring is described as a mini-language within Clojure. It's not essential to learn this mini-language; however, as the authors of TJoC point out, destructuring facilitates concise, elegant code. Making Code More Understandable One of the scariest things for those who are just now learning how to do some coding is the fact that they have to try to figure out what a seemingly impossible set of rules and structures means for the work that they are trying to do. It is not easy at all, and many people struggle with it in big ways. Fortunately, there are some people who are going about the process of destructuring code so that it may be broken into smaller and more manageable chunks. If this is to happen, then one can easily see how they can potentially get a lot more value from the process of coding, and even how they can contribute to it for themselves in the future. We need to be as encouraging of the next generation of coders as we possibly can because there is no question that they will ultimately have an outsized impact on how the future of coding is decided. If they are best set up to understand coding and to make sense of its many intricacies, then they will be able to handle it without problems. However, we need to support and encourage them along the way, and that all begins by making coding easier to understand in general. What is destructuring? Clojure supports abstract structural binding, often called destructuring, in let binding lists, fn parameter lists, and any macro that expands into a let or fn. -- http://clojure.org/special_forms The simplest example of destructuring is assigning the values of a vector. user=> (def point [5 7]) #'user/point user=> (let [[x y] point] (println "x:" x "y:" y)) x: 5 y: 7 note: I'm using let for my examples of destructuring; however, in practice, I tend to use destructuring in function parameter lists at least as often, if not more often. I'll admit that I can't remember ever using destructuring like the first example, but it's a good starting point. A more realistic example is splitting a vector into a head and a tail. When defining a function with an arglist** you use an ampersand. The same is true in destructuring. user=> (def indexes [1 2 3]) #'user/indexes user=> (let [[x & more] indexes] (println "x:" x "more:" more)) x: 1 more: (2 3) It's also worth noting that you can bind the entire vector to a local using the :as directive. user=> (def indexes [1 2 3]) #'user/indexes user=> (let [[x & more :as full-list] indexes] (println "x:" x "more:" more "full list:" full-list)) x: 1 more: (2 3) full list: [1 2 3] Vector examples are the easiest; however, in practice I find myself using destructuring with maps far more often. Simple destructuring on a map is as easy as choosing a local name and providing the key. user=> (def point {:x 5 :y 7}) #'user/point user=> (let [{the-x :x the-y :y} point] (println "x:" the-x "y:" the-y)) x: 5 y: 7 As the example shows, the values of :x and :y are bound to locals with the names the-x and the-y. In practice we would never prepend "the-" to our local names; however, using different names provides a bit of clarity for our first example. In production code you would be much more likely to want locals with the same name as the key. This works perfectly well, as the next example shows. user=> (def point {:x 5 :y 7}) #'user/point user=> (let [{x :x y :y} point] (println "x:" x "y:" y)) x: 5 y: 7 While this works perfectly well, creating locals with the same name as the keys become tedious and annoying (especially when your keys are longer than one letter). Clojure anticipates this frustration and provides :keys directive that allows you to specify keys that you would like as locals with the same name. user=> (def point {:x 5 :y 7}) #'user/point user=> (let [{:keys [x y]} point] (println "x:" x "y:" y)) x: 5 y: 7 There are a few directives that work while destructuring maps. The above example shows the use of :keys. In practice I end up using :keys the most; however, I've also used the :as directive while working with maps. The following example illustrates the use of an :as directive to bind a local with the entire map. user=> (def point {:x 5 :y 7}) #'user/point user=> (let [{:keys [x y] :as the-point} point] (println "x:" x "y:" y "point:" the-point)) x: 5 y: 7 point: {:x 5, :y 7} We've now seen the :as directive used for both vectors and maps. In both cases, the locale is always assigned to the entire expression that is being destructured. For completeness I'll document the :or directive; however, I must admit that I've never used it in practice. The :or directive is used to assign default values when the map being destructured doesn't contain a specified key. user=> (def point {:y 7}) #'user/point user=> (let [{:keys [x y] :or {x 0 y 0} point] (println "x:" x "y:" y)) x: 0 y: 7 Lastly, it's also worth noting that you can destructure nested maps, vectors and a combination of both. The following example destructures a nested map user=> (def book {:name "SICP" :details {:pages 657 :isbn-10 "0262011530"}) #'user/book user=> (let [{name :name {pages :pages isbn-10 :isbn-10} :details} book] (println "name:" name "pages:" pages "isbn-10:" isbn-10)) name: SICP pages: 657 isbn-10: 0262011530 As you would expect, you can also use directives while destructuring nested maps. user=> (def book {:name "SICP" :details {:pages 657 :isbn-10 "0262011530"}) #'user/book user=> user=> (let [{name :name {:keys [pages isbn-10]} :details} book] (println "name:" name "pages:" pages "isbn-10:" isbn-10)) name: SICP pages: 657 isbn-10: 0262011530 Destructuring nested vectors is also very straight-forward, as the following example illustrates user=> (def numbers [[1 2][3 4]]) #'user/numbers user=> (let [[[a b][c d]] numbers] (println "a:" a "b:" b "c:" c "d:" d)) a: 1 b: 2 c: 3 d: 4 Since binding forms can be nested within one another arbitrarily, you can pull apart just about anything -- http://clojure.org/special_forms The following example destructures a map and a vector at the same time. user=> (def golfer {:name "Jim" :scores [3 5 4 5]}) #'user/golfer user=> (let [{name :name [hole1 hole2] :scores} golfer] (println "name:" name "hole1:" hole1 "hole2:" hole2)) name: Jim hole1: 3 hole2: 5 The same example can be rewritten using a function definition to show the simplicity of using destructuring in parameter lists. user=> (defn print-status [{name :name [hole1 hole2] :scores}] (println "name:" name "hole1:" hole1 "hole2:" hole2)) #'user/print-status user=> (print-status {:name "Jim" :scores [3 5 4 5]}) name: Jim hole1: 3 hole2: 5 There are other (less used) directives and deeper explanations available on http://clojure.org/special_forms and in The Joy of Clojure. I recommend both. **(defn do-something [x y & more] ... )
August 12, 2022
by Jay Fields
· 15,020 Views · 1 Like
article thumbnail
Rust’s Ownership and Borrowing Enforce Memory Safety
By using an ownership model, Rust takes a novel approach to ensure memory safety at compile time.
August 11, 2022
by Senthil Nayagan
· 5,292 Views · 2 Likes
article thumbnail
Introduction: Querydsl vs. JPA Criteria
This article presents an introduction to the Querydsl series with the goal to highlight the difference from JPA Criteria.
Updated August 9, 2022
by Arnošt Havelka DZone Core CORE
· 19,424 Views · 8 Likes
article thumbnail
Snake-Based REST API (Python, Mamba, Hydra, and Fast API)
Using Python, Fast API, Hydra, and Mamba to build dockerized applications.
August 9, 2022
by Bartłomiej Żyliński DZone Core CORE
· 8,517 Views · 4 Likes
article thumbnail
Massively Scalable Geographic Graph Analytics: InfiniteGraph and Uber’s Hexagonal Hierarchical Spatial Index
In this article, learn how InfiniteGraph was used to build an implementation of the H3 Hexagonal Hierarchical Spatial Index.
August 9, 2022
by Daniel Hall
· 5,423 Views · 1 Like
article thumbnail
How to Build Spark Lineage for Data Lakes
Better understand the intricacies of Spark and how to build a lineage graph for data lakes.
August 8, 2022
by Lior Gavish
· 4,982 Views · 1 Like
article thumbnail
Build a Java Microservice With AuraDB Free
For today’s adventure, we want to build a Java microservice that connects to, and interacts with, graph data in a Neo4j AuraDB Free database.
Updated August 8, 2022
by Jennifer Reif DZone Core CORE
· 9,863 Views · 5 Likes
article thumbnail
Event Loop in JavaScript
JavaScript is single-threaded, meaning only one action can be performed at a time. Event Loop is the queue of commands to be called.
August 8, 2022
by Sebastian Z.
· 6,138 Views · 2 Likes
article thumbnail
ExpectedConditions in Selenium
In this Selenium tutorial, you will learn how to use Expected Conditions in Selenium to fix timing-related issues due to the dynamic loading of WebElements.
August 6, 2022
by Himanshu Sheth DZone Core CORE
· 12,199 Views · 3 Likes
article thumbnail
Handling File Uploads With NestJS and MySQL
Many developers despise dealing with file uploads. In this blog, we will teach you how to build a file uploading functionality using NestJS and MySQL.
August 6, 2022
by Clara Ekekenta
· 6,236 Views · 1 Like
article thumbnail
Amazon Lightsail: Virtual Cloud Server
Take an in-depth at topics relating to Amazon Lightsail, such as virtual private server basics, getting started with Lightsail, and Lightsail container service.
August 5, 2022
by Jawad Hasan Shani DZone Core CORE
· 9,120 Views · 3 Likes
article thumbnail
Spark-Radiant: Apache Spark Performance and Cost Optimizer
In this article, learn how to boost performance, reduce cost and increase observability for Spark Application using Spark-Radiant.
Updated August 5, 2022
by Saurabh Chawla
· 5,426 Views · 1 Like
article thumbnail
Diving Deep into Smart Contracts
Smart contracts allow two parties to enter into an agreement. Take a deep dive into smart contracts to see how to reuse a single contract over and over again.
August 5, 2022
by John Vester DZone Core CORE
· 76,657 Views · 5 Likes
article thumbnail
The Challenges of Ajax CDN
Learn why it’s no longer a best practice to host JavaScript a content delivery network (CDN) due to security considerations, network penalties, and to avoid a single point of failure.
August 5, 2022
by Mehdi Daoudi
· 8,565 Views · 1 Like
article thumbnail
Make Your Integration Seamless By Using Ballerina Client Connectors
Learn how to use the Ballerina OpenAPI tool to generate the ballerina client connector.
August 4, 2022
by Sumudu Nissanka
· 4,822 Views · 3 Likes
article thumbnail
XAMPP vs WAMP: Which Local Server Is Best?
Learn about XAMPP vs WAMP: what they are, similarities, differences, the advantages and disadvantages of XAMPP, and the advantages and disadvantages of WAMP.
August 4, 2022
by Praise Iwuh
· 11,328 Views · 1 Like
article thumbnail
Getting Started With Nose in Python
In this Python Nose tutorial, we deep-dive into the Nose framework, a test automation framework that extends unittest and further leverage Nose to perform Selenium test automation.
August 3, 2022
by Himanshu Sheth DZone Core CORE
· 7,894 Views · 4 Likes
article thumbnail
7 Great Terminal/CLI Tools Not Everyone Knows
Do you frequently work with the CLI? Whether you use Windows, macOS, or Linux, in this video, learn about 7 tools that may be unfamiliar to you.
August 3, 2022
by Marco Behler
· 18,224 Views · 7 Likes
article thumbnail
CockroachDB and Deno: Combining Two “Quick to Start” Technologies
This tutorial connects Deno with CockroachDB. Explore Deno CLI, Deno Deploy, CockroachDB, CockroachDB Serverless, and beginner TypeScript and SQL concepts.
August 2, 2022
by Morgan Winslow
· 4,769 Views · 1 Like
article thumbnail
Java Class Loading: Performance Impact
Learn more about class loading in Java and its performance impact.
August 1, 2022
by Ram Lakshmanan DZone Core CORE
· 7,698 Views · 4 Likes
  • Previous
  • ...
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×