<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Fulton Byrne</title>
    <description>The latest articles on Forem by Fulton Byrne (@fbyrne).</description>
    <link>https://forem.com/fbyrne</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F253986%2Fe3069f94-a546-4b90-8211-c2bad21b9349.jpg</url>
      <title>Forem: Fulton Byrne</title>
      <link>https://forem.com/fbyrne</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fbyrne"/>
    <language>en</language>
    <item>
      <title>Trying out git submodules</title>
      <dc:creator>Fulton Byrne</dc:creator>
      <pubDate>Sat, 02 Jan 2021 16:41:14 +0000</pubDate>
      <link>https://forem.com/fbyrne/trying-out-git-submodules-2o3n</link>
      <guid>https://forem.com/fbyrne/trying-out-git-submodules-2o3n</guid>
      <description>&lt;p&gt;Organizing code into multiple git repositories remains one of the most charming organization methods available to developers. Multiple repositories work best when teams logically isolate their work together and away from others, but this system may break down when teams develop dependencies on each other's work. &lt;/p&gt;

&lt;p&gt;Perhaps, a team writes helm charts and deploys them from one repository, but code that the helm chart deploys exists in another. Git submodules provide an oft overlooked method for mitigating &lt;a href="https://www.sciencedirect.com/topics/psychology/cognitive-load-theory"&gt;cognitive load&lt;/a&gt; when managing dependent repositories.&lt;/p&gt;

&lt;p&gt;When following this tutorial feel free to fork my example repository, &lt;code&gt;git@github.com:Freyert/submodule_example.git&lt;/code&gt;, and follow along.&lt;/p&gt;

&lt;p&gt;Getting started with submodules is actually fairly easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git submodule add git@github.com:left-pad/left-pad.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;NOTE: left-pad is no longer maintained.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This clones the remote repository into the current directory and adds configuration to &lt;code&gt;.gitmodules&lt;/code&gt; at the git repository’s root. Git add and git commit these changes then push to the remote repository.&lt;/p&gt;

&lt;p&gt;Now remove the cloned version of the repository and try cloning down the repository again. Notice that the submodule resources were not cloned down from the submodule’s repository. This may not make sense, but imagine a repository with hundreds of submodules. Pulling only the submodules required saves a lot of time. Here is one way to clone a submodule:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git submodule update --init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This initializes and updates the git submodule, therefore fetching the remote code. If the work environment generally requires all submodules when cloning the repository, more recent versions of git allow this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone git@github.com:Freyert/submodule_example.git --recurse-submodules
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That should suffice for a quick taste of submodules to peak interest. Managing dependencies through submodules introduces its own complexity which is why almost every system today includes dependency management frameworks. Think NPM for nodejs or Ruby Gems for Ruby. When considering submodules weigh the cost of using submodules against the cost of introducing a dependency management system. Most of the time dependency management wins, but submodules do have some advantages that are worth learning about.&lt;/p&gt;

&lt;h3&gt;
  
  
  More Reading
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/3796927/how-to-git-clone-including-submodules"&gt;&lt;em&gt;Stackoverflow: How to "git clone" including submodules?&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://git-scm.com/book/en/v2/Git-Tools-Submodules"&gt;&lt;em&gt;Pro Git: Chapter 7.11 Submodules&lt;/em&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://git-scm.com/docs/git-submodule"&gt;&lt;em&gt;Git Command Reference: git-submodule&lt;/em&gt;&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;Good resource for all &lt;code&gt;git submodule&lt;/code&gt; commands.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://git-scm.com/docs/gitsubmodules"&gt;&lt;em&gt;Git Reference: gitsubmodules&lt;/em&gt;&lt;/a&gt; 

