Fork me on GitHub
#beginners
<
2016-02-16
>
Josh Horwitz04:02:39

I’m working on this one right now, http://www.4clojure.com/problem/22

Josh Horwitz04:02:53

Crazy how hard it is to figure out how to solve in Clojure

ericlavigne04:02:15

The "special restrictions" is amusing, because that would otherwise be the one-word answer.

ericlavigne04:02:24

Basically means you need to write the count function. I would use loop/recur.

meow04:02:42

I hate loop/recur.

ericlavigne04:02:05

reduce also works.

ericlavigne04:02:26

What's wrong with loop/recur?

meow04:02:16

It always feels awkward. I prefer `into` or `for` instead. Or a custom transducer for some things.

Josh Horwitz04:02:10

Can’t even follow what you guys are talking about lol

ericlavigne04:02:50

Josh, just put all those words on separate pieces of paper, shuffle them, and choose one. Look up the page and it will help to solve the problem.

meow04:02:52

Is clojuredocs working again?

meow04:02:29

Just look at the info on `into` and `for` and you'll see what they can do.

meow04:02:32

My favorite clojure function, now with transducer support:

ericlavigne04:02:32

"into" makes a collection. Need to turn collection into scalar here.

ericlavigne04:02:40

"reduce" will be much easier.

meow04:02:16

I'll take `into` with transducers over `reduce` 99 times out of 100.

meow04:02:34

But I haven't looked at the problem you're trying to solve.

ericlavigne04:02:50

Josh, do you have access to REPL to try out partial solutions? That exercise page looks a little unforgiving if you can't test out your solutions.

meow04:02:10

Yes, always work in a REPL.

meow04:02:42

Cursive makes this super easy. (Once you get the darn thing configured.)

ericlavigne04:02:13

Josh, I'll help you through a reduce-based solution. Need to hear that you're ready though. 😉

meow04:02:21

@ericlavigne: His profile status would appear to indicate otherwise.

antonshwab10:02:13

I have a lein-project. Here is its structure

``````.
├── CHANGELOG.md
├── doc
│   └── intro.md
├── project.clj
├── resources
├── src
│   └── clojure_sicp
│       ├── braveclojure
│       │   └── brave.clj
│       └── core.clj
├── target
│   ├── base+system+user+dev
│   │   ├── classes
│   │   │   └── META-INF
│   │   │       └── maven
│   │   │           └── clojure-sicp
│   │   │               └── clojure-sicp
│   │   │                   └── pom.properties
│   │   ├── repl-port
│   │   └── stale
│   │       └── leiningen.core.classpath.extract-native-dependencies
│   └── base+system+user+dev+test+test
│       ├── classes
│       │   └── META-INF
│       │       └── maven
│       │           └── clojure-sicp
│       │               └── clojure-sicp
│       │                   └── pom.properties
│       └── stale
│           └── leiningen.core.classpath.extract-native-dependencies
└── test
└── clojure_sicp
└── core_test.clj
``````

antonshwab10:02:19

Q: How to run file brave.clj?

antonshwab10:02:49

I've tried `lein run -m clojure-sicp.braveclojure.brave`

antonshwab10:02:47

Here is brave.clj

``````(ns clojure-sicp.braveclojure.brave)

(defn latlng->point
"Convert lat/lng map to comma-separated string"
[latlng]
(str (:lat latlng) "," (:lng latlng)))

(defn points
[locations]
(clojure.string/join " " (map latlng->point locations)))

(def heists [{:location "Cologne, Germany"
:cheese-name "Archbishop Hildebold's Cheese Pretzel"
:lat 50.95
:lng 6.97}
{:location "Zurich, Switzerland"
:cheese-name "The Standard Emmental"
:lat 47.37
:lng 8.55}
{:location "Marseille, France"
:cheese-name "Le Fromage de Cosquer"
:lat 43.30
:lng 5.37}
{:location "Zurich, Switzerland"
:cheese-name "The Lesser Emmental"
:lat 47.37
:lng 8.55}
{:location "Vatican City"
:cheese-name "The Cheese of Turin"
:lat 41.90
:lng 12.45}])

(println (points heists)) ``````

antonshwab10:02:59

Here output

