Back
partition (clj)
(source)function
(partition n coll)
(partition n step coll)
(partition n step pad coll)
Returns a lazy sequence of lists of n items each, at offsets step
apart. If step is not supplied, defaults to n, i.e. the partitions
do not overlap. If a pad collection is supplied, use its elements as
necessary to complete last partition upto n items. In case there are
not enough padding elements, return a partition with less than n items.
Examples
penpot/penpot
#_:clj-kondo/ignore
(ns app.common.data.macros
"Data retrieval & manipulation specific macros."
(:refer-clojure :exclude [get-in select-keys str with-open min max])
#?(:cljs (:require-macros [app.common.data.macros]))
(:require
#?(:clj [clojure.core :as c]
:cljs [cljs.core :as c])
[app.common.data :as d]
[cljs.analyzer.api :as aapi]
[cuerdas.core :as str]))
(defmacro with-open
[bindings & body]
{:pre [(vector? bindings)
(even? (count bindings))
(pos? (count bindings))]}
(reduce (fn [acc bindings]
`(let ~(vec bindings)
(try
~acc
(finally
(d/close! ~(first bindings))))))
`(do ~@body)
(reverse (partition 2 bindings))))
clojure/core.typed
(ns clojure.core.typed.test.poly-record
(:require [clojure.core.typed :as t]))
;polymorphic
(defrecord> [[x :variance :covariant]]
FooP [a :- x,
b :- Number]
Object
(toString [this] \"\"))"
[& args]
(let [vbnd (when (vector? (first args))
(first args))
args (if vbnd
(next args)
args)
[nme fields & args] args]
`(do (ann-record
~@(concat (when vbnd
[vbnd])
nme
fields))
(defrecord ~nme ~(mapv first (partition 3 fields))
~@args))))
)
jonase/eastwood
(ns testcases.unusednss3
(:require [clojure.core.protocols :as protocols]
[clojure.core.reducers :as reducers]
[clojure.data :as data]
[clojure.java.io :as io]
[clojure.reflect :as reflect]))
(extend-type String
data/EqualityPartition
(equality-partition [x] nil))
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/partition-by (t/All [x] (t/IFn [[x :-> t/Any] :-> (t/Transducer x (t/NonEmptyAVec x))]
[[x :-> t/Any] (t/Seqable x) :-> (t/ASeq (t/NonEmptyASeq x))]))
cc/partition-all (t/All [x] (t/IFn [t/Int :-> (t/Transducer x (t/NonEmptyAVec x))]
[t/Int (t/? t/Int) (t/Seqable x) :-> (t/ASeq (t/NonEmptyASeq x))]))
cc/partition (t/All [a] [t/Int (t/? t/Int) (t/? t/Int) (t/Seqable a) :-> (t/ASeq (t/NonEmptyASeq a))])
cc/partitionv (t/All [a] [t/Int (t/? t/Int) (t/? t/Int) (t/Seqable a) :-> (t/ASeq (t/NonEmptyAVec a))])
findmyway/reinforcement-learning-an-introduction
;; @@
(ns rl.chapter03.grid-world
(:require [clojure.core.matrix :as m]))
;; @@
;; =>
;;; {"type":"html","content":"<span class='clj-nil'>nil</span>","value":"nil"}
;; <=
;; @@
(let [N 5
discount 0.9
world (m/zero-matrix N N)
mesh-idx (for [i (range N) j (range N)] [i j])
actions [[-1 0] [1 0] [0 -1] [0 1]] ;; [:left :right :down :up]
prob (zipmap actions (repeat 0.25))
get-next-states (fn [idx action]
(case idx
[0 1] [4 1]
[0 3] [2 3]
(mapv + idx action)))
get-rewards (fn [idx action]
(cond
(= [0 1] idx) 10
(= [0 3] idx) 5
(some #{(mapv + idx action)} mesh-idx) 0
:else -1))
update-w1 (fn [w idx]
(apply + (map #(* (prob %)
(+ (get-rewards idx %)
(* discount (get-in w (get-next-states idx %) (get-in w idx)))))
actions)))
update-w2 (fn [w idx]
(apply max (map #(+ (get-rewards idx %)
(* discount (get-in w (get-next-states idx %) (get-in w idx))))
actions)))
iterate-fn (fn [update-f w]
(->> (map (partial update-f w) mesh-idx)
(partition (count w))
(mapv vec)))
stop? (fn [[a b]] (< (m/abs (- (m/esum a) (m/esum b))) 0.0001))
converge #(ffirst (filter stop? (partition 2 (iterate (partial iterate-fn %) world))))]
(m/pm (converge update-w1))
(m/pm (converge update-w2)))