Skip to content

Latest commit

 

History

History
133 lines (111 loc) · 4.41 KB

2020-06-29.md

File metadata and controls

133 lines (111 loc) · 4.41 KB

Learning Clojure in Public - Week 2 Day 1 (8/35)

Week 1 is now over, now onto week 2! I think I covered a lot of ground last week (6 chapters of Clojure from the Group Up, 49 4clojure problems). I hope to do the same this week.

It feels like I started just yesterday (almost true!) but I've already learned a lot. I am also making a lot of progress in how I synthesize my learning and am able to produce reusable (at least for myself) content. I mostly refer to my own notes at this point when I am trying to solve a problem.

Expectations

The expectation for today was to cover some of Chapter 8 of Clojure from the Ground Up (maybe half since it's a long chapter), and then solve some 4clojure problems. I was aiming at the lofty goal of completing 11 new ones to get my total to 60 because it's the next step in my ClojureFam checkpoint but it looks that it will be for later this week.

What I learned

In Chapter 8 of Clojure from the Ground Up, we went to space! We built a rocket.

You can see the code I wrote here and the tests here.

user=> (use 'scratch.rocket :reload)
nil
user=> (atlas-v)
{:dry-mass 50050, :fuel-mass 284450, :time 0, :isp 3050, :max-fuel-rate 285550/253, :max-thrust 4152000.0}
user=> (-> (atlas-v) prepare clojure.pprint/pprint)
{:dry-mass 50050,
 :fuel-mass 284450,
 :time 0,
 :isp 3050,
 :max-fuel-rate 285550/253,
 :max-thrust 4152000.0,
 :position {:x 6378137, :y 0, :z 0},
 :velocity {:x 0, :y 463.8312116386399, :z 0}}
nil

Then running this in the REPL, caused a NullPointerException. I have had trouble debugging error when trying to solve 4clojure problems so every little bit of information will be very helpful.

user=>
(-> (atlas-v) prepare (step 1) clojure.pprint/pprint)
Execution error (NullPointerException) at scratch.rocket/step (rocket.clj:128).
null
class java.lang.NullPointerException

I wasn't able to print the stack with (pst *e) like in the examples.

I also didn't realize that on Calva you can just click "All" and you will see the stack. Quite useful!

After fixing the issue I was able to run the functions in the REPL:

user=> (use 'scratch.rocket :reload)
nil
user=> (-> (atlas-v) prepare (step 1) clojure.pprint/pprint)
6378137.0
{:time 0,
 :isp 3050,
 :fuel-mass 71680300/253,
 :max-fuel-rate 285550/253,
 :dry-mass 50050,
 :max-thrust 4152000.0,
 :position {:x 0, :y 463.8312116386399, :z 0},
 :t 1,
 :velocity
 {:x 0.491184411870704, :y 463.8312116386399, :z -9.799999999999999}}
nil

With some difficulty I was able to match the results of the author and plot the trajectory of the rocket:

user=>
(->> (atlas-v) prepare (trajectory 1) (take 3) clojure.pprint/pprint)
({:dry-mass 50050,
  :fuel-mass 284450,
  :time 0,
  :isp 3050,
  :max-fuel-rate 285550/253,
  :max-thrust 4152000.0,
  :position {:x 6378137, :y 0, :z 0},
  :velocity {:x 0, :y 463.8312116386399, :z 0}}
 {:dry-mass 50050,
  :fuel-mass 71680300/253,
  :time 1,
  :isp 3050,
  :max-fuel-rate 285550/253,
  :max-thrust 4152000.0,
  :position {:x 6378137, :y 463.8312116386399, :z 0},
  :velocity
  {:x 0.491184411870704,
   :y 463.8312116386399,
   :z -6.000769315822031E-16}}
 {:dry-mass 50050,
  :fuel-mass 71394750/253,
  :time 2,
  :isp 3050,
  :max-fuel-rate 285550/253,
  :max-thrust 4152000.0,
  :position
  {:x 6378137.491184412,
   :y 927.6624232772798,
   :z -6.000769315822031E-16},
  :velocity
  {:x 1.0172105016106543,
   :y 463.83049896253056,
   :z -1.2001538631644063E-15}})
nil

And even the altitude of the rocket:

user=>
(->> (atlas-v) prepare (trajectory 1) (map altitude) (take 10) clojure.pprint/pprint)
(0.0
 0.016865378245711327
 0.5586459208279848
 1.660183128900826
 3.3565550493076444
 5.683078692294657
 8.675312489271164
 12.36905876826495
 16.80036628153175
 22.005532761104405)
nil

Takeaways

It was a lot of math, but it was also very instructive on how to make functions work together. Each function serves one purpose and chaining them one after another gets you where you need to go. I look forward to completing the rest of the program tomorrow.

Weirdly enough the difficulty lied in making the functions return the same output as the author's, as I have been typing all the functions by hand (which helps understanding but introduces bugs that are hard to debug).

Sadly I didn't complete any 4clojure today. I hope that things will be different tomorrow.