Fork me on GitHub
#beginners
<
2021-02-05
>
suren06:02:44

Is there a way to get the function and/or its namespace from within the clojurescript function? Something like

(defn a-function []
  (.log js/console <namespace>.<function-name>))

Mno08:02:14

should work?

Adrian Imanuel13:02:05

Hi guys, as stupid as it looks, but i literally clueless how to install https://github.com/clojure/data.csv Can i just copy & paste csv.clj into my project "src" folder? if yes, then each time i create a project, i need to paste csv.clj into my src project, am i correct?

javahippie13:02:42

How did you set up your project? With leiningen or deps.edn, maybe?

Adrian Imanuel13:02:02

with leinengen

lein new app dummyProject

Stuart13:02:14

edit your project.clj to add these lines for data.csv:

(defproject reference "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url ""}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [org.clojure/data.csv "1.0.0"]]
  :main ^:skip-aot reference.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})
See the dependencies section ^^

Stuart13:02:28

then you will want to reference it in your .clj file

Stuart13:02:04

e.g. something like this

(ns foo.core
    (:require [clojure.data.csv :as csv]))
You can now reference it in your code using the csv alias.

Stuart13:02:31

I think you will have to restart your REPL after adding it as a dependency.

Adrian Imanuel13:02:37

oh okay @U013YN3T4DA , i'll try it... thanks for the step by step. so for each project i made, i need to add these lines into my project.clj, CMIIW?

(defproject reference "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url ""
  :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
            :url ""}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [org.clojure/data.csv "1.0.0"]]
  :main ^:skip-aot reference.core
  :target-path "target/%s"
  :profiles {:uberjar {:aot :all}})

Stuart13:02:27

The important part is here:

:dependencies [[org.clojure/data.csv "1.0.0"]]
This is where you specify the dependencies you want to use, so in your case the data.csv. If you know C#/F# its similar to adding nuget references

Stuart13:02:10

If you are using deps.edn rather than project.clj, it's different. To find out what the dependency you need to add usually you go to the github page of the project: SO in this case you are using leinengen, so you want the leinengen dependency information:

Stuart13:02:17

That is the string you want to add to your project.clj

Stuart13:02:21

under dependencies

Stuart13:02:39

For lein, it will be in that format. A vector with 2 items, the first being the dependency and the second being the version.

Adrian Imanuel13:02:43

AHHHH GOTCHA! Thanks a lot @U013YN3T4DA!! πŸ˜…πŸ™

afry14:02:59

Happy Friday fellow new Clojurians πŸ’ͺ I'm streaming my morning warmup session again this morning if you'd like to join me for a quick 30-minute code challenge! Challenge should start around 9:45-ish EST. Stream located here: https://www.twitch.tv/a_fry_ We're trying to solve https://purelyfunctional.tv/issues/purelyfunctional-tv-newsletter-412-module-depth-is-bogus/ using https://clojure.org/guides/spec. Spec has been an interesting beast so far. I've been poking at it trying to make sense of it for a few days now, but I think I'm about to have a breakthrough. Not only with spec, but also with https://clojuredocs.org/clojure.core/quote, which is yet another powerful tool that I kind of understand in theory, but not in practice. Anyway, we'd be glad to have you along πŸ™‚

Fernando de Paula Carvalho15:02:02

Hello guys. Anyone here knows how to convert a vector to a map in clojure? I tried to use "into {}" and "conj {}" but it didn't work the way I want.

Tried: (conj {} [:a "aaa" :c "ccc"])
But returned only => {:a "aaa"}

alexmiller15:02:59

(apply hash-map [:a "aaa" :c "ccc"])

Fernando de Paula Carvalho16:02:26

Thanks!! That's exactly what I need!!! Solved my problem!!! Have a nice day!

Adrian Imanuel16:02:07

aren't we gonna celebrate, #beginners member has reached 12k

confusedparrot 21
nice 6
πŸŽ‰ 3
Oliver Lieu16:02:37

Hello, everybody.

πŸ‘‹ 15
popeye17:02:56

Team , I have below atom object like this

{<input> #objectcom.dinesh.AbstactModel 0xe316971 <AbstactCom   
{:a "a" :b "b"} |  
["a", "b", "c"] >]}

popeye17:02:33

I want to empty the content of model

popeye17:02:38

like

{<input> #objectcom.dinesh.AbstactModel 0xe316971 <AbstactCom   
{} |   >]}

popeye17:02:44

how can I achieve this

dpsutton17:02:04

atom's have two principal functions to interact with them. reset! which takes a value and makes the contents of the atom that value, and swap! which takes a function which takes the current value of the atom and makes the atom's value the result of (f value-of-atom). I'm not sure what your data is, but if you know how to transform your data with some function f, then just (swap the-atom f) will do whatever you want. The point is that the fact that its an atom should be basically irrelevant with those two functions. If you know how to transform your data, figure that part out and then call (swap! the-atom my-function) or (reset! the-atom some-value)

dpsutton17:02:24

atom's are kinda meant to be used with immutable data. if a com.dinesh.AbstractModel is mutable you might not need the atom around it

dpsutton17:02:58

classic example, a mutable java hashmap

~/p/clojure ❯❯❯ clj
Clojure 1.10.2
user=> (def x (java.util.HashMap.))
#'user/x
user=> x
{}
user=> (.put x 1 2)
nil
user=> x
{1 2}
user=>

noisesmith17:02:23

@popeyepwr an atom is only a container, what is an AbstactModel?

popeye17:02:38

it is the object returned from java methd

noisesmith17:02:58

why is it in an atom?

noisesmith17:02:24

anyway, "object returned from a java method" describes every value possible in clojure

noisesmith17:02:39