``````lein run clojure-sicp.braveclojure.brave
50.95,6.97 47.37,8.55 43.3,5.37 47.37,8.55 41.9,12.45
Exception in thread "main" java.lang.IllegalStateException: heists already refers to: #'clojure-sicp.braveclojure.brave/heists in namespace: clojure-sicp.core, compiling:(clojure_sicp/core.clj:7:1)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6875)
at clojure.lang.Compiler.analyze(Compiler.java:6669)
at clojure.lang.Compiler.analyze(Compiler.java:6625)
at clojure.lang.Compiler.eval(Compiler.java:6931)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at clojure.lang.RestFn.applyTo(RestFn.java:142)
at clojure.core\$apply.invokeStatic(core.clj:648)
at clojure.lang.RestFn.applyTo(RestFn.java:137)
at clojure.core\$apply.invokeStatic(core.clj:648)
at clojure.core\$require.invokeStatic(core.clj:5796)
at clojure.core\$require.doInvoke(core.clj:5796)
at clojure.lang.RestFn.invoke(RestFn.java:408)
at user\$eval5\$fn__7.invoke(form-init8450613833812556873.clj:1)
at user\$eval5.invokeStatic(form-init8450613833812556873.clj:1)
at user\$eval5.invoke(form-init8450613833812556873.clj:1)
at clojure.lang.Compiler.eval(Compiler.java:6927)
at clojure.lang.Compiler.eval(Compiler.java:6917)
at clojure.main\$init_opt.invokeStatic(main.clj:277)
at clojure.main\$init_opt.invoke(main.clj:277)
at clojure.main\$initialize.invokeStatic(main.clj:308)
at clojure.main\$null_opt.invokeStatic(main.clj:342)
at clojure.main\$null_opt.invoke(main.clj:339)
at clojure.main\$main.invokeStatic(main.clj:421)
at clojure.main\$main.doInvoke(main.clj:384)
at clojure.lang.RestFn.invoke(RestFn.java:421)
at clojure.lang.Var.invoke(Var.java:383)
at clojure.lang.AFn.applyToHelper(AFn.java:156)
at clojure.lang.Var.applyTo(Var.java:700)
at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalStateException: heists already refers to: #'clojure-sicp.braveclojure.brave/heists in namespace: clojure-sicp.core
at clojure.lang.Namespace.warnOrFailOnReplace(Namespace.java:88)
at clojure.lang.Namespace.intern(Namespace.java:72)
at clojure.lang.Compiler\$DefExpr\$Parser.parse(Compiler.java:546)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6868)
... 46 more
``````

antonshwab10:02:02

delaguardo10:02:39

When you call `lein run -m NAMESPACE` without explicit MAIN_FUNCTION leiningen lookup `-main` in NAMESPACE. If your NAMESPACE doesn't have one it run main function from namespace declared under :main option in project.clj file.

delaguardo10:02:59

try read `lein help run`

delaguardo10:02:37

And obviously there is no need to call clojure as any script language)

antonshwab10:02:55

I understand how to run core.clj, but didn't understand how to run any other file

delaguardo10:02:20

just require any namespace into core.clj

delaguardo10:02:42

``````(ns any.core
(:require [any.foo :as foo]))

(defn -main [& args]
(foo/bar))
``````

delaguardo10:02:20

you should pay attention that there is no need to call "another file". operate with namespaces in your program

antonshwab10:02:26

what is `bar` in `foo/bar`?

delaguardo10:02:17

function in any.foo namespace

antonshwab10:02:23

