Back
mapcat (clj)
(source)function
(mapcat f)
(mapcat f & colls)
Returns the result of applying concat to the result of applying map
to f and colls. Thus function f should return a collection. Returns
a transducer when no collections are provided
Examples
clojure
(ns clojure.test-clojure.reducers
(:require [clojure.core.reducers :as r]
[clojure.test.generative :refer (defspec)]
[clojure.data.generators :as gen])
(:use clojure.test))
(defequivtest test-mapcat
[mapcat r/mapcat #(into [] %)]
[(fn [x] [x])
(fn [x] [x (inc x)])
(fn [x] [x (inc x) x])])
(deftest test-mapcat-obeys-reduced
(is (= [1 "0" 2 "1" 3]
(->> (concat (range 100) (lazy-seq (throw (Exception. "Too eager"))))
(r/mapcat (juxt inc str))
(r/take 5)
(into [])))))
penpot/penpot
(ns app.main.ui.icons
(:require
[clojure.core :as c]
[cuerdas.core :as str]
[rumext.v2]))
(defmacro collect-icons
[]
(let [ns-info (:ns &env)]
`(cljs.core/js-obj
~@(->> (:defs ns-info)
(map val)
(filter (fn [entry] (-> entry :meta :icon)))
(mapcat (fn [{:keys [name] :as entry}]
[(-> name c/name str/camel str/capital) name]))))))
clojure/core.typed
;copied from clojure.tools.analyzer.passes.constant-lifter
(ns clojure.core.typed.analyzer.common.passes.constant-lifter
(:require [clojure.core.typed.analyzer.common :as common]
[clojure.core.typed.analyzer.common.utils :refer [const-val]]))
(defmethod constant-lift :map
[{:keys [keys vals form env] :as ast}]
(if (and (every? :literal? keys)
(every? :literal? vals)
(empty? (meta form)))
(let [c (into (empty form)
(zipmap (mapv const-val keys)
(mapv const-val vals)))
c (if (= (class c) (class form))
c
(apply array-map (mapcat identity c)))]
(merge (dissoc ast :keys :vals :children)
{:op :const
::common/op ::common/const
:val c
:type :map
:literal? true}))
ast))
mikera/core.matrix
WARNING: because they lack efficient indexed access, sequences will perform badly for most
array operations. In general they should be converted to other implementations before use."
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.implementations :as imp]
#?(:clj [clojure.core.matrix.macros :refer [scalar-coerce error]]))
#?(:clj (:import [clojure.lang ISeq])
:cljs (:require-macros [clojure.core.matrix.macros :refer [scalar-coerce error]])))
(extend-protocol mp/PFunctionalOperations
ISeq
(element-seq [m]
(if (== 0 (long (mp/dimensionality (first m))))
m ;; handle 1D case, just return this sequence unchanged
(mapcat mp/element-seq m)))
(element-map
([m f]
(mapv #(mp/element-map % f) m))
([m f a]
(let [[m a] (mp/broadcast-compatible m a)]
(mapv #(mp/element-map % f %2) m (mp/get-major-slice-seq a))))
([m f a more]
(let [[m a & more] (apply mp/broadcast-compatible m a more)] ; FIXME
(mapv #(mp/element-map % f %2 %3) m (mp/get-major-slice-seq a) (map mp/get-major-slice-seq more)))))
(element-map!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!")))
(element-reduce
([m f]
(reduce f (mapcat mp/element-seq m)))
([m f init]
(reduce f init (mapcat mp/element-seq m)))))
typedclojure/typedclojure
(ns ^:no-doc typed.ann.clojure
"Type annotations for the base Clojure distribution."
#?(:cljs (:require-macros [typed.ann-macros.clojure :as macros]))
(:require [clojure.core :as cc]
[typed.clojure :as t]
#?(:clj [typed.ann-macros.clojure :as macros])
#?(:clj typed.ann.clojure.jvm) ;; jvm annotations
#?(:clj clojure.core.typed))
#?(:clj
(:import (clojure.lang PersistentHashSet PersistentList
APersistentMap #_IPersistentCollection
#_ITransientSet
IRef)
(java.util Comparator Collection))))
cc/map (t/All [c a b :..] (t/IFn [[a :-> c] :-> (t/Transducer a c)]
[[a b :.. b :-> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) :.. b :-> (t/NonEmptyASeq c)]
[[a b :.. b :-> c] (t/Seqable a) (t/Seqable b) :.. b :-> (t/ASeq c)]))
cc/mapv (t/All [c a b :..] (t/IFn [[a b :.. b :-> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) :.. b :-> (t/NonEmptyAVec c)]
[[a b :.. b :-> c] (t/Seqable a) (t/Seqable b) :.. b :-> (t/AVec c)]))
cc/mapcat (t/All [c a b :..] (t/IFn
[[a :-> (t/Seqable c)] :-> (t/Transducer a c)]
[[a b :.. b :-> (t/Seqable c)] (t/Seqable a) (t/Seqable b) :.. b :-> (t/ASeq c)]))
cc/cat (t/All [x] (t/Transducer (t/Seqable x) x))
#?@(:cljs [] :default [
cc/pmap (t/All [c a b :..] (t/IFn [[a b :.. b :-> c] (t/NonEmptySeqable a) (t/NonEmptySeqable b) :.. b :-> (t/NonEmptyASeq c)]
[[a b :.. b :-> c] (t/Seqable a) (t/Seqable b) :.. b :-> (t/ASeq c)]))
cc/pcalls (t/All [r] [[:-> r] :* :-> (t/ASeq r)])
])
ReactiveX/RxClojure
(ns rx.lang.clojure.realized-test
(:require [rx.lang.clojure.realized :as r]
[rx.lang.clojure.core :as rx]
[rx.lang.clojure.future :as rx-future]
[rx.lang.clojure.blocking :as rx-blocking]
[clojure.test :refer [deftest testing is]]))
; Playing with some expressing some of the video stuff with this.
(comment
(->> (get-list-of-lists user-id)
(rx/mapcat (fn [list]
(->> (video-list->videos list)
(rx/take 10))))
(rx/mapcat (fn [video]
(->> (r/let-realized [md (video->metadata video)
bm (video->bookmark video)
rt (video->rating video user-id)]
{:id (:id video)
:title (:title md)
:length (:duration md)
:bookmark bm
:rating {:actual (:actual-star-rating rt)
:average (:average-star-rating rt)
:predicted (:predicted-star-rating rt) } })))))
(->> (get-list-of-lists user-id)
(rx/mapcat (fn [list]
(->> (video-list->videos list)
(rx/take 10))))
(rx/mapcat (fn [video]
(->> (r/realized-map :md (video->metadata video)
:bm (video->bookmark video)
:rt (video->rating video user-id))
(rx/map (fn [{:keys [md bm rt]}]
{:id (:id video)
:title (:title md)
:length (:duration md)
:bookmark bm
:rating {:actual (:actual-star-rating rt)
:average (:average-star-rating rt)
:predicted (:predicted-star-rating rt) } }))))))
(->> (get-list-of-lists user-id)
(rx/mapcat (fn [list]
(->> (video-list->videos list)
(rx/take 10))))
(rx/mapcat (fn [video]
(->> (r/realized-map :id (:id video)
:md [(video->metadata video)
first
#(select-keys % [:title :duration])]
:bookmark (video->bookmark video)
:rating [(video->rating video user-id)
first
#(hash-map :actual (:actual-star-rating %)
:average (:average-star-rating %)
:predicted (:predicted-star-rating %))])
(rx/map (fn [m]
(-> m
(merge (:md m))
(dissoc :md)))))))))