&lt;ul&gt;
&lt;li&gt;Good resource for configuring &lt;code&gt;submodules&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Cover Image of the git logo is attributed to Jason Long, CC BY 3.0 &lt;a href="https://creativecommons.org/licenses/by/3.0"&gt;https://creativecommons.org/licenses/by/3.0&lt;/a&gt;, via Wikimedia Commons the image was retrieved on January 2, 2021 from &lt;a href="https://commons.wikimedia.org/wiki/File:Git-logo.svg"&gt;https://commons.wikimedia.org/wiki/File:Git-logo.svg&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Notes on Dr. Andrew Ng's "Machine Learning" Week 5 </title>
      <dc:creator>Fulton Byrne</dc:creator>
      <pubDate>Sat, 01 Feb 2020 21:04:01 +0000</pubDate>
      <link>https://forem.com/fbyrne/notes-on-dr-andrew-ng-s-machine-learning-week-5-4c1h</link>
      <guid>https://forem.com/fbyrne/notes-on-dr-andrew-ng-s-machine-learning-week-5-4c1h</guid>
      <description>&lt;p&gt;This week is definitely one of the more difficult to get through. The exercise has a high number of complex, moving parts that all need to move together in order to complete.&lt;/p&gt;

&lt;p&gt;Often times when struggling with concepts I look for other explanations. I found Zeyuan Hu's write up on the math for the exercise a great aggregation of all the math for the week: &lt;a href="https://zhu45.org/posts/2017/May/23/andrew-ngs-ml-week-04-05/" rel="noopener noreferrer"&gt;&lt;em&gt;Andrew Ngs ML Week 04 - 05&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I also discovered some jewels in the course forums. The course forums are generally a horror story of duplicated questions, but if you look hard enough you can find some serious help from the course mentors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.coursera.org/learn/machine-learning/discussions/all/threads/m0ZdvjSrEeWddiIAC9pDDA" rel="noopener noreferrer"&gt;&lt;em&gt;Programming Exercise Tutorial (List)&lt;/em&gt;&lt;/a&gt; will be my first stop before starting any exercises from here on out. The mentor provided tutorials typically provide a nigh step by step write up to completing any exercise.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mistakes Were Made
&lt;/h2&gt;

&lt;p&gt;The common errors I ran into were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Incorrect Dimensions&lt;/li&gt;
&lt;li&gt;Matrix Multiplication Caveats&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Incorrect Dimensions
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.coursera.org/learn/machine-learning/discussions/all/threads/a8Kce_WxEeS16yIACyoj1Q" rel="noopener noreferrer"&gt;&lt;em&gt;ex4 tutorial for nnCostFunction and backpropagation&lt;/em&gt;&lt;/a&gt; goes in depth on how each matrix dimension should match.&lt;/p&gt;

&lt;p&gt;I also found these &lt;a href="https://www.coursera.org/learn/machine-learning/discussions/all/threads/p7PJgfyLEearJRI0kpm64A/replies/axkiVfyTEeaT0Qr5Ew1QIA/comments/FOtymfyVEeaT0Qr5Ew1QIA" rel="noopener noreferrer"&gt;debugging tips&lt;/a&gt; to be extraordinarily helpful combined with knowing how to use the &lt;a href="https://octave.org/doc/v4.2.1/Debugging.html" rel="noopener noreferrer"&gt;Octave Debugger&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Matrix Multiplication
&lt;/h1&gt;

&lt;p&gt;I &lt;em&gt;really&lt;/em&gt; prefer using the vectorized approach for any solution in the course. They are generally far easier to write, but you can also get really mixed up because matrix multiplication has a lot of rules and conditions.&lt;/p&gt;

&lt;p&gt;The cost function with the double sigmas made me really want to use matrix multiplication to do summation, but I ended up using the easier element wise multiplication and summation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fta9og79zejmdiy5ee76i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fta9og79zejmdiy5ee76i.png" alt="cost function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, the forum post &lt;a href="https://www.coursera.org/learn/machine-learning/discussions/weeks/5/threads/AzIrrO7wEeaV3gonaJwAFA" rel="noopener noreferrer"&gt;&lt;em&gt;Computing the NN cost J using the matrix product&lt;/em&gt;&lt;/a&gt; explains the intuition needed in order to correctly apply matrix multiplication in this case.&lt;/p&gt;

&lt;p&gt;I'm writing mine out in Octave as a way to translate the material between two mediums. So we begin with the two mxK (m = 2 &amp;amp; K = 3) matrices:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A = [1 2; 3 4; 5 6]
B = [7 8; 9 10; 11 12]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want the sum over the rows and columns of the element wise product:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sum(sum(A .* B))
ans =  217 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Naively, I expect I can reach an equivalent answer by the matrix product of transpose A and B.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A' * B

