Back
flatten (clj)
(source)function
(flatten x)
Takes any nested combination of sequential things (lists, vectors,
etc.) and returns their contents as a single, flat lazy sequence.
(flatten nil) returns an empty sequence.
Examples
Netflix/PigPen
(test-diff
(->> s
(#'pigpen.oven/braise {})
(#'pigpen.pig.oven/dec-rank {})
(#'pigpen.oven/optimize-binds {})
(map #(select-keys % [:projections :ancestors :fields :id :type])))
'[{:type :return, :id return1, :fields [return1/value]}
{:type :rank, :id rank2_0, :fields [rank2/index rank2/value], :ancestors [return1]}
{:type :project
:id project4
:fields [project4/value]
:ancestors [rank2_0]
:projections [{:type :projection
:expr {:type :code
:init (clojure.core/require (quote [pigpen.runtime]))
:func (clojure.core/comp (pigpen.runtime/process->bind (pigpen.runtime/pre-process nil :frozen))
(pigpen.runtime/process->bind (fn [[i v]] [(dec i) v]))
(pigpen.runtime/map->bind (pigpen.runtime/with-ns pigpen.pig.oven-test vector))
(pigpen.runtime/process->bind (pigpen.runtime/post-process nil :frozen)))
:udf :seq
:args [rank2_0/$0 rank2_0/value]}
:flatten true
:alias [project4/value]
:types nil}]}]))))
(test-diff
(->> s
(#'pigpen.oven/braise {})
(#'pigpen.oven/optimize-binds {})
(#'pigpen.pig.oven/split-project {})
(map #(select-keys % [:projections :ancestors :fields :id :type])))
'[{:type :return
:id return1
:fields [return1/value]}
{:type :project
:id project3_0
:fields [project3_0/value0]
:ancestors [return1]
:projections [{:type :projection
:expr {:type :code
:init (clojure.core/require (quote [pigpen.runtime]))
:func (clojure.core/comp (pigpen.runtime/process->bind (pigpen.runtime/pre-process nil :frozen))
(pigpen.runtime/map->bind (pigpen.runtime/with-ns pigpen.pig.oven-test inc))
(pigpen.runtime/process->bind (pigpen.runtime/post-process nil :frozen)))
:udf :seq
:args [return1/value]}
:flatten false
:alias [project3_0/value0]}]}
{:type :project
:id project3
:fields [project3/value]
:ancestors [project3_0]
:projections [{:type :projection
:expr {:type :field
:field project3_0/value0}
:flatten true
:alias [project3/value]
:types nil}]}]))))
Netflix/PigPen
(deftest test-code$
(test-diff
(code$ :seq
'(require '[pigpen.runtime])
'(var clojure.core/prn)
["a" 'r0/b 'c/d])
'{:type :code
:udf :seq
:init (require (quote [pigpen.runtime]))
:func (var clojure.core/prn)
:args ["a" r0/b c/d]}))
(deftest test-projection-field$
(test-diff
(projection-field$ 'r0/value)
'{:type :projection
:expr {:type :field
:field r0/value}
:flatten false
:alias [value]}))
(deftest test-projection-func$
(test-diff
(projection-func$ '[value] true
(code$ :seq
`(require '[pigpen.runtime])
`identity
'[r0/value]))
'{:type :projection
:expr {:type :code
:init (clojure.core/require (quote [pigpen.runtime]))
:func clojure.core/identity
:udf :seq
:args [r0/value]}
:flatten true
:alias [value]}))
(deftest test-project$*
(with-redefs [pigpen.raw/pigsym pigsym-zero]
(test-diff
(project$* [(projection-field$ 'r0/value)] {} 'r0)
'{:type :project
:id project0
:description nil
:ancestors [r0]
:fields [project0/value]
:field-type :frozen
:projections [{:type :projection
:expr {:type :field
:field r0/value}
:flatten false
:alias [project0/value]}]
:opts {:type :project-opts}})))
(deftest test-project$
(with-redefs [pigpen.raw/pigsym pigsym-zero]
(test-diff
(project$ [(projection-field$ 'r0/value)] {} r0)
'{:type :project
:id project0
:description nil
:ancestors [{:id r0
:fields [r0/value]
:field-type :frozen}]
:fields [project0/value]
:field-type :frozen
:projections [{:type :projection
:expr {:type :field
:field r0/value}
:flatten false
:alias [project0/value]}]
:opts {:type :project-opts}})))
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))))
#?@(:cljs [] :default [
clojure.java.shell/sh [t/Any :*
;would be nice (combine :* and kw args)
; t/Str :*
;& :optional {:in t/Any ;; any valid input to clojure.java.io/copy
; :inc-enc t/Str :out-env (t/U ':bytes t/Str)
; :env (t/U (Array t/Str) (t/Map t/Any t/Any))
; :dir (t/U t/Str java.io.File)}
:-> '{:exit t/Str
:out (t/U (Array byte) t/Str)
:err t/Str}]
clojure.java.browse/browse-url [t/Any :-> t/Any]
clojure.java.io/delete-file [clojure.java.io/Coercions (t/? t/Any) :-> t/Any]
clojure.java.io/make-parents [(t/+ clojure.java.io/Coercions) :-> t/Any]
clojure.java.io/file [(t/+ clojure.java.io/Coercions) :-> java.io.File]
clojure.java.io/as-relative-path [clojure.java.io/Coercions :-> t/Str]
;; TODO second arg is flattened IOFactoryOpts
clojure.java.io/reader [clojure.java.io/IOFactory :-> java.io.BufferedReader]
;; TODO second arg is flattened IOFactoryOpts
clojure.java.io/writer [clojure.java.io/IOFactory :-> java.io.BufferedWriter]
clojure.java.io/resource [t/Str (t/? ClassLoader) :-> (t/Nilable java.net.URL)]
clojure.stacktrace/e [:-> t/Any]
clojure.stacktrace/print-cause-trace [Throwable :-> t/Any]
clojure.stacktrace/print-stack-trace [Throwable :-> t/Any]
clojure.stacktrace/print-throwable [Throwable :-> t/Any]
clojure.stacktrace/root-cause [Throwable :-> Throwable]
;; FIXME keyword arguments
clojure.reflect/reflect [(t/+ t/Any) :-> (t/Map t/Any t/Any)]
clojure.inspector/atom? [t/Any :-> t/Bool]
clojure.inspector/collection-tag [t/Any :-> t/Keyword]
clojure.inspector/tree-model [t/Any :-> t/Any]
clojure.inspector/old-table-model [t/AnySeqable :-> t/Any]
clojure.inspector/inspect [t/Any :-> javax.swing.JFrame]
clojure.inspector/inspect-tree [t/Any :-> javax.swing.JFrame]
clojure.inspector/inspect-table [t/AnySeqable :-> javax.swing.JFrame]
])
cc/flatten [t/AnySeqable :-> (t/Seq t/Any)]
scicloj/clj-djl
(ns clj-djl.ndarray.ndarray-shape-manipulation-test
(:require [clojure.test :refer :all]
[clj-djl.ndarray :as nd]
[clj-djl.utils :refer :all]
[clojure.core.matrix :as matrix])
(:import [ai.djl.ndarray.types DataType]
[java.nio FloatBuffer]))
(deftest flatten-test
(with-open [ndm (nd/new-base-manager)]
(let [array (nd/create ndm (float-array [1. 2. 3. 4.]))
result (nd/create ndm (float-array [1. 2. 3. 4.]))]
(is (= (nd/flatten array) result)))
(let [array (nd/create ndm [1. 2. 3. 4.])
result (nd/create ndm [1. 2. 3. 4.])]
(is (= (nd/flatten array) result)))
;; multi-dim
(let [array (nd/create ndm (float-array [1. 2. 3. 4.]) (nd/shape [2 2]))
result (nd/create ndm (float-array [1. 2. 3. 4.]))]
(is (= (nd/flatten array) result)))
(let [array (nd/create ndm [1. 2. 3. 4.] [2 2])
result (nd/create ndm [1. 2. 3. 4.])]
(is (= (nd/flatten array) result)))
;; scalar
(let [array (nd/create ndm (float 5.))
result (nd/create ndm (float-array [5.]))]
(is (= (nd/flatten array ) result)))
(let [array (nd/create ndm 5.)
result (nd/create ndm [5.])]
(is (= (nd/flatten array ) result)))
;; zero-dim
(let [array (nd/create ndm (nd/shape [2 0]))
result (nd/create ndm (nd/shape [0]))]
(is (= (nd/flatten array) result)))
(let [array0 (nd/create ndm (nd/shape [2 0]))
array1 (nd/create ndm (nd/shape [3 0]))
array2 (nd/create ndm (nd/shape [0 3]))
result (nd/create ndm (nd/shape [0]))]
(is (= (nd/flatten array0)
(nd/flatten array1)
(nd/flatten array2)
result)))))
(deftest reshape-test
(with-open [ndm (nd/new-base-manager)]
(let [array (nd/create ndm (float-array [1. 2. 3. 4. 5. 6.]))
result (-> (nd/create ndm (float-array [1. 2. 3. 4. 5. 6.]))
(nd/reshape [2 1 1 3]))]
(is (= (nd/reshape array [2 1 1 3]) result))
(is (= (nd/reshape array [-1 1 1 3]) result)))
(let [array (nd/create ndm [1. 2. 3. 4.])
result (nd/create ndm [1. 2. 3. 4.])]
(is (= (nd/flatten array) result)))
;; multi-dim
(let [array (nd/create ndm (float-array [1. 2. 3. 4. 5. 6.]) (nd/shape [3 2]))
result (nd/create ndm (float-array [1. 2. 3. 4. 5. 6.]) (nd/shape [2 3]))]
(is (= (nd/reshape array [2 3]) result))
(is (= (nd/reshape array [2 -1]) result))
(is (= (nd/reshape array [-1 3]) result)))
(let [array (nd/create ndm [1. 2. 3. 4. 5. 6.] [3 2])
result (nd/create ndm [1. 2. 3. 4. 5. 6.] [2 3])]
(is (= (nd/reshape array [2 3]) result))
(is (= (nd/reshape array [2 -1]) result))
(is (= (nd/reshape array [-1 3]) result)))
;; scalar
(let [array (nd/create ndm (float 5.))
result (nd/create ndm (float-array [5.]))]
(is (= (nd/reshape array [1]) result)))
(let [array (nd/create ndm 5.)
result (nd/create ndm [5.])]
(is (= (nd/reshape array [1]) result)))
;; zero-dim
(let [array (nd/create ndm (nd/shape [1 0]))
result (nd/create ndm (nd/shape [2 3 0 1]))]
(is (= (nd/reshape array [2 3 0 1]) result)))
(let [array0 (nd/create ndm (nd/shape [2 0]))
array1 (nd/create ndm (nd/shape [3 0]))
array2 (nd/create ndm (nd/shape [0 3]))
result (nd/create ndm (nd/shape [2 3 0 1]))]
(is (= (nd/reshape array0 [2 3 0 1])
(nd/reshape array1 [2 3 0 1])
(nd/reshape array2 [2 3 0 1])
result)))))
(comment
(def ndm (nd/new-base-manager))
(nd/flatten (nd/create ndm (nd/shape [0 2])))
(nd/flatten (nd/create ndm [0]))
(def array (nd/reshape (nd/arange ndm 6.) [2 3]))
array
(nd/split array [2])
)
findmyway/plotly-clj
;; @@
(ns bubble
(:require [clojure.core.matrix :as m]
[clojure.core.matrix.dataset :as d]
[clojure.core.matrix.random :as rnd]
[clojure.data.csv :as csv])
(:use [plotly-clj.core] :reload-all))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
(let [colors (flatten (repeat 3 ["rgb(205, 12, 24)" "rgb(22, 96, 167)"]))
line-types ["dashdot" "dashdot" "dash" "dash" "dot" "dot"]
cols (map #(d/column-name data %) (range 1 7))]
(-> (plotly data)
(plot-seq
(for [[col color line-type] (map vector cols colors line-types)]
#(add-scatter %
:x :month
:y col
:name (str col)
:line {:color color :width 4 :dash line-type})))
(plot "Average-High-and-Low-Temperatures-in-New-York" :fileopt "overwrite")
embed-url))
;; @@
;; =>
;;; {"type":"html","content":"<iframe height=\"600\" src=\"//plot.ly/~findmyway/46.embed\" width=\"800\"></iframe>","value":"pr'ed value"}
;; <=