This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-08-16
Channels
- # announcements (1)
- # beginners (83)
- # boot (10)
- # cider (23)
- # cljdoc (24)
- # cljs-dev (3)
- # clojure (138)
- # clojure-finland (1)
- # clojure-italy (12)
- # clojure-losangeles (2)
- # clojure-mexico (1)
- # clojure-nl (18)
- # clojure-russia (23)
- # clojure-sg (1)
- # clojure-spec (15)
- # clojure-uk (126)
- # clojurescript (94)
- # cloverage (2)
- # core-async (1)
- # cursive (98)
- # datomic (54)
- # figwheel-main (19)
- # hyperfiddle (21)
- # jobs (1)
- # jobs-rus (4)
- # leiningen (4)
- # liberator (4)
- # off-topic (15)
- # parinfer (9)
- # re-frame (23)
- # reagent (19)
- # reitit (2)
- # ring-swagger (3)
- # rum (3)
- # shadow-cljs (244)
- # sql (4)
Hey guys i am a student trying to learn FP programming and Clojure. I want to implement a game so i was wondering what library/framework is best to learn? I figure using libraries from the JVM would be difficult to use functionally if i don't have much experience.
you might want to check out https://github.com/oakes/play-cljs he has a clojure version as well. (play-clj - but he's focusing more on play-cljs).
@danial.javady clojure tends to use more of a mix and match approach than other languages, ie there is no one library/framework you should learn, but there are hundreds of useful tools to do specific tasks. Generally there is no problem in adding as many of those as you need. Hit us up with specifics and we can suggest things. You may be better off with clojurescript for game programming though, and lein-figwheel setup comes with a link to a flappy bird demo that might be of interest to you: https://github.com/bhauman/lein-figwheel.
thanks alot guys! I was a bit hesitant on diving right in because I know just the basics about front-end dev. Would it help to be proficient in JS, HTML, and CSS? I really hate JS... Right now i want to get an idea of how to develop functional applications properly.
@danial.javady you can check out #clojure-gamedev as well, though it's pretty quiet. I've had fun using play-clj for what it's worth.
thank you, play-clj seems like a nice way to avoid the browser ^^
Thats a no on the js. It wouldn't hurt to know it, but you don't need it, and in the early stages of learning functional programming you are better of sticking to languages that don't let you cheat and use mutable things...
Should qualify that, "don't make it easy for you to use mutable things". Clojure(script) does allow it for the situations where it is really necessary.
Hello, i’ve just hit this:
(let [f (constantly "foo")]
(defrecord A []
Object (toString [x] (f x))))
;=> CompilerException java.lang.RuntimeException: Unable to resolve symbol: f
i know i can do a def instead of the let block but it puzzles me a little.
any thoughts?defrecord doesn't use closed over values - it's an intentional design decision to prevent usage of the "functional closure as object" idiom from scheme / common lisp
we have actual objects, using a closure as if it's an object is almost always the wrong thing in clojure
I have a question, it has little to do with clojure other than that's how I'll be implementing it, but how do I update a single line in a (potentially but not always) gigantic file efficiently
It'd be possible to collect all the lines before and after, apply some change to the line I need, and put all of them back together again to write to a file
but the file size is potentially greater than the amount of ram the program will have access to
forgive the trolling, but if you find yourself needing to change files like that it might be that you’re not using the right tool
if the line you need to change happens to be near or at the end of the file it'll need to fit in memory anyway right
So here's the deal, I'm trying to work on a personal project where I'm creating a super-super-super primitive file-based edn database (I know this is bad I'd never do it in practice) because I want to play around with creating a query language within clojure
I just want to be able to explore the creation of a query language without working in terms given to me by some other database
That's the idea, but I really am not focusing on the storage part, frankly I'm not smart/educated enough to design that well
they key, as you just mentioned, is to split stuff up in some way so that there are many small files instead of one huge
I'll probably just make a disgusting filesystem db that's like root/db-name/table-name/record0...record9999
doing this in a smart and efficient way entails reading a lot of books about the subject 😛
there's probably some really clever way to partition files and keep them balanced but i dont know enough about this stuff to even think of where to start
I tried to look into implementing a b-tree and doing things at least halfway in the right direction
but it's way too much for me to understand or even put the time into since storage isn't really the goal here
Databases work by using fixed size records, mostly. For variable length stuff they break it up into fixed size blocks. If you're dealing with a "line" in a text file, you have no real way to do this. Best option is to write the changes to a temp file and then move the temp file to replace the original.
How can I write this in Clojure?
final ComparatorOptions comparatorOptions = new ComparatorOptions();
final Comparator myComparator = new Comparator(comparatorOptions) {
public String name() {
return "my comparator";
}
public int compare(final Slice s1, final Slice s2) {
return s1.data().length - s2.data().length;
}
};
I’ve tried using proxy…getting an error CompilerException java.lang.UnsupportedOperationException: name,
(defn comparator [options]
(proxy [Comparator] [options]
(name [] "foo")
(compare [^Slice a ^Slice b] -1)))
In our project, we don’t write comparators directly. Instead, we write regular Clojure two-arity predicates (ie, functions that take two parameters and return truthy or falsey) and then use the clojure.core/comparator
to generate the Java comparator. https://clojuredocs.org/clojure.core/comparator
Was there a reason you went with that instead of the native comparator?
Sorry, I read your post too quickly. I didn’t realize you were talking about a RocksDB-specific thing
If you’re talking about using clojure.core/comparator
instead of using proxy
to build a comparator, you end up with same thing and clojure.core/comparator
is a lot easier 🙂
Np. Good suggestion…looking like I may have to do battle with this abstract class though 😞
I was curious myself and found this example: https://stackoverflow.com/questions/22556278/extending-an-java-abstract-class-with-clojure-java-lang-classloader
Comparator is RocksDB comparator which is an abstract class: https://github.com/facebook/rocksdb/blob/master/java/src/main/java/org/rocksdb/Comparator.java
Oh, my bad. I read your first message too quickly
I didn’t realize you were talking about a different comparator 🙂
If anyone cares, I did it!
(interpret-operation [:insert test-db :people {:name "alice" :age 18}])
(interpret-operation [:insert test-db :people {:name "pants" :age 40}])
(interpret-operation [:query test-db {:table :people :select :all}])
==> ({:name "alice", :age 18} {:name "pants", :age 40})
(interpret-operation [:query test-db {:table :people
:conditions {:age #(> % 20)}
:select :one}])
==> {:name "pants", :age 40}
@alice if you’re interested in a Clojure/EDN query language, check out https://github.com/halgari/odin
Yeah, it’s pretty cool. I’ve used it for some toy projects and I’ve hoping someone would fork it and keep running with it
In my experience, the kind of thing you did gets the job done without all the extra complicated stuff 🙂
I don't forsee myself needing anything like this in the future tbh, there's a lot of stuff that does this kinda thing better that's already been proven effective in the industry
Definitely!
Looks like this patch would make my above question easier: https://dev.clojure.org/jira/browse/CLJ-1255. Is the only alternative AOT and gen-class?
I’ve read another option that avoids AOT is writing the implementation in Java then calling from Clojure
Hi all! I'm fairly new to Clojure, I was generally curious if there's a preferred Clojure style in terms of defining things recursively on your own, vs using loop
and recur
. So, the code below implements the same function twice -- count the number of bits that are equal to one in the binary representation of that number. Is there one answer that's preferred? Or is a personal preference at this point? Thanks!
`
(defn count-bits
"Write a function that takes an (unsigned) integer as input, and returns
the number of bits that are equal to one in the binary representation of
that number."
[n]
(cond
(= n 0) 0
(= (bit-and n 1) 0) (count-bits (bit-shift-right n 1))
:else (inc (count-bits (bit-shift-right n 1)))))
vs
;;Similar solution with use of loop - seen in slns:
;; (defn count-bits-loop
;; [n]
;; (loop [i n
;; c 0]
;; (if (zero? i) c
;; (recur (bit-shift-right n 1) (+ c (bit-and i 1))))))
@marlenefdez Clojure doesn't detect implicit tail-recursion. IIRC it's an issue with the way the JVM works, it's just not possible on it
So if you define it yourself, you are running the risk of blowing the stack if the recursion goes too deep
loop-recur is considered pretty low-level (but performant). If you can do it with sequence operations, do that.
With loop
and recur
, you are explicitly telling Clojure that you are doing tail recusion, and it will moan and refuse to compile if it's not actually tail recursion, and will make sure to not blow the stack
But often I see people coming from other languages using loop a lot more than they should/could, because you can often use map
and reduce
and such
@ghadi yes, I noticed that, but thought that would be too much "cheating" if the whole purpose of that tiny function was practice 🙂
@carr0t thank you! I'll try to stick with loop
and recur
@marlenefdez you need unsigned-bit-shift-right
(defn bit-count [n]
(->> n
(iterate #(unsigned-bit-shift-right % 1)) ;; sequence of shifts
(map #(bit-and % 1)) ;; select only last bit
(take 64)
(reduce + 0))) ;; sum them up
@marlenefdez that's a sequence-oriented approach:thinking_face: thank you, @ghadi I wouldn't have thought to solve it that way, I need to learn more about "sequence oriented approach"
it definitely is, thank you!
i'm also generally new to clojure, so it's a mind shift in general 🙂
Also, note that in your case, the recur is in tail position so you could just do:
Which is pretty idiomatic and also should be as fast as the loop / recur
awesome, thanks, @jsa-aerial
Looking for a combination of partition-by
and filter
or a take-while
that doesn’t terminate but instead drops values and returns a new collection with the next sequence of vals that satisfy pred, example:
(let [pred #(or (>= 5 % 1) (>= 10 % 8))
nums (range 11)]
(into [] (partition-by pred) nums)
;; => [[0] [1 2 3 4 5] [6 7] [8 9 10]]
;; desired => [[1 2 3 4 5] [8 9 10]]
)
how would that predicate give you those groups for that input?
throw away everything where it returned false, and collect the ones that were consecutive?
my first instinct is (fn group-consecutive [pred? coll] (when (seq coll) (lazy-seq (cons (take-while (pred? coll)) (group-consecutive (drop-while (complement pred?) coll)))
- it's not quite right (might end up with nils in the output you don't want) but is probably close to something that works
@denik with specter that's:
(select (continuous-subseqs #(or (>= 5 % 1) (>= 10 % 8))) (range 11))
;; => [[1 2 3 4 5] [8 9 10]]
is there a decent pattern for creating graphs of functions? For example I want to start at an input, have a few transforms along the way, and end at an output. Each function passes a context object to the next function. The graph can split or merge too, so each stage could have multiple outputs or inputs.
Multiple inputs for a single function? Lots of "if apply this transformation only if these conditions match"?
sounds like the chain of responsibility pattern / interceptor pattern to me. check out pedestal or re_frame
reminded me of interceptors too. Though I don't think you can fork and join chains in the same interceptor chain.
yeah specifically im thinking of approximating pipes and filters in a functional way instead of object-oriented. I'll take a look at interceptor or cor
what would be the condition that decides whether an output goes to one or more different functions? Some list of predicates provided with the graph to whatever the run-fn is?
the graph would be defined by json or something like that, or even hardcoded, so it's like in -> fn1 -> [fn2, fn3] => fn4 -> out. so maybe the functions just take an input and return the result that is of the same time but transformed, and then the next function takes it from there
but there would be some metadata that different functions respond to...like maybe one responds to unicode characters, the other responds to ascii, etc.
But I'm not sure whether you'd want to store those branches [fn2, fn3] directly in the graph or as an external parameter you pass into whatever traversal function
The fns could all be top level. You just provide it a domain specific AST and fns get dereferenced from some op-map as necessary. Then your branching and joining is dictated by some idiom in your AST.
Onyx does this at scale. I wrote a simple library around a similar concept: https://github.com/jonahbenton/flower The idea of data driven control flow I think is very powerful.
Yeah, essentially, if instead of def'ing all your fns in a namespace, you def them as vals on a map, then you can pretty much treat it like an immutable map-fn-ns that you can pass around. There's a lot of interesting ways you can take that.
This discussion reminds me of Prismatic Graph, from what little I remember about it. http://plumatic.github.io/prismatics-graph-at-strange-loop
Yeah, flower looks pretty sweet. I like that "fns return either maps or booleans" idiom. Because maps are functions of their keys, you can probably construct any given logic with just map applications
and because clojure maps share structure underneath, you can create virtual execution graphs that could never fit in memory.
@U04VDQDDY yes this is also a good resource.
Yeah, graph is this idea on steroids.