ans =

    89    98
   116   128
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Surprise! its an mxm (2x2) matrix because when you multiply m x K and K x m you get an m x m matrix.&lt;/p&gt;

&lt;p&gt;The cost function is supposed to reduce to a scalar value though. What's missing? As the mentor's article points out we just need to take the &lt;a href="https://en.wikipedia.org/wiki/Trace_(linear_algebra)" rel="noopener noreferrer"&gt;trace&lt;/a&gt; or the sum of the main diagonal of a &lt;em&gt;square&lt;/em&gt; matrix for our answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;trace(A' * B)
ans = 217
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Does this always work? In this instance, where we want the sum of the columns and rows of two matrices of equal dimension: yes. This is not always the case though.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Matrix_multiplication" rel="noopener noreferrer"&gt;Matrix multiplication&lt;/a&gt; requires mxn * nxp dimensions. So only the columns of the first must match the rows of the second. You won't necessarily receive a square matrix.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Hadamard_product_(matrices)" rel="noopener noreferrer"&gt;Hadamard Product&lt;/a&gt; a.k.a element wise multiplication only requires that the two matrices have the same dimensions. Again, not necessarily resulting in a square matrix.&lt;/p&gt;

&lt;p&gt;Specifically to the above cost function though, this &lt;em&gt;does&lt;/em&gt; always work because &lt;code&gt;y&lt;/code&gt; and &lt;code&gt;x&lt;/code&gt; are both mxK. Intuitively this makes sense. If X is a matrix of (instances) x (features) so that each row is an instance with columns for each feature then each row of &lt;code&gt;y&lt;/code&gt; is an instance's "actual" value. That alone doesn't work because &lt;code&gt;y&lt;/code&gt; is typically either a column vector or its columns correspond to the number of classes as in the case of digit recognition.&lt;/p&gt;

&lt;p&gt;The reason this &lt;em&gt;does&lt;/em&gt; always work for the cost function is we are not dealing with X, but &lt;code&gt;h(X)&lt;/code&gt; which contains the "predicted values" for each instance in X therefore having the same dimensions as y. Therefore the formula can multiply &lt;code&gt;h(X)&lt;/code&gt; and y because they have the same dimensions and we can sum the result using trace because their product is a square matrix.&lt;/p&gt;

&lt;p&gt;I won't make guarantees here though as this really only applies to the double sigma part of the equation. Evaluating the terms inside the equation is another story, but I wager if I encountered this equation again in another exercise I could build the vectorized version now that I understand more of the rules.&lt;/p&gt;

&lt;p&gt;It is also interesting to note that the mentor points out the vectorized implementation may not necessarily perform better due to a lot of wasted calculations that are thrown away after using trace. So if you built the element wise version like myself give yourself a pat on the back.&lt;/p&gt;

&lt;p&gt;Those are my notes for the week! Best of luck!&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Notes on Andrew Ng's "Machine Learning" Week 2</title>
      <dc:creator>Fulton Byrne</dc:creator>
      <pubDate>Tue, 24 Dec 2019 23:43:39 +0000</pubDate>
      <link>https://forem.com/fbyrne/notes-on-andrew-ng-s-machine-learning-week-2-15n9</link>
      <guid>https://forem.com/fbyrne/notes-on-andrew-ng-s-machine-learning-week-2-15n9</guid>
      <description>&lt;p&gt;The most difficult part of week 2 is taking the formulas and converting them into Octave. Especially, if  you don't quite grasp the formula notations and Octave is new.&lt;/p&gt;

&lt;p&gt;I will walk through some of the boiler plate code regarding Gradient Descent, the hypothesis function, cost function, and try to develop some intuition to move things forward. I will also point out my own gaps of understanding in the hopes that I can fill them in later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Know Your Resources
&lt;/h2&gt;

&lt;p&gt;As I was writing these notes I stumbled on the "Resources" section which has been immensely helpful and clarified quite a lot. I highly recommend reading the lecture notes for each week as they clarify the videos and other resources quite a lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Shape of Matrices
&lt;/h2&gt;

&lt;p&gt;The shapes of two matrices dictate whether or not you can compute the &lt;a href="https://mathinsight.org/matrix_vector_multiplication"&gt;dot product&lt;/a&gt; of the two.&lt;/p&gt;

&lt;p&gt;In Dr. Ng's slides on &lt;em&gt;Vectorization&lt;/em&gt; he calculates the following hypothesis function using an iterative notation (summation) and a vectorized notation (transpose and dot product):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jlB5i6rW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/q5zmkdt19i9po63qnpgx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jlB5i6rW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/q5zmkdt19i9po63qnpgx.png" alt="Iterative Summation vs. Vectorized Dot Product"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Note that in this case "x_j" (x subscript j) indicates the value of feature "j". The Week 2 Lecture Notes under resources is incredibly helpful here. "x" is a column vector of feature values. Perhaps its the square footage of each house in the data set or the  number of floors for each house. Whereas the &lt;code&gt;thetas&lt;/code&gt; represent "weights" for each feature such as the price per floor or price per square foot.&lt;/p&gt;

&lt;p&gt;The week 2 notes expand this function like so:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;hθ(x)=θ_0+θ_1x_1+θ_2x_2+θ_3x_3+⋯+θ_nx_n&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This hypothesis function takes a row or instance from the data set, say a house, and takes each of its features &lt;code&gt;x_y&lt;/code&gt; multiplied against their corresponding &lt;code&gt;θ_y&lt;/code&gt; or weight. The value for theta is what we attempt to discover through Gradient Descent.&lt;/p&gt;

&lt;p&gt;A part of the exercise I don't quite understand is that a column of ones is added to X. This column of ones will be paired with &lt;code&gt;θ_0&lt;/code&gt; when calculating the &lt;code&gt;hθ(x)&lt;/code&gt; function. This is definitely a part I have confusion on.&lt;/p&gt;

&lt;p&gt;Getting back to vectorization. Seeing that the summation form of the hypothesis function can be represented as &lt;code&gt;hθ(x)=θ_0+θ_1x_1+θ_2x_2+θ_3x_3+⋯+θ_nx_n&lt;/code&gt; makes it a lot easier to connect this to matrix multiplication where you multiply each value of a row times each value of a column in two matrices and sum them to get a new value.&lt;/p&gt;

&lt;p&gt;Since we &lt;em&gt;know&lt;/em&gt; x and theta are column vectors lets see how the vectorized version of the hypothesis function would actually work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;% initialize column vector a. ; starts a new row.
octave:8&amp;gt; a = [ 1; 2; 3; 4; 5; 6 ]
a =

   1
   2
   3
   4
   5
   6

% initialize column vector b. ; starts a new row.
octave:9&amp;gt; b = [ 1; 2; 3; 4; 5; 6]
b =

   1
   2
   3
   4
   5
   6

% and voila a transpose dot product b returns a single value.
octave:10&amp;gt; a' * b
ans =  91
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Continuing on the Programming Tips from Mentors has a really helpful clarification to reach a fully vectorized solution:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"h(x) = theta' * x" vs. "h(x) = X * theta?"&lt;/p&gt;

&lt;p&gt;Lower-case x typically indicates a single training example.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore X potentially has multiple "x" or single training examples. A training example in our case would be a row and each column in the row a feature for that training sample.&lt;/p&gt;

&lt;p&gt;Remember the &lt;code&gt;Σ(h(x_j) - y)&lt;/code&gt;? The sigma tells us we need to calculate this for all rows of X. Since X has a column for each row of theta we can multiply them together as we did &lt;code&gt;theta' * x&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Calculating Cost
&lt;/h2&gt;

&lt;p&gt;The programming exercise has this definition for the cost function:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hU54bktM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/lmol7e7c1urx3dacwziu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hU54bktM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/lmol7e7c1urx3dacwziu.png" alt="Cost Function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The cost function &lt;code&gt;J(θ)&lt;/code&gt; has a very minor detail where it plugs &lt;code&gt;x^(i)&lt;/code&gt; into the hypothesis function. Meaning for a particular feature/column calculate the hypothesis over all training examples. The summation symbols to the left indicate that this should be done for each column in the data set. You could do this iteratively, but its  a lot easier when you realize X * theta means the same thing. X * theta means multiply every training example (row) times every theta (column) aka &lt;code&gt;θ_0 + θ_1x_1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of subtracting one &lt;code&gt;x&lt;/code&gt; from one &lt;code&gt;y&lt;/code&gt; we can now use the &lt;code&gt;h(x)&lt;/code&gt; vector calculated above subtract the &lt;code&gt;y&lt;/code&gt; matrix. &lt;/p&gt;

&lt;p&gt;The interesting bit though is that originally &lt;code&gt;y&lt;/code&gt; was a single column and so was &lt;code&gt;X&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;X = data(:, 1); y = data(:, 2);
disp(size(X))
disp(size(y))

% 97 1
% 97 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, &lt;code&gt;X&lt;/code&gt; is reassigned to have two columns. Subtraction between these two matrices &lt;a href="https://stattrek.com/matrix-algebra/matrix-addition.aspx"&gt;should not be possible&lt;/a&gt;, but &lt;a href="https://math.stackexchange.com/questions/86848/to-subtract-two-matrices-with-different-dimensions-in-octave-matlab"&gt;Octave allows it&lt;/a&gt; via &lt;a href="https://octave.org/doc/v4.2.2/Broadcasting.html"&gt;"broadcasting"&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;y&lt;/code&gt; has fewer dimensions Octave transforms it into a matrix consisting of &lt;code&gt;[y, y]&lt;/code&gt; to match &lt;code&gt;X&lt;/code&gt; dimension of 97 x 2. At this point the ones column on &lt;code&gt;X&lt;/code&gt; is a throw away column used to provide proper shape. &lt;code&gt;X[:,2]&lt;/code&gt; and &lt;code&gt;y[:]&lt;/code&gt; have a relation recorded in the original dataset, but the arbitrary ones column should have no such relation. Perhaps their hint about dimensional analysis makes this make sense.&lt;/p&gt;

&lt;p&gt;The next trick comes when trying to figure out how to take &lt;code&gt;h(x) - y&lt;/code&gt; to the power of &lt;code&gt;2&lt;/code&gt;. Remember everything in the function definition is defined iteratively so what the function is telling us is to square &lt;em&gt;each&lt;/em&gt; element of the matrix, &lt;em&gt;not&lt;/em&gt; the entire matrix.&lt;/p&gt;

&lt;p&gt;Your intuition might be to use Octave's broadcast exponentiation (&lt;code&gt;x.^y&lt;/code&gt; or &lt;code&gt;x.**y&lt;/code&gt;) provides this for us:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Element-by-element power operator. If both operands are matrices, the number of rows and columns must both agree, or they must be broadcastable to the same shape. If several complex results are possible, the one with smallest non-negative argument (angle) is taken. This rule may return a complex root even when a real root is also possible. Use realpow, realsqrt, cbrt, or nthroot if a real result is preferred.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(&lt;a href="https://octave.org/doc/v4.2.2/Broadcasting.html"&gt;&lt;em&gt;Broadcasting 19.2&lt;/em&gt;&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Which I think actually works since its an element-by-element power operation, but the vectorized version is described in the week 2 lecture notes as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fkC6hVTP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/spgejxipj4m0tojqfxe5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fkC6hVTP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/spgejxipj4m0tojqfxe5.png" alt="Vectorized Cost Function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This preserves the iterative squaring of each term by multiplying each position in the matrix by itself. Pretty neat!&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;There is so much more I still don't quite understand, but at this point I have a framework for converting iterative solutions to vectorized solutions. Some of intuition behind the hypothesis function and the cost functions themselves are still beyond me. Why is understanding "dimensional analysis" important? Why is it OK to do broadcast subtraction of &lt;code&gt;(X * theta) - y&lt;/code&gt;?&lt;/p&gt;

</description>
      <category>machinelearning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Parsing JSON with Circe</title>
      <dc:creator>Fulton Byrne</dc:creator>
      <pubDate>Sat, 02 Nov 2019 16:02:53 +0000</pubDate>
      <link>https://forem.com/fbyrne/parsing-json-with-circe-139o</link>
      <guid>https://forem.com/fbyrne/parsing-json-with-circe-139o</guid>
      <description>&lt;p&gt;I received some very good feedback on my last post: Scala has some popular libraries that should be avoided. Not only that, I learned a few libraries I considered before required more investigation. Today I want to look at Circe. After working in Scala a bit, I understand the problems it wants to solve and why the project makes the choices it does.&lt;/p&gt;

&lt;h1&gt;
  
  
  Cursors
&lt;/h1&gt;

&lt;p&gt;Think about how you use JSON for a minute. Programs retrieve or assign to a single field at a time. Parsing the entire document when the program only needs one field wastes processing time and heap memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;car&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I wrote more NodeJS I would not bat an eye at parsing one document entirely, but when working with many documents or a large document I would use &lt;a href="https://www.npmjs.com/package/JSONStream"&gt;JSONStream&lt;/a&gt; to selectively parse items out of the document. This provides two benefits: it does not require storing the entire document into memory and only the pertinent fields parse.&lt;/p&gt;

&lt;p&gt;Circe provides "cursors", an API to traverse raw JSON and project the values therein. Think about cursors the way you think about the blinking &lt;code&gt;|&lt;/code&gt; cursor when writing an email or text. It keeps track of your place in the document, moves around the document to track the new current location, and even selects interesting text for modification.&lt;/p&gt;

&lt;h1&gt;
  
  
  Example
&lt;/h1&gt;

&lt;p&gt;I'm assuming some &lt;code&gt;sbt&lt;/code&gt; knowledge. Here's the &lt;code&gt;build.sbt&lt;/code&gt; I used to run this project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"basic_json"&lt;/span&gt;

&lt;span class="n"&gt;version&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"0.1"&lt;/span&gt;

&lt;span class="n"&gt;scalaVersion&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;"2.12.2"&lt;/span&gt;

&lt;span class="k"&gt;lazy&lt;/span&gt; &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;circeVersion&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.12.2"&lt;/span&gt;


&lt;span class="n"&gt;libraryDependencies&lt;/span&gt; &lt;span class="o"&gt;++=&lt;/span&gt; &lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
  &lt;span class="s"&gt;"io.circe"&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="s"&gt;"circe-parser"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;circeVersion&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;"io.circe"&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="s"&gt;"circe-core"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;circeVersion&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
  &lt;span class="s"&gt;"io.circe"&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="s"&gt;"circe-generic"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;circeVersion&lt;/span&gt;
&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example iterates through a JSON document via a cursor to select and present the car's age the document describes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;io.circe.parser._&lt;/span&gt;
&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;rawJson&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="s"&gt;"""
      |{
      |  "hello": "world",
      |  "car": {
      |    "type": "honda",
      |    "age": 23,
      |    "color": "blue",
      |    "used": true,
      |    "owner": {
      |       "name": "darwin",
      |       "age": 43
      |    }
      |  }
      |}
      |"""&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;stripMargin&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;parsed&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rawJson&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Left&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Right&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;hcursor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;downField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"car"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;get&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Right&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Left&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsed&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A lot going on, but focusing on the critical area simplifies things:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;parsed&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rawJson&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Left&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Right&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;hcursor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;downField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"car"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;get&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;match&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Right&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="nc"&gt;Left&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Circe's &lt;a href="https://circe.github.io/circe/api/io/circe/Parser.html"&gt;&lt;code&gt;parse&lt;/code&gt;&lt;/a&gt; from &lt;code&gt;import io.circe.parser._&lt;/code&gt; ingests the raw JSON document and returns an &lt;a href="https://www.scala-lang.org/api/current/scala/util/Either.html"&gt;&lt;code&gt;Either&lt;/code&gt;&lt;/a&gt;. &lt;code&gt;Left&lt;/code&gt; contains useful information about failures, while &lt;code&gt;Right&lt;/code&gt; contains success values. I always return the unwrapped &lt;code&gt;Left&lt;/code&gt; value since I just want to log results, but ideally &lt;code&gt;Lefts&lt;/code&gt; would branch code into an error handling branch.&lt;/p&gt;

&lt;p&gt;Finally, the important piece: actually retrieving the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nv"&gt;r&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;hcursor&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;downField&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"car"&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;get&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="s"&gt;"age"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;r&lt;/code&gt; represents the &lt;code&gt;Right&lt;/code&gt; result of &lt;code&gt;parse&lt;/code&gt; which contains an &lt;code&gt;hcursor&lt;/code&gt; into the JSON document. &lt;a href="https://circe.github.io/circe/api/io/circe/HCursor.html"&gt;&lt;code&gt;HCursors&lt;/code&gt;&lt;/a&gt; maintain a cursor's history so relevant history may surface during an error indicating why the cursor operation failed.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;downField("x")&lt;/code&gt; moves the cursor's position into the &lt;code&gt;JSON&lt;/code&gt; object specified by &lt;code&gt;"x"&lt;/code&gt;. Very similar to how JavaScript accesses object via &lt;code&gt;obj["x"]&lt;/code&gt;. &lt;code&gt;get[Type]("field")&lt;/code&gt; performs a &lt;code&gt;downField&lt;/code&gt; to the &lt;code&gt;"field"&lt;/code&gt; key, parses the &lt;code&gt;JSON&lt;/code&gt; object at that point, and then casts it to the type specified. In my case I cast the &lt;code&gt;"age"&lt;/code&gt; field to an &lt;code&gt;Int&lt;/code&gt;. Casting tells Scala to take an object it thought was text and convert it into a Scala object like an &lt;code&gt;Int&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Scala definitely requires more thought when working with JSON. While Circe's techniques seem radically different from Javascript at first glance; these languages really have a lot in common. As an exercise try triggering different &lt;code&gt;Left&lt;/code&gt; branches to see the messages they produce.&lt;/p&gt;

</description>
      <category>scala</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Easy JSON with Scala</title>
      <dc:creator>Fulton Byrne</dc:creator>
      <pubDate>Sun, 20 Oct 2019 19:03:56 +0000</pubDate>
      <link>https://forem.com/fbyrne/easy-json-with-scala-5f6p</link>
      <guid>https://forem.com/fbyrne/easy-json-with-scala-5f6p</guid>
      <description>&lt;p&gt;&lt;em&gt;Update:&lt;/em&gt; I've been notified the &lt;code&gt;json4s&lt;/code&gt; has some fairly severe and public DoS issues.&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--ci2zuXlm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/589217042072346624/aZ5TAJ5C_normal.png" alt="Andriy Plokhotnyuk profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Andriy Plokhotnyuk
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @aplokhotnyuk
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4t6ys1m--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      &lt;a href="https://twitter.com/Fbyrne"&gt;@Fbyrne&lt;/a&gt; CAUTION: json4s is vulnerable under DoS/DoW attacks which exploit hash collision vulnerabilities of Scala's HashMap or parsing vulnerabilities of Java's BigInteger/BigDecimal values: &lt;a href="https://t.co/EtgtGvepfW"&gt;github.com/json4s/json4s/…&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      19:46 PM - 20 Oct 2019
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1186005842262482951" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WwRENZp4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1186005842262482951" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PFD0MJBa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1186005842262482951" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6wx1BHu3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;I'll make another post exploring this basic case using some of the libraries recommended in the thread.&lt;/p&gt;




&lt;p&gt;I had been searching for an easy way to work with JSON in Scala. Most libraries expect the user wants to create advanced encoding and decoding functionality; therefore making them a bit difficult to jump into.&lt;/p&gt;

&lt;p&gt;Fortunately I found this StackOverflow post that showed how to represent JSON using basic Scala data types such as &lt;code&gt;Map&lt;/code&gt;, &lt;code&gt;List&lt;/code&gt;, and &lt;code&gt;String&lt;/code&gt;:&lt;/p&gt;


&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;h1&gt;
          &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pTF_nE4a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/stackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
            &lt;a href="https://stackoverflow.com/questions/27948128/how-to-convert-scala-map-into-json-string" rel="noopener noreferrer"&gt;
               How to convert Scala Map into JSON String?
            &lt;/a&gt;
        &lt;/h1&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Jan 14 '15&lt;/span&gt;
            &lt;span&gt;Comments: &lt;/span&gt;
            &lt;span&gt;Answers: 7&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/27948128/how-to-convert-scala-map-into-json-string" rel="noopener noreferrer"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5MiFESHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/stackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          20
        &lt;/div&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rk_a5QFN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/stackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;For example, I have this Map value in Scala:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;val m = Map(
    "name" -&amp;gt; "john doe", 
    "age" -&amp;gt; 18, 
    "hasChild" -&amp;gt; true, 
    "childs" -&amp;gt; List(
        Map("name" -&amp;gt; "dorothy", "age" -&amp;gt; 5, "hasChild" -&amp;gt; false),
        Map("name" -&amp;gt; "bill", "age" -&amp;gt; 8, "hasChild" -&amp;gt; false)
    )
)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I want to convert…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    
      &lt;a href="https://stackoverflow.com/questions/27948128/how-to-convert-scala-map-into-json-string" rel="noopener noreferrer"&gt;Open Full Question&lt;/a&gt;
    
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;The really big takeaway is that &lt;code&gt;json4s&lt;/code&gt; performs recursive JSON encoding/decoding with fundamental Scala types where most basic libraries will only encode/decode one level deep.&lt;/p&gt;

&lt;p&gt;Getting started is fairly easy.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;1.&lt;/b&gt; Follow the &lt;a href="https://github.com/json4s/json4s#installation"&gt;installation steps&lt;/a&gt; for &lt;code&gt;json4s&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Add the following to your &lt;code&gt;build.sbt&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Change 3.6.7 to whatever the latest version is on Maven Central&lt;/span&gt;
&lt;span class="n"&gt;libraryDependencies&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="s"&gt;"org.json4s"&lt;/span&gt; &lt;span class="o"&gt;%%&lt;/span&gt; &lt;span class="s"&gt;"json4s-native"&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="s"&gt;"3.6.7"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;2.&lt;/b&gt; Decode JSON&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json4s._&lt;/span&gt;
  &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json4s.native.JsonMethods._&lt;/span&gt;

  &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;DecodeJSON&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;jsonString&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt;
      &lt;span class="s"&gt;"""
        |{
        |  "hello": "world",
        |  "data": {
        |   "number": 12,
        |   "lie": false
        |   }
        |}
        |"""&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;stripMargin&lt;/span&gt;

    &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;decoded&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonString&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decoded&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// JObject(List((hello,JString(world)),(data,JObject(List((number,JInt(12)), (lie,JBool(false)))))))&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Notice the output uses special wrapper classes to represent the JSON data types and structure. The full list is found &lt;a href="https://github.com/json4s/json4s#guide"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;b&gt;3.&lt;/b&gt; Encoding JSON&lt;/p&gt;

&lt;p&gt;Decoding recursively from Maps and Lists is possible as shown in the Stackoverflow post, but &lt;code&gt;json4s&lt;/code&gt; provides a domain specific language (DSL) for creating JSON.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json4s.JsonDSL._&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json4s.native.JsonMethods._&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Hello&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

&lt;span class="c1"&gt;// ~ is part of the json4s scala DSL&lt;/span&gt;
&lt;span class="c1"&gt;// it means these fields belong on the same Object level.&lt;/span&gt;
&lt;span class="c1"&gt;// It corresponds directly to a JSON comma (,).&lt;/span&gt;

  &lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;jsonScala&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"hello"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"world"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;
    &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"data"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"number"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;
        &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"lie"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;compact&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;jsonScala&lt;/span&gt;&lt;span class="o"&gt;)))&lt;/span&gt;
  &lt;span class="c1"&gt;// {"hello":"world","data":{"number":12,"lie":false}}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;There are many JSON libraries for Scala out there, but &lt;code&gt;json4s&lt;/code&gt; excels for quick and dirty projects involving dynamic JSON and simple types. Also, I'm really new to Scala so if you have a library you enjoy for working with JSON please share!&lt;/p&gt;

</description>
      <category>scala</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
