Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure map into vector

Tags:

clojure

I have this collection of movies by directors:

([{:title "Blade Runner 2049", :genre "Action", :year 2017}
  {:title "Gladiator", :genre "Drama", :year 2000}
  {:title "Mars", :genre "Adventure", :year 2015}
  {:title "American Gangster", :genre "Crime", :year 2007}]
 [{:title "The Godfather", :genre "Crime", :year 1972}
  {:title "Apocalypse Now", :genre "Drama", :year 1979}
  {:title "Jack", :genre "Comedy", :year 1996}]
 [{:title "The Wolf", :genre "Comedy", :year 2013}
  {:title "GoodFellas", :genre "Crime", :year 1990}
  {:title "The Departed", :genre "Drama", :year 2006}
  {:title "The Aviator", :genre "Drama", :year 2004}])

which is a list of 3 vectors(Directors) and the corresponding movies

If I do:

(doseq [directors-movie movies]
   (doseq [movie directors-movie]
     (println movie)))

I can print all movies one by one:

#imdb.movie.Movie{:title Blade Runner 2049, :genre Action, :year 2017}
#imdb.movie.Movie{:title Gladiator, :genre Drama, :year 2000}
#imdb.movie.Movie{:title Mars, :genre Adventure, :year 2015}
#imdb.movie.Movie{:title American Gangster, :genre Crime, :year 2007}
#imdb.movie.Movie{:title The Godfather, :genre Crime, :year 1972}
#imdb.movie.Movie{:title Apocalypse Now, :genre Drama, :year 1979}
#imdb.movie.Movie{:title Jack, :genre Comedy, :year 1996}
#imdb.movie.Movie{:title The Wolf, :genre Comedy, :year 2013}
#imdb.movie.Movie{:title GoodFellas, :genre Crime, :year 1990}
#imdb.movie.Movie{:title The Departed, :genre Drama, :year 2006}
#imdb.movie.Movie{:title The Aviator, :genre Drama, :year 2004}

What I need is to put all this movies into a Vector.

Thanks, R.

like image 432
ibercode Avatar asked Dec 13 '22 19:12

ibercode


2 Answers

(reduce into [] l)

or

(into [] cat l)

will do (where l is your input list)

like image 175
Michiel Borkent Avatar answered Mar 01 '23 02:03

Michiel Borkent


This will do it:

(-> movies
    flatten
    vec)

Alternatives are:

(->> movies
     (apply concat)
     vec)

, and:

(->> movies
     (mapcat identity)
     vec) 
like image 45
Chris Murphy Avatar answered Mar 01 '23 02:03

Chris Murphy