This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-09-26
Channels
- # arachne (4)
- # beginners (70)
- # bigdata (1)
- # boot (373)
- # braid-chat (3)
- # cider (4)
- # cljs-dev (10)
- # cljsjs (6)
- # cljsrn (27)
- # clojars (11)
- # clojure (114)
- # clojure-austria (1)
- # clojure-czech (2)
- # clojure-dusseldorf (2)
- # clojure-greece (7)
- # clojure-italy (2)
- # clojure-nl (6)
- # clojure-russia (15)
- # clojure-serbia (11)
- # clojure-spec (92)
- # clojure-uk (5)
- # clojurescript (183)
- # component (9)
- # cursive (28)
- # datomic (36)
- # editors (4)
- # emacs (1)
- # garden (11)
- # hoplon (155)
- # lein-figwheel (7)
- # mount (47)
- # om (97)
- # onyx (25)
- # proton (3)
- # rdf (3)
- # re-frame (80)
- # reagent (9)
- # ring-swagger (9)
- # spacemacs (1)
- # untangled (145)
- # vim (2)
i don’t totally understand the use case for clojure protocols. they seem sort of like traditional OO protocols, but without class instances what exactly implements them?
@liamd Records, defined by defrecord
can implement them. You can also extend the protocol to include any types you want.
protocols are actually faster, but require more code to implement the behavior compared to multimethods
Another difference between protocols and mulitmethods is that protocols only offer type based dispatch on the first/primary arg
I agree, when you feel like you need to do inheritance, always double check your design
yeah i’m writing a game and while i’ve gotten my head out of OO space for web services it’s tougher in this domain
It's easy to get trapped in mimicking OO using protocols and inheritance. Do not do it 🙂
to make this concrete: i’m going to have a list of different kinds of units in my game state and i want to map functions over all of them that have them return their new state
i kinda started sketching this out:
(defprotocol Entity
"An entity is anything that can be represented on the map"
(draw [x-size y-size] "An entity must be able to draw itself given a size constraint"))
(defprotocol Controllable
"A unit is a controllable entity that the player can interact with"
(onSelected [this] "Executed when the user selects this entity")
(onInteractWith [this other] "Executed when this entity is selected and then another entity is selected")
(onTurnStart [this] "Executed on turn start")
(onTurnEnd [this] "Executed on turn end”))
sometimes, a simple cond
in your functions is the most expressive way to go about this
im starting to think having a multimethod dispatch on some “:type” in the unit records would make more sense
if the functionality does not differ that much from unit to another, I think you can get away with a cond
or case
on unit type in your functions
clojure print system is an example of a mm with extensions made by different namespaces:
If you are not careful, it is easy to create a spaghetti mess of MM's in your code base
i’m thinking what might be best is turn each of those methods into multi methods with defaults and then “override” them in unit files
i just mean if i need some new behavior it’s easy to add a new multimethod with a default in one place and all my existing “:type”’s will just default to it i guess?
then as i need to specify some behavior for a specific :type i can just add a case in that file
if you are adding new units to your game, you are extending it. But if the unit types are already defined and won't be extended regularly, that's a confined (limited) space of types.
liamd: my completely underequalified 2c: i’ve never built a game before, but i’m working on one in cljs; i don’t really ever use records or protocols/multimethods, i mainly just use functions and maps, and they’ve been working fine for me so far
code’s at https://github.com/jrheard/voke , work in progress is http://jrheard.com/voke (wasd + arrow keys), i’ve never done this before haven’t made it very far, but am linking this stuff in case it’s at all helpful
i’m using what i think is a reasonable interpretation of the entity-component-system pattern i’ve read about
@jrheard awesome! i’ll definitely check out your project. it’s great to see prior art and there’s very little in the way of clojure games
https://github.com/aaron-santos/robinson exists fwiw and seems to be waaaaayyyyy farther along / better in every way than what i’ve got
i’ve been keeping notes from the day i started the project, they’re at https://github.com/jrheard/voke/blob/master/dev-diary.txt but likely very unhelpful/scattered, lots of relevant links buried in there though
ns-resolve
also needs a ns
as parameter. this ns is what I need, but I only have its name as a string
I need an explanation, scratching my head when reading this code: https://github.com/cemerick/piggieback/blob/440b2d03f944f6418844c2fab1e0361387eed543/src/cemerick/piggieback.clj#L294 to me it looks like a fancy way how to use vars as keys in a map to store some information, the comment "ensure that bindings exist so cljs-repl can set!” is not accurate, no bindings are created AFAICS, set! can be called even without this code
I was reading http://clojure.org/reference/vars and I don’t see reason why one would use vars as keys in a map instead of namespaced keywords
also wondering if the author relies on the fact that each new invocation of wrap-cljs-repl will run on a new thread so it will get a new (independent) instances of those dynamic vars (somehow?)
I understand the purpose of the code, it works, but to me it is magic, because I don’t understand exact mechanism here
@witek when I try to call a function from a namespace, knowing namespace and function name as strings, i use this function
(defn- action
[ns-name function-name]
(require (symbol ns-name))
(binding [*ns* (symbol ns-name)]
(ns-resolve *ns* (symbol function-name))))
it takes the namespace-name and function-name as strings and returns the actual function wanted from the named namespaceI’m playing around with clojure.spec - is there not a way for me to do something like (s/keys :req-un [[:foo keyword?] [:bar string?]])
rather than
(s/def ::foo keyword?)
(s/def ::bar string?)
(s/def my-map (s/keys :req-un [::foo ::bar]))
? Otherwise I’ll end up with awfully many specs which boil down to keyword?/int?/string? and the likeStill — I then wonder - what if I need to define specs for multiple :type fields ? (or some other generic field for a map). It just seems like I’ll have to define an awful lot of separate cljs files then
@pseud Part of the design is to encourage the use of namespaced keywords more widely 🙂
But even with unqualified keywords, you can use namespaces in the specs to identify which sort of :type
you are referring to:
(s/def ::thing-a (s/keys :req-un [:a/type ::foo ::bar]))
(s/def ::thing-b (s/keys :req-un [:b/type ::bar ::quux]))
This has two separate :type
specs — :a/type
and :b/type
and shares ::bar
as a spec.Anyone know how to run lein clean on heroku?
heroku run lein clean
Depending on which dyno you want to do it, you might have to change that command a bit
robert hooke looks like advised functions in emacs, which means they look awesome and terrible
It would be really nice to have something like https://lambdaisland.com/blog/25-04-2016-union-types in core.
Does anyone know how to make a tag like <script src="demo_async.js" async></script>
using hiccup?
Usually you can simply use :async true
in your options and hiccup will do the right thing
Or you could use the xhtml variant: [:script {:async "async" :src "foo.js"}]
@hugo.corbucci, @timgilbert, both of those result in "<script async=\"async\" src=\"foo.js\"></script>"
is that valid?
pretty sure it is… most browsers will treat those key-only attributes to be “truthy” if they’re present with whichever value
oh ok, thanks
@liamd in current alpha's there is an any?
but it won't mean what you think it does. the function for that is some
some?
is "
Returns true if x is not nil, false otherwise.” so isn’t that just #(not (nil? %))
some
and some?
are different
some
is mostly what you'd want any?
to do
in the same way that sets are almost predicates for their contents
there should be a word for a function that does something except for the edge cases of false
and nil
do other lisps have those kinds of functions too?
does some
have the same edge case[s]?