This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-01-18
Channels
- # aleph (1)
- # announcements (2)
- # aws (4)
- # beginners (73)
- # boot (2)
- # boot-dev (3)
- # cider (6)
- # cljs-dev (40)
- # clojure (64)
- # clojure-austin (2)
- # clojure-belgium (1)
- # clojure-dev (25)
- # clojure-estonia (1)
- # clojure-europe (16)
- # clojure-italy (11)
- # clojure-nl (4)
- # clojure-spec (90)
- # clojure-sweden (2)
- # clojure-uk (105)
- # clojurescript (58)
- # core-async (10)
- # cursive (23)
- # data-science (1)
- # datascript (3)
- # datomic (14)
- # duct (11)
- # fulcro (48)
- # graphql (1)
- # hyperfiddle (3)
- # kaocha (95)
- # liberator (1)
- # lumo (6)
- # nrepl (1)
- # off-topic (14)
- # onyx (2)
- # overtone (8)
- # portkey (3)
- # re-frame (31)
- # reagent (6)
- # shadow-cljs (185)
- # sql (12)
- # tools-deps (6)
- # vim (6)
- # yada (224)
(ns people.core
(:require [people.utils :refer :all]))
(defrecord Person [name age height])
(defmulti create-person
(fn [data]
{:age (contains? data :age)
:height (contains? data :height)}))
(defmethod create-person
{:age true :height false}
[{:keys [age]}]
(->Person "Gabriel" age ""))
(defmethod create-person
{:age false :height true}
[{:keys [height]}]
(->Person "Gabriel" "" height))
(defmethod create-person
:default
[_]
(->Person "Gabriel" "" ""))
(defn create-person-2 [ & {age :age height :height size :size :or {age "" height ""}}]
(prn height)
(prn age))
Having age
and height
as empty strings seems a bit odd -- why not nil
(which means "were not provided")?
Also, when you defrecord
you get a map->Person
constructor as well which would pretty much give you create-person
without any of the multimethod stuff...
I guess I'm confused about what you're trying to achieve here... (map->Person {:name "Gabriel"})
or (map->Person {:name "Gabriel" :age 42})
or (map->Person {:name "Gabriel" :age 42 :height 168})
(assuming centimeters 🙂 )
(map->Person (merge defaults data))
where defaults
is whatever defaults you want as a hash map
It is also common to have some function wrapping your record constructor. There is nothing stopping you from writing a (defn my-person [args} ...)
that does arbitrary data mangling before calling map->Person
^^.
Stuart Sierra on the topic: https://stuartsierra.com/2015/05/17/clojure-record-constructors
Hello! What tools do clojurians prefer for static analysis (e.g. to catch arity exceptions in compile time)
seems cool, thanks
also speculative might be nice to enable in your tests or dev. it’s not static, so it needs runtime: https://github.com/borkdude/speculative
I think joker limits itself to only one namespace at a time for user built functions, so it doesn’t catch all the things. If you know more tools, let me know 🙂
This one looks promising: https://github.com/arohner/spectrum
I don;t know what that would do in combination of speculative, but it sure looks interesting
only me who gets Available Obsolete from melpa -- ?
Eastwood should be able to catch such mistakes, as well as other things. https://github.com/jonase/eastwood
I suspect one of its primary down sides that remain is warnings that are not problems, and for some of those there is no straightforward way to silence Eastwood about them for future runs.
What is the rationale behind the any?
function ( https://clojuredocs.org/clojure.core/any_q ) ? I find it very confusing. It seems like a counterpart to every?
but is is not and its name is misleading, at least for me 8(
I'm implementing a lazy sequence and at the end of the recursive function I have the following form (concat so-far digits)
. When executing the function with a large amount of data it fails with a StackOverflowError. If I replace the form with (apply conj so-far digits)
, it works fine. Do I understand correctly that concat
realizes the sequence and apply
does not?
@borkdude oh, right. Makes sense, thanks. It’s just this whole family of functions lacks some common naming convention. any?
vs. not-any?
, similar name but completely different, some
vs. every?
similar purpose, but different name style… I use clojure just intermittently and every single time need to lookup which one is the right one 8(
@nikola.kasev You may find this article helpful about concat
https://stuartsierra.com/2015/04/26/clojure-donts-concat
@bbktsk the reason some
does not end with a question mark is that it returns the first value for which the predicate holds, or nil, not a boolean
@seancorfield I'll have a look, thanks
say, I have a map of functions, where each function takes a reference to the map as its first argument - I can’t partially apply each function with the map, because .. when you partially apply the function, the map is the original unwrapped functions 😛
something like:
(defn icon [_ icon-data]
[:icon (:url icon-data)])
(defn nav [{:keys [icon]} nav-item]
(:span
(:title nav-item)
(icon (:icon nav-item))))
(defn header [{:keys [nav]} data]
[:header (:title data)
(map nav (:nav-items data))])
(defn page [{:keys [header]} data]
[:section
(header (:content data))])
(def foo {:page page
:header header
:icon icon
:nav nav})
Short of storing it in an atom, you could do:
(defn icon [_ icon-data]
[:icon (:url icon-data)])
(defn nav [{:keys [icon] :as m} nav-item]
(:span
(:title nav-item)
(icon m (:icon nav-item))))
(defn header [{:keys [nav] :as m} data]
[:header (:title data)
(map (partial nav m) (:nav-items data))])
(defn page [{:keys [header] :as m} data]
[:section
(header m (:content data))])
(def foo {:page page
:header header
:icon icon
:nav nav})
((:page foo) foo {:content {:nav-items ["blah1" "blah2"]}})
Not sure if that does what you're looking for though.Or get fancy with prismatic's fnk/graph stuff http://plumatic.github.io/prismatics-graph-at-strange-loop
thanks that's a great tool. I'm using a graph library already to resolve dependencies, this might slot in fine.
I would look at https://github.com/stuartsierra/component
Good evening, is any of you having this problem?
WARNING: sun.reflect.Reflection.getCallerClass is not supported. This will impact performance.
Its clj 10, it doesn’t happen when I use clj -m xxx
but it does happen when I do the uber jar and start it like java -cp xxxxx
@rcustodio How are you building the uberjar?
@seancorfield clj -A:depstar -m hf.depstar.uberjar target/$(NAME)-$(VERSION).jar
And you're using the same version of Java to run the uberjar?
Bing suggests that warning might be coming from log4j2...
https://stackoverflow.com/questions/53049346/is-log4j2-compatible-with-java-11 includes a possible workaround, if something in your app is using log4j2 (and other search results point to logging startup too).
(if you're running clj -m ...
and java -cp ...
in different directories, it might be picking up a different logging config?)