DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Clojure: Equivalent to Scala’s flatMap/C#’s SelectMany

Clojure: Equivalent to Scala’s flatMap/C#’s SelectMany

Mark Needham user avatar by
Mark Needham
·
Jul. 04, 11 · Java Zone · Interview
Like (0)
Save
Tweet
5.62K Views

Join the DZone community and get the full member experience.

Join For Free

I’ve been playing around with Clojure a bit over the weekend and one thing I got stuck with was working out how to achieve the functionality provided by Scala’s flatMap or C#’s SelectMany methods on collections.

I had a collection of zip files and wanted to transform that into a collection of all the file entries in those files.

If we just use map then we’ll end up with a collection of collections which is more difficult to deal with going forward.

In Scala we’d do the following:

import scala.collection.JavaConversions._
 
val zip1 = new ZipFile(new File("/Users/mneedham/Documents/my-zip-file.zip"))
val zip2 = new ZipFile(new File("/Users/mneedham/Documents/my-zip-file2.zip"))
 
List(zip1, zip2).flatMap(_.entries)

I was originally make using of map followed by flatten but I learnt from my colleague Phil Calcado that the function I wanted is mapcat which leads to this solution:

(def zip1 (new ZipFile (file "/Users/mneedham/Documents/my-zip-file.zip")))
(def zip2 (new ZipFile (file "/Users/mneedham/Projects/springer/aim/src/test/unit/resources/small_delivery.zip")))
 
(mapcat (fn [file] (enumeration-seq (.entries file))) (list zip1 zip2))

I also learnt about the various functions available to create sequences, such as enumeration-seq from other types which are listed at the bottom of this page.

Scala uses implicit conversions to do that and presumably you’d hide away the conversion in a helper function in Clojure.

From http://www.markhneedham.com/blog/2011/07/03/clojure-equivalent-to-scalas-flatmapcs-selectmany/

Scala (programming language) Clojure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Is Your Code DRY or WET?
  • Top 10 Automated Software Testing Tools
  • Modern Application Security Requires Defense in Depth
  • Applying Domain-Driven Design Principles to Microservice Architectures

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo