Fork me on GitHub
#beginners
<
2020-05-11
>
tio03:05:14

i’m having trouble with the brave book; specifically the reduce function

krzyz06:05:25

I highly recommend ‘How to Design Programs’ for learning functional programming, particularly recursion. It’s freely available at https://htdp.org/.

tio16:05:48

Ah! Will look into it, thank you.

tio03:05:25

for example, what are these types of functions called so i can youtube videos about them?

``The symmetrize-body-parts function (starting at ➊) employs a general strategy that is common in functional programming. Given a sequence (in this case, a vector of body parts and their sizes), the function continuously splits the sequence into a head and a tail. Then it processes the head, adds it to some result, and uses recursion to continue the process with the tail.``

seancorfield03:05:38

@tiotolstoy Search for "recursive functions"

seancorfield04:05:38

(the other term to search for @tiotolstoy is "higher order functions")

Jeroen06:05:22

Ok… me thusfar… • Visual Studio Code with Calve. • Went through Clojure for the Brave. • Did all Clojure Koans. (Icheated on the later ones by looking at the solutions. • Now planning on reading/going through “Web Development with Clojure, Third Edition” by Dmitri Sotnikov and Scot Brown ( http://pragprog.com ) Or are there other recommended paths for learning?

krzyz06:05:34

Write code. Haha. I did a lot of reading myself, but nothing beats working on your own project. At some point while studying I felt like I was just going through the motions and not paying as much attention. That never happens when you are trying to solve problems that you care about. Knowledge seems to stick better when you’ve earned it with sweat and blood, at least in my experience. Also, check out other people’s code repositories, or read the source for libraries that you use. You will wonder why people do things a certain way, and it teaches you a lot.

Aviv Kotek08:05:04

what would be the easiest way to go with any `auto-complete` tool on given data? (without integrating any search engine) `f`would need to get some `string` and return some amount of relevant data (`starts_with` for example) simple start could be just to implement a "like_starts_with" SQL query with any clojure-jdbc. maybe there's some work with libraries on this? can't find much

mloughlin09:05:22

Have you considered using a Java library with interop?

Aviv Kotek09:05:13

heh good idea 🙂

Ben Sless09:05:58

Does your data fit in memory?

Ben Sless09:05:45

So why not a map? 🙃

Ben Sless09:05:00

besides implementing the trie yourself

Ben Sless09:05:21

Clojure maps have good performance characteristics

Ben Sless09:05:51

What you'd want ideally is a trie which also saves the top N children of each node for easy access

Aviv Kotek09:05:46

yea building a trie is some-work

Aviv Kotek09:05:57

it could be nice for fun but I guess i'll just move the solution outside clojure-scope

Aviv Kotek09:05:20

thinking on again a simple auto-complete just needs to fetch the data - store it in memory and use 3rd-party for that

Aviv Kotek09:05:53

Writing the question here helped me understand how to tackle the problem 🙂 thanks guys!

Ben Sless09:05:31

Aviv Kotek09:05:23

yea, data is small and won't change so basically you can load it all up once, then it's no-fun and just use some libraries. those tasks become harder when data is dynamic, then some search-engine helps out

Santiago12:05:37

how can I create an HTML string from hiccup? I would like to generate HTML and show it on a web page as part of a reagent app so that the user can copy the html and use it somewhere else

Kevin12:05:55

Hiccup has an `html` function which does what you want (I think) https://github.com/weavejester/hiccup#syntax

Kevin12:05:11

Not sure about reagent’s version though

Anyone interested in dependent types? There is “In Further Praise of Dependent Types” post on HN frontpage now that I didn’t understand at all, but the idea sort of maybe sounds interesting… It also led me to a comment mentioning Idris and how the author of Idris gave a talk about using it’s type system to define matrix transposition, and I found that video, and it’s so hard to folllow, and it’s lines and lines of type definitions for what is `#(apply mapv vector %)` in clojure, but still, all those static guarantees sound interesting, perhaps not very practical in currently existing statically typed languages…

Ben Sless13:05:16

They look interesting. Do you want to link some of the sources? I remember checking if it's possible with `spec`, came across things like https://gist.github.com/Azel4231/2d46a884fcdddafe5696b6e89d1a1695

my comment there with a link to a video of matrix transposition https://news.ycombinator.com/item?id=23139972#23141456

@UK0810AQ2 interesting gist, I wonder if there can be a spec-like static type system…

Haha, I thought that's #off-topic for some reason :)

Cameron00:05:46

ah I meant to comment on this this morning and now doubly wish I did, as I in fact have a project for a spec-like inferred static type system (with structural typing, rather than nominal typing and eventually maybe something that may amount to dependent typing); this is an area of experimentation I wish to explore

Cameron00:05:51

in my case, the extra type safety I wanted to experiment with is seeing what is possible additionally encoding pure prolog-ish rules and facts, like "x > n and n > n2 then x > n2" (sorry, its been like 6 years since I've used prolog) and then, into a type, "x > 3"

Cameron00:05:30

I really wanted to explore Shen at some point, as I could have sworn it was implied you could encode any predicate statically (I have no sort of rigorous basis to know if that's possible or what's possible here), but that sort of fizzled out, as I remember having a lot of trouble just getting a decent repl going / finding proper support for it (that being said, I was willing to try again eventually)

Cameron00:05:57

In my case, I'm still messing with a format for representing the types, right now there's a haskellian shorthand for expressing them (but they're also being stored as that shorthand) that looks like `[:-> :Int :Int :int]`, `[:=> [:Num :x] :x -> :x -> :x]`

Cameron00:05:19

(great this is long) naturally the idea is to express them underneath explicitly, so that trying to read the different 'properties' of the type would just involve, well, being able to ask for them directly, with a type looking like

``````{:typeclasses [[:Num :x]]
:type [:-> :x :x :x]} ``````
Interestingly the first example I think of, in the first time I saw a conversation covering this idea of explicit vs implicit data (a little while after I'd first really thought about) where I thought 'yes, that's it, they're thinking this exact same idea' was I believe a conversation of yours on whether to represent some part of cljfx implicitly as a vector or explicitly as a map

woah, nice!

I like specs (that are also data) for type representation 🙂

Cameron15:05:49

One area I especially want to examine is where the line draws between static typing giving you a strength of sorts at a cost of what you can express, and when its just flat out unobtrusively detecting some unambiguous contradiction in your intention (such as writing something like `(def a {:a 1 \:b 2}) (:c a)` -- not the best example, since this is something you would get an error for anyways (and one ) --> but something like that maybe where `(:c a)` is a little less direct and buried somewhere else). After all, for those who still want full dynamic expression, by definition this would be maximizing that experience 😄

Joe16:05:01

Hello folks, I’m having an issue using the `java.time` package. It is definitely running Java 8. When running `(java.time.LocalDate/of 2019 1 1)` it throws `IllegalArgumentException: No matching method of found taking 3 args`.

noisesmith16:05:34

does wrapping the longs in `(int )` help? usually clojure is good at checking for int methods when provided with longs, but it's worth a try

Joe16:05:11

:face_palm: turns out the test was passing in a keyword not an int.

noisesmith16:05:34

well that explains it (btw `1` isn't an int in clojure, it's a long)

Joe16:05:54

I thought the issue was related to the environment configuration or how it was being imported. This is the first time I’m using the Java interop functionality.

Joe16:05:11

Good to know. Thank you!

Joe16:05:24

I’ve confirmed that it works in a REPL as well as the java docs.

Jakob Durstberger17:05:52

I know naming functions with `?` when they return a boolean is idiomatic, but would you do the same for keywords like `{:active? true}` vs. `{:active true}` vs. `{:is-active true}`

kelveden18:05:26

I've seen both the first two in the wild but I don't recall seeing the last one - it smells a bit too much of Java probably.

Jakob Durstberger18:05:57

> it smells a bit too much of Java probably. Definitely does, just thought I wouldn’t leave it out

Jakob Durstberger18:05:26

@U6N4HSMFW I like the reasoning that it gets confusing when you use destructuring and therefore won’t do it. Thanks 🙂

kelveden18:05:05

That article is a good read by the way: I agree with everything in it. The distinction between a Boolean and a predicate is the key.

AJ Jaro18:05:58

I actually prefer the third option as it hints towards the type of value it’s referring to.

kelveden19:05:49

Yeah, whilst I agree with the article in its principles I wouldn't say that I always stick to those rules in practice - I do find it makes for more readable code to use keywords with `?` sometimes.

dev.4openID18:05:08

So I have a bbb.clj file with ns myproject.bbb and defn bbx in it, when I test/execute bbx it works just fine I have core.clj with ns myproject.core and I include a require [myproject.bbb as abc] I then call in core.clj (abc/bbx) and it does not execute. Currently under the impression that by referencing bbx via require [myproject.bbb as abc] it should be working. Is this correct? Odd thing was that it was seemingly working previously

andy.fingerhut18:05:12

Do you have `:as` with a colon, or `as` with no colon? The latter is incorrect.

andy.fingerhut18:05:04

If it isn't too many lines, you are welcome to copy and paste your entire `ns` form into chat, so we can see what it looks like character-for-character

dev.4openID18:05:27

yes I have :as in place

dev.4openID18:05:57

It reports it cannot find bbx when executing the call from core

dev.4openID18:05:00

[myproject.bbb :as abc]

andy.fingerhut18:05:31

Is your project published anywhere? It is difficult to know what is going wrong without more information than you have given so far.

dev.4openID18:05:04

No it is not published - I am an early learner

andy.fingerhut18:05:35

Are you willing to copy and paste the entire `(ns ...)` form in the file that contains the `[myproject.bbb :as abc]` expression?

dev.4openID18:05:21

(ns myproject.core (:gen-class) ;;(:import (org.apache.logging.log4j Level LogManager)) (:require [cheshire.core :refer :all] [clj-http.client :as client] [clojure.data.json :as djson] [http://clojure.java.io :as io] [clojure.pprint :as pprint] [clojure.spec.alpha :as spec] [clojure.string :as string] [myproject.bbb :as abc] )) (abc/dispatch_fn "6738549661") (ns myproject.bbb (:gen-class) (:require [cheshire.core :refer :all] [clj-http.client :as client] [clojure.data.json :as djson] [http://clojure.java.io :as io] [clojure.pprint :as pprint] [clojure.spec.alpha :as spec] [clojure.string :as string]) (defn dispatch_fn [myval] (do something ..........) )

dev.4openID18:05:19

I had to truncate the code

dev.4openID18:05:45

I am under impression the code calling the defn in the referred file bbb.clj should execute When I call it within bbb.clj it works fine

aarkerio18:05:15

and what is the error message?

dev.4openID18:05:56

Unhandled clojure.lang.Compiler\$CompilerException Error compiling src/myproject/core.clj at (45:1) #:clojure.error{:phase :compile-syntax-check, :line 45, :column 1, :source "/Users/xxxxxx/Google Drive/development/projects/myproject/src/myproject/core.clj"} Compiler.java: 6808 clojure.lang.Compiler/analyze Compiler.java: 6745 clojure.lang.Compiler/analyze Compiler.java: 3820 clojure.lang.Compiler\$InvokeExpr/parse Caused by java.lang.RuntimeException No such namespace: abc Util.java: 221 clojure.lang.Util/runtimeException Compiler.java: 7384 clojure.lang.Compiler/resolveIn Compiler.java: 7358 clojure.lang.Compiler/resolve Compiler.java: 7319 clojure.lang.Compiler/analyzeSymbol Compiler.java: 6768 clojure.lang.Compiler/analyze Compiler.java: 6745 clojure.lang.Compiler/analyze

noisesmith18:05:36

if you has an `:as abc` in the ns block, this error means that the namespace aliased as abc did not compile correctly

noisesmith18:05:55

dev.4openID18:05:02

Resolved, I had to get out of spacemacs and reload. Followed by updating packages etc. Restart and now it works. No change in code written. there must have been a bug in one of the packages I updated on the weekend

😁 4
👏 4
dev.4openID18:05:01

Thanks for the interest to all

Michael W18:05:20

Is there a way to get all keywords in a map that match a pattern? I have many keywords that start with :subnet_ or :pool_ like ':subnet_id :pool_id :subnet_name :pool_name', etc How can I pull out all the :subnet_* entries?

noisesmith18:05:42

you can use `filter` across `(keys m)` , inside the filter you can use `name` to get a string form a key, and `starts-with` or `re-matches` to find a given prefix

noisesmith18:05:35

``````scratch=> (filter (fn [k] (clojure.string/starts-with? (name k) "foo_")) (keys {:foo_a 0 :foo_b 1 :bar_a 2}))
(:foo_a :foo_b)``````

😂 4
dev.4openID21:05:38

ns:myproject.core ns:myproject.scope I I :require [project.scope :as s] (defn abc [] ,,,) I I (s/abc 123) ;; it works!! ;; (abc 123) ;; it works!! actually it is (abc somenumber) actually it is (abc somenumber) ns: myproject.scope.test I (deftest qwerty (abc 123)) ;; does not work Question---------- for myproject.scope.test do I require [project.scope :as s] thus: (deftest qwerty (s/abc 123)) -OR- do I require [project.scopecore :as c] thus: (deftest qwerty (c/abc 123)) -OR- both?? to make my test execute

noisesmith21:05:16

at the top - myproject.core should not be able to use abc directly

noisesmith21:05:39

and no user of myproject.core should be able to use abc as if it were in core - require is not transitive like that

dev.4openID21:05:52

OK so how do access abc. Do I use '(require.... statements? from core

noisesmith21:05:19

right, if you have `:require [myproject.scope :as s]` you should use `s/abc`

dev.4openID21:05:34

noisesmith21:05:34

and this should be done in every ns that wants to call abc directly

dev.4openID21:05:06

I updated my question to reflect that in the interim

noisesmith21:05:33

c/abc doesn't work, you can only get abc from the ns that defines it

dev.4openID21:05:08

Nevertheless, so whereever I want to access abc I need to state `:require [myproject.scope :as s]`

noisesmith21:05:33

right, you can use a different alias of course, but that's the concept

dev.4openID21:05:45

So I have to have `:require [myproject.scope :as s] in myproject.test as well`

noisesmith21:05:49

right, usually what we do is pair a test ns to a code ns, and only test one ns per test

dev.4openID21:05:35

What is interesting is that I did include `:require [myproject.scope :as s]` in myproject.scope.test as well but it does not execute - I get no such namespace

noisesmith21:05:32

that should be working, you might have some broken state that needs to be reset / restarted

dev.4openID21:05:04

I ran the "test" call in the code ns and it still works. Same with core

dev.4openID21:05:03

Not in the test, yet all have `[myproject.scope :as s]`

noisesmith22:05:09

any file that has a valid require should be able to use definitions in myproject.scope, if there's something else wrong you need to describe the specific error because I can't see what the problem is from what you've shared so far

dev.4openID22:05:06

I am learning clojure.testing so it must be my deftest is incorrect. I will have to investigate (deftest aaa ((is (= 0 (s/abc "123")))) fails to work

dev.4openID22:05:08

😁 Now it works!! I give up Thx @noisesmith

noisesmith22:05:37

you have too many parens around `is` there btw - remember that clojure doesn't have "free parens" - they change the meaning of something

noisesmith22:05:09

`(x)` in clojure is like `x()` in other programming languages - it means you want to call x

dev.4openID22:05:37

,Funny ting is (deftest azz (is (= 1 1) )) works (deftest aaa ((is (= 0 (s/myproject.scope "123")))))

noisesmith22:05:22

like I said, (x) means call x

noisesmith22:05:30

so ((x)) means call x, then call the thing it returns

dev.4openID22:05:06

yes - (s/myproject.scope "123") returns a value

noisesmith22:05:10

is returns nil, so ((is ...)) is an npe it will probably error, unless the thing you put inside is directly returns a function you can call

dev.4openID22:05:29

so it becomes (is (= - result))

noisesmith22:05:47

((is ...)) for any ... is an npe as is returns nil

noisesmith22:05:55

doesn't matter what is inside

noisesmith22:05:52

oh wait - it returns the thing you check, my bad

noisesmith22:05:11

so if `=` returns true, it will blow up because `true` isnt' a function

dev.4openID22:05:41

is https://clojuredocs.org/clojure.core.logic • (is u v op)

``````Set the value of a var to value of another var with the operation
applied. Non-relational.``````
So it seems is is not a t/f test

noisesmith22:05:07

we want clojure.test/is not clojure.core.logic/is

noisesmith22:05:09

``````(ins)scratch=> (doc clojure.test/is)
-------------------------
clojure.test/is
([form] [form msg])
Macro
Generic assertion macro.  'form' is any predicate test.
'msg' is an optional message to attach to the assertion.

Example: (is (= 4 (+ 2 2)) "Two plus two should be 4")

Special forms:

(is (thrown? c body)) checks that an instance of c is thrown from
body, fails if not; then returns the thing thrown.

(is (thrown-with-msg? c re body)) checks that an instance of c is
thrown AND that the message on the exception matches (with
re-find) the regular expression re.
nil``````

dev.4openID22:05:58

So (deftest azz   (is (= 1 1) ))

noisesmith22:05:12

right, that's a correct usage of test/is

dev.4openID22:05:33

(deftest aaa   ((is (= 0 (s/myproject.scope "123")))))

noisesmith22:05:48

that fails because neither true nor false are functions

noisesmith22:05:00

the extra parens announce that you want to call the return value of is

noisesmith22:05:09

and = can only return true or false

dev.4openID22:05:36

so = result in (is (true OR false)) which is not allowed

noisesmith22:05:00

in the context of invocation, neither true or false is valid, correct

noisesmith22:05:05

the fix is to not use extra parens

noisesmith22:05:20

parens request invocation

dev.4openID22:05:21

how do you mean

dev.4openID22:05:33

using my example

noisesmith22:05:43

((f)) error, true isn't a function

dev.4openID22:05:54

yes it is an outcome

noisesmith22:05:17

"outcome" doesn't mean anything in clojure, it's a boolean

dev.4openID22:05:27

how do i then get is to apply to the state

dev.4openID22:05:55

(=1 1 ) is true

noisesmith22:05:12

just change `((is ...))` to `(is ...)` the extra parentheses cause your error

dev.4openID22:05:12

that is a type there is only one set () on outside of is (deftest aaa   ((s (= 0 (s/myproject.scope "123")))) how it got here I dont know

dev.4openID22:05:36

(deftest aaa   (is (= 0 (s/myproject.scope "123")))) how it got here I dont know

dev.4openID22:05:45

fat fingers 😉

dpsutton22:05:00

if you just evaluate `(s/myproject.scope "123")` what does that return?

dev.4openID22:05:53

fail cannot find namespace s

dev.4openID22:05:09

no such namespace

dev.4openID22:05:22

yet I have it in the ns

noisesmith22:05:13

s/myproject.scope looks weird - I don't think it's even possible to have a var with that name

dev.4openID22:05:43

Unable to resolve symbol: scope in this context when I remove the s/

noisesmith22:05:07

well myproject.scope isn't a var, it's potentially a namespace

noisesmith22:05:13

what you want is something like s/abc

dev.4openID22:05:59

Guys I have been working on my stuff since 8 a,m BST. I am now whacked and perhaps I am making mistakes now. I appreciate your assistance but I think I need to call it a day and have a kip With a clearer mind Thanks @noisesmith

Patrick Farwick23:05:55

So i have a deps.edn project I am trying to use cljfmt to format. When I run

``````clojure -Sdeps '{:deps {cljfmt {:mvn/version "0.6.4"}}}' \
-m cljfmt.main check``````
it returns lines that are incorrect and also "no such file: project.clj" When I run the same command with `fix` it just returns the "no such file: project.clj" and does not seem to change the file at all

noisesmith23:05:19

it puts "project.clj" into its default-paths, which can be changed by providing more args after check / fix https://github.com/weavejester/cljfmt/blob/c9f94c0d426230d0fb696af4eb53b00cc5002035/cljfmt/src/cljfmt/main.clj#L196

noisesmith23:05:09

so you could try `cljfmt.main fix src test deps.edn`

Patrick Farwick23:05:41

++ that did seem to work

noisesmith23:05:18

anything after `fix` or `check`, if present, should be used instead of the default which contains `project.clj` - for bonus points one could make a PR on the project that doesn't try to access project.clj if it doesn't exist (at least not fail over it...)

Patrick Farwick23:05:01

Looks like there is an open PR that addresses the issue: https://github.com/weavejester/cljfmt/pull/193

noisesmith23:05:31

wow, nrepl versions 0.7.0 and 0.8.0-SNAPSHOT both crash on ::foo/bar keywords claiming they are invalid

dpsutton23:05:31

Like repo is to eval just that keyword. Blows up in >0.7.0?

noisesmith23:05:27

@dpsutton right, but that keyword ::foo/bar is only valid if `foo` is aliased in the current ns. Regardless, it shouldn't crash the client though.

noisesmith23:05:36

it's just a syntax error / unable to resolve

dpsutton23:05:44

Right. The fix sounds difficult. And there’s something similar in piggieback as well. How to read input

noisesmith23:05:46

if I use `lein repl :connect` instead of using nrepl directly via `clj` it works just fine

dpsutton23:05:05

I thought the ticket said it was only tty

dpsutton23:05:26

The bencode transport was unaffected

noisesmith23:05:34

maybe I found a second issue, or maybe clj is somehow using the tty transport the way I invoked it

noisesmith23:05:10

I problably should have brought this up in #clojure rather than #beginners, but at least I have my workaround