Skip to content

Instantly share code, notes, and snippets.

@jgomo3
Created July 25, 2022 19:48
Show Gist options
  • Select an option

  • Save jgomo3/7989f5d22e7ab1fb4fae5d75c1853c4b to your computer and use it in GitHub Desktop.

Select an option

Save jgomo3/7989f5d22e7ab1fb4fae5d75c1853c4b to your computer and use it in GitHub Desktop.

Revisions

  1. jgomo3 created this gist Jul 25, 2022.
    50 changes: 50 additions & 0 deletions bb1.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    (ns jgomo3.playground.blackbox.bb1
    (:require [java-time :as jt]))

    (def bb-date-format "y-M-d")

    (defn en-año
    "Determina si `fecha` está en el año `año`."
    [año fecha]
    (= (jt/as fecha :year) año))

    (defn fechas-del-año
    "Devuelve todas las `fechas` que sean del año `año`."
    [año fechas]
    (filter (partial en-año año) fechas))

    (defn filtro-por-año
    "`secuencia` es una colección de cadenas que representan fechas según
    el formato `bb-date-format` y la función devolverá otra collección
    con todas las fechas que sean del año que representa la cadena
    `año`."
    [secuencia año]
    (let [decode-input-item (partial jt/local-date bb-date-format)
    decode-input-seq (partial map decode-input-item)
    encode-output-item (partial jt/format bb-date-format)
    encode-output-seq (partial map encode-output-item)
    año_ (Integer/parseInt año)
    proc-seq (partial fechas-del-año año_)]
    (->> secuencia
    decode-input-seq
    proc-seq
    encode-output-seq)))


    (comment
    (en-año 2020 (jt/local-date 2020 9 20));; => true
    (en-año 2021 (jt/local-date 2020 9 20)) ;; => false

    (fechas-del-año 2020 (map (partial apply jt/local-date) [[2020 1 4] [2019 5 7] [2021 12 11] [2020 8 9]]))
    ;; => (#object[java.time.LocalDate 0x5df741bf "2020-01-04"]
    ;; #object[java.time.LocalDate 0x70bf87c2 "2020-08-09"])

    (map (partial jt/local-date bb-date-format) ["2020-1-4", "2019-4-7", "2021-12-11", "2020-8-9"])
    ;; => (#object[java.time.LocalDate 0x1ddbe51e "2020-01-04"]
    ;; #object[java.time.LocalDate 0x3062435c "2019-04-07"]
    ;; #object[java.time.LocalDate 0xaebe50 "2021-12-11"]
    ;; #object[java.time.LocalDate 0x394e9042 "2020-08-09"])

    (filtro-por-año ["2020-1-4", "2019-4-7", "2021-12-11", "2020-8-9"] "2020")
    ;; => ("2020-1-4" "2020-8-9")
    )