This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-10-02
Channels
- # beginners (98)
- # bigdata (1)
- # bitcoin (1)
- # boot (32)
- # cider (20)
- # cljs-dev (57)
- # cljsrn (130)
- # clojure (93)
- # clojure-dusseldorf (1)
- # clojure-germany (1)
- # clojure-greece (3)
- # clojure-italy (2)
- # clojure-russia (203)
- # clojure-spec (14)
- # clojure-uk (50)
- # clojurescript (127)
- # css (7)
- # cursive (6)
- # data-science (1)
- # datomic (4)
- # emacs (1)
- # events (1)
- # fulcro (8)
- # funcool (12)
- # graphql (7)
- # jobs (1)
- # lein-figwheel (2)
- # luminus (2)
- # off-topic (7)
- # om (16)
- # onyx (4)
- # parinfer (17)
- # pedestal (6)
- # portkey (36)
- # proton (3)
- # re-frame (10)
- # shadow-cljs (140)
- # spacemacs (12)
- # specter (1)
- # sql (1)
- # vim (10)
- # yada (10)
@dfcarpenter Data structures are immutable. assoc
doesn't change the data structure, it returns a new version of it. You need styles to be an argument in the loop as well.
This seems like a rather Mundane topic, but does anyone have any good resources on file IO? I’m looking at the definition for
and i’m feeling that i should be more comfortable with such a fundamental topic. It seems a file can mean many things, local-file, site, etc.. and its not even clear to me what i’m handed back. It seems like a lazy list?
I have successful used this concept dozens of times but i’m wondering where people go to really wrap there heads around it.
@drewverlee in the jvm a file is a file on disk
io/reader returns a BufferedReader
+user=> ( ".bashrc")
#object[java.io.BufferedReader 0x70f59913 "java.io.BufferedReader@70f59913"]
+user=> (type *1)
java.io.BufferedReader
if you want the abstraction that could be a file, or could be data from the network, or could be input from data in memory in a byte array, etc, what you want is InputStream
@drewverlee regarding resources, on the jvm clojure IO is java IO, and it follows all the rules and conventions of the java classes, the functions in
wrap up some of the more common operations for convenience but they don't offer a novel approach beyond what the jvm offers https://docs.oracle.com/javase/tutorial/essential/io/streams.html
also you can look at the classes returned by the functions in http://clojure.java.io, they are all built into java and well documented
in the java tutorial I linked above, this is where it starts talking about files (it covers a bunch of generic io oriented stuff first, as I mentioned before on the jvm a "file" is not an abstraction or general thing, it is always a file on your filesystem) https://docs.oracle.com/javase/tutorial/essential/io/fileio.html
Thanks, a ton. that gives me a good starting place to look into stuff further!
@madstap Thanks! that was it
Hello to anyone who might be out there. I was trying to get better at Clojure (I suck at it now) by writing little functions that do simple things. I'm having some trouble with dotimes
in the sense of this function I created
the repl I started up says that the let statement is an unsupported binding form
Basically I'm trying to decement a number by 0.333 a certain number of times. Nothing too fancy
Ok it just returns a bunch of nils
dotimes
just evaluates the body n
times and lets you know which iteration you’re on through the binding (`i` in your example). Remember that your data is immutable, too, so this may be a better way if you do want to use dotimes
for it:
(defn dec-by-3rds [n x]
(dotimes [i n]
(println (- x (* i 0.333)))))
Oh that makes sense
doesn't dotimes
allow for side effects?
Oh I misundetstood what you said "immutable". My bad
Is my method of learning Clojure fine? I find it somewhat more useful (for me, at least) than some of the 4clojure probelms.
Only you know the answer to that. :) Experimenting with the REPL to understand how stuff works is what we all do, regardless of experience - it’s very instructive and lets you play and poke with things.
It's just I have trouble with the 4Clojure problems, and maybe that's just do to needing to understand a majority of Clojure's core functions better.
@bigdaddys1oth you might want to look at https://github.com/functional-koans/clojure-koans/tree/master/src/koans - this could give you a good overview of basic clojure stuff. Another good resource is http://exercism.io which I liked a bit more than 4clojure. Moreover, ClojureDocs (http://clojuredocs.org/) is a good site to see some examples of usage of Clojure core functions + there is "SEE ALSO" section at the bottom which references the similar functions, e.g. http://clojuredocs.org/clojure.core/dotimes Finally, there are a few good books for Clojure beginners to get started: Many people like "Clojure for the Brave and True" and "Living Clojure". As regards to Clojure standard lib, there's a book Clojure Standard Library (in progress work): https://www.manning.com/books/clojure-standard-library
@bigdaddys1oth Are you sorting the 4Clojure problems on difficulty?
Hello. I'm not sure if it has been shared here already but I'd recommend this free course for everyone starting with Clojure: http://mooc.fi/courses/2014/clojure/
I have this for getting fields from a pdf document
(defn get-fields [document]
(->> document
(.getDocumentCatalog)
(.getAcroForm)
(.getFields)))
If I wanted to be able to pass in any of those things to get the fields, what would be the clojure way to do that?
matching the type in the function?
multi-methods?you could use a multimethod or protocol - if what you are doing is directly dispatching on type, that’s exactly what protocols are for, though if you only have one operation (rather than a set of methods that go together) a multimethod can be simpler.
Does anyone know how I can get emacs cider to show test diffs?
on failures
(defn anagrams-for
[word word-list]
(filter #(apply (.contains word %) %) word-list))
(print (anagrams-for "first" ["one", "two", "strif"]))
When I run it I get this
CompilerException java.lang.ClassCastException: java.lang.Boolean cannot be cast to clojure.lang.IFn, compiling:(/home/danny/exercism/clojure/anagram/src/anagram.clj:7:1)
@dannystanny01 apply
takes a function as a first argument, but (.contains word %)
returns a boolean
so this also doesn't work, which I tried previously
(filter #(apply .contains word %) word-list))
throws
CompilerException java.lang.RuntimeException: Unable to resolve symbol: .contains in this context, compiling:(/home/danny/exercism/clojure/anagram/src/anagram.clj:5:12)
So .contains
isn't a function?(defn anagrams-for
[word word-list]
(let [letters (sort (seq word))]
(filter #(= letters (sort (seq %))) word-list)))
try using seq
instead of apply
to turn a string into a seq of chars and go from there 🙂
alright, so I'm definitely a little lost on your answer, but I ran it and it does indeed work. Still need to try and figure out my version for learnings. So I replaced apply
with seq
as you suggested (as well as the different inner anon fn)
(defn anagrams-for
[word word-list]
(filter #(seq (fn [letter] (.contains word letter)) word-list)))
Now this is returning an object in mem
#object[clojure.core$filter$fn__4808 0x14de7968 clojure.core$filter$fn__4808@14de7968]
I'm guessing because filter
returns a lazy seq and I still need to call it properly.. which I'm apparently not doing?example: (.contains "asdf" "a")
works, so now you want to do that for a sequence of chars, so you need some kind of loop
and then you want to check if they are all true, so you turn to http://clojuredocs.org and search for the appropriate function 😄
yea, i just have a hard time navigating as a total noob. verrrry different from my previous languages
then started on 4clojure, but that wasn't great for learning because it was mostly code golf from other users
I remember https://aphyr.com/tags/Clojure-from-the-ground-up being helpful when I started out
that is, try each piece separately at the REPL and make sure it does what you think before putting it together with something else
to continue on my example above:
user=> (every? true? (map #(.contains "asdf" (str %)) (seq "asdx")))
false
so if you turn that into a function, you have something that checks if each char of a string is in another char
sorry if my examples kinda suck, it’s really hard to just hint at a solution without giving it away completely 😄
No worries, so as a quick question, so far in your examples you've chosen map
over filter
, any reason for that in this situation? Or just so as not to give me an answer 🙂
user=> (map #(.contains "asdf" (str %)) (seq "asdx"))
(true true true false)
user=> (filter #(.contains "asdf" (str %)) (seq "asdx"))
(\a \s \d)
they way I think about it is “is the character in the string, yes or no?“, therefore I want map
you could do another variant with filter
if you count th chars in the word, and in the filtered word. if they are anagrams, the should be the same
so my thought process is like the outside in, filter on the collection being fed to the function
with the inner contents of filter applying some function to test for all letters being in the word
Since you're working with side-effects, you don't really want map
at all, you want doseq
-- and that will make it easier to work on parts of the file.
(doseq [line (line-seq reader)] (do-filter line))
instead of your (doall (map ...))
-- so now you can do take
/`drop` on the (line-seq reader)
part easily enough
map
is if you want to transform a sequence -- and use the result of that transformation. doseq
is for when you want to process a sequence for side-effects (and have no returned value).
@seancorfield thank you!