the real answer lies in understanding the object you are trying to use

popeye19:02:27

I am calling java method on atom object

(println " ---1---" (swap! atom-name (.removeAll Abstractclas)))

popeye19:02:39

but it is not printing anything

popeye19:02:54

Am I wrong anything here?

dpsutton19:02:01

swap expects a function. try (swap! sstore #(.removeAl %))

popeye19:02:21

still it is not printing

popeye19:02:02

atleast it should return me error... it also not returning

popeye20:02:06

How can I call java function in

dpsutton20:02:53

ah right. if .removeAll returns nil that's what the value of your atom will be set to. atom's aren't a great fit for mutable containers. if you really want to keep using them you could do (swap! store #(doto % (.removeAll))

dpsutton20:02:59

~/p/clojure ❯❯❯ clj
Clojure 1.10.2
user=> (def x (atom (java.util.HashMap.)))
#'user/x
user=> (swap! x #(.put % 1 2))
nil
user=> @x
nil
user=> (def x (atom (java.util.HashMap.)))
#'user/x
user=> (swap! x #(doto % (.put 1 2)))
{1 2}
user=> @x
{1 2}
user=>

dpsutton20:02:23

put mutates the hashmap but returns nil because mutation. i imagine your removeAll does the same

noisesmith20:02:47

in the general case it's not safe to put mutable things inside atoms, because atoms retry operations which means something side effecting can be done multiple times

andy.fingerhut00:02:55

If the mutating Java methods called via swap! are not synchronized in some other way, they can run concurrently if multiple concurrent swap! calls are made, and they will garble the mutable state. The atom is not helping anything

blak3mill3r01:02:06

it's not only pointless, it sort of suggests that things are okay, at least at a cursory glance

noisesmith20:02:12

if all you are doing is clearing state that should be OK, but it's generally a bad design choice

popeye20:02:37

when I call the method I am getting :message No matching field found: remove for class java.lang.Class

popeye20:02:56

where as I am callinh (.remove abstract-class)

popeye20:02:25

why it is pointing to java,lang.Class ?

noisesmith20:02:49

perhaps you forgot to supply the object

noisesmith20:02:57

abstract-class is an instance of class class

noisesmith20:02:41

you probably wanted what you had in the original: #(.removaAll % AbstractClass) - the example @dpsutton provided left out the arg

noisesmith20:02:16

(swap! a f x) is the same as (swap! a #(f % x))

noisesmith20:02:33

the middle arg becomes implicit, but you need it to become explicit again to call a method

Christian20:02:29

This is more of a theoretical question. I often stumble upon operational vs. denotational, when I read articles about FP (and clojure). So maybe someone with a better understanding can tell me, if I am on the correct path. Clojure is based on small step structural operational semantics. That means I can get the meaning of a programm by looking at all the underlying functions and the sum of its parts makes the thing a thing. Haskel is denotational (?) and that means, that I get a function and a function has a meaning by itself. "make fibonacci number x" is just that, no looking at the smaller parts. I don't really get the difference? When I write a procedural python programm, I can get the meaning as well, when I look at the tiniest parts of it. is that an operational semantic now? Or is this more based on the inner structure of the program, as python is line by line and clojure is a list of list of list of list? What is Haskel then? just a word, don't look further?

noisesmith20:02:25

@christiaaan if that is true, the domain of a python program is always python's internal state machinery

noisesmith20:02:56

in haskell, you can specify some new semantics (usually something close to how math would represent a problem, or attempting to be that)

noisesmith20:02:39

you can't do that in python - there's no context or use case where the meaning of the program is constituted by anything but the rules of the language itself - there's no condition that allow an abstraction that doesn't leak

noisesmith20:02:41

@christiaaan perhaps to make it even more clear: if you look at your CPU's machine operations as transitions of a state machine, all programs that are executable are "pure"

noisesmith20:02:06

the problem is that you can't write any program in an assembler that ignores that state machine

noisesmith20:02:08

if haskell were 100% pure, you actually could confidently ignore how it was implemented while implementing your own code (in practice there's plenty of gotchas, but it's less leaky than other languages)

Christian20:02:51

Does that point to overflows and such things?

noisesmith20:02:56

in clojure we can have a Long and a Double as two distinct things

noisesmith20:02:10

in machine code there's no such thing - there's memory and it goes in a register

noisesmith20:02:29

(the opcode using that register treats that memory as a Long or Double or whatever)

noisesmith20:02:46

so in assembly you deal with misaligned data types, overflows, the fact that any given function call might totally break your data stack or dereference invalid data locations

noisesmith20:02:39

each language that's actually worth using provides some level of simplification over the model the machine implements, and promises that the model won't randomly stop working

Christian20:02:34

So you are saying there is a limit to being pure. I don't get the connection to the semantics here

noisesmith20:02:37

and by extension, some languages provide a simpler model to build on than others

noisesmith20:02:17

I'm saying that "purity" is a continuum, and that haskell's main goal as a language is to maximize purity

noisesmith20:02:33

(sorry everyone, I think this has gone way off topic)

πŸ‘ 3
caumond21:02:25

Interesting that said

Christian20:02:36

so some people call it academic

Christian20:02:05

It helps me a lot to think about all the stuff I read at wikipedia, but I seme to arrive nowhere with it.

andy.fingerhut00:02:06

I am pretty sure that for most programming languages, it is possible to define either or both of an operational or denotational semantics. Strachey in the 1960s devised one of the first denotational semantics, and he did it for an imperative language (something like Algol of the time, but it could have been C).

πŸ‘€ 3
andy.fingerhut00:02:34

I have the rough idea that you have to work a bit harder to define a denotational semantics for mutable languages, but I'm no authority on the matter.