``````(defn -main
[& args]
(brave/(println (points heists)))``````
right?

delaguardo11:02:17

``````(defn -main [& args]
(println (brave/points brave/heists)))
``````

delaguardo11:02:38

because `(println (brave heists))` is not a function

delaguardo11:02:49

its function call

antonshwab11:02:00

In `brave.clj`

``````(defn print1
(println (points heists)))``````

antonshwab11:02:33

In `core.clj`

``````(ns clojure-sicp.core
(:require [clojure-sicp.braveclojure.brave] :as brave))

(defn -main
[& args]
(brave/print1))``````

delaguardo11:02:56

right, its better for understand what is going on

antonshwab11:02:17

but again

``````lein run clojure-sicp.braveclojure.brave
Exception in thread "main" java.lang.IllegalArgumentException: Parameter declaration "println" should be a vector, compiling:(clojure_sicp/braveclojure/brave.clj:33:1)
``````

delaguardo11:02:23

``````(defn print1 []
(println (points heists)))
``````

delaguardo11:02:32

forgot about `[]`

delaguardo11:02:24

and after that you can start with `lein run` without namespace

antonshwab11:02:47

``````lein run                                /cygdrive/c/Users/a_s/clojure-sicp 1
Exception in thread "main" java.lang.ClassCastException: java.lang.Boolean cannot be cast to clojure.lang.Symbol, compiling:(clojure_sicp/core.clj:1:1)
``````

delaguardo11:02:28

``````(ns clojure-sicp.core
(:require [clojure-sicp.braveclojure.brave] :as brave))
``````

delaguardo11:02:45

``````(ns clojure-sicp.core
(:require [clojure-sicp.braveclojure.brave :as brave]))
``````

antonshwab11:02:53

it is quite difficult.

delaguardo11:02:15

little practice and you will be happy with clojure)

antonshwab11:02:50

This is the only way to run the examples from the book http://www.braveclojure.com/do-things/#Pulling_It_All_Together ? That is, write the code in one file, then in core.clj:

``````(ns clojure-sicp.core
(:require [clojure-sicp.braveclojure.brave] :as brave))``````
and then place the name of desired function in `-main`. Finally: lein run. So?

delaguardo11:02:37

`[clojure-sicp.braveclojure.brave] :as brave` => `[clojure-sicp.braveclojure.brave :as brave]`

antonshwab11:02:44

misprint. in my file

``[clojure-sicp.braveclojure.brave :as brave]``

antonshwab11:02:30

мне показалось?

delaguardo11:02:54

most clojure program has one entry point

Josh Horwitz13:02:02

@meow @ericlavigne Sorry Guys! I have a 8 month pregnant wife who needed me last night so I got dragged away lol

meow13:02:09

Family comes first. Understood.

Josh Horwitz13:02:41

I appreciate it, it’s getting pretty crazy the closer we get!

Josh Horwitz13:02:56

I’m trying to differentiate between Reduce, Map and Apply

meow13:02:31

Should be able to get help here. Best wishes on your impending delivery.

akiva13:02:26

Hah, @joshua.d.horwitz, your life is about to change in a way you cannot even imagine. The first few months are incredible and fleeting so memorize, document, and journal every day. You’ll appreciate it later.

Josh Horwitz14:02:44

@akiva: I’m freaking out!

Josh Horwitz15:02:35

@akiva: That is great! Thanks for the advice, I’m going to get both, need all the help I can get!

yogidevbear15:02:18

@joshua.d.horwitz: Listen to your intuition and especially your wife's intuition. And forget about sleep for the next 13 years at least 😉

akiva15:02:54

I think I’ll lose more sleep when my daughter turns 13. And buy a bouquet of shotguns.

lucien.knechtli15:02:34

different one for every day of the week?

akiva15:02:51

Yep, and double-barrelled ones for the weekend.

lucien.knechtli15:02:17

might have better luck with bear traps 😛

akiva15:02:10

Hahaha. And anti-personnel landmines. And a moat that even Leinigen’s ants can’t cross.

akiva15:02:15

There we’re slightly back on topic.

thomasdeutsch17:02:05

what is wrong with this?: `(apply js/Date. [2011 4 3])` is it not possible to use apply with a js function, or is ist something else i am doing wrong?

roberto17:02:12

hmmm, not sure. You might want to ask in #C03S1L9DN

roberto17:02:26

I don’t know if `js/Date.` is a function

roberto17:02:46

I’ve never had to do that, so haven’t run into a scenario like yours

solicode17:02:56

@thomasdeutsch: It appears to be a JavaScript restriction. `new` and `apply` don’t seem to mix the way you’re expecting it to

roberto17:02:10

yeah, I don’t think `new` is a function

roberto17:02:41

I would recommend using cljs-time for working with dates https://github.com/andrewmcveigh/cljs-time

roberto17:02:49

if you are doing anything a little bit complicated.

thomasdeutsch17:02:36

i do not need that lib I did not know about this js restriction. thank you @